1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Basics for Sockets Programming

Discussion in 'Win32' started by Jaihind, Aug 26, 2006.

  1. Jaihind

    Jaihind New Member

    Socket ( Definition ):

    • Socket is End to End Point Communication.
    • Establishing a Connection Between two Applications or processes or threads.
    • Creating interface between two programs ( Server and Client Programs ).
    Socket is very useful for communication between systems, that's called inter process communication. Sockets can communicate within the systems or across network system.

    Socket System consist of client system as well as server system

    Client System


    Client System acts as a Receiver . In this system Requests to Server, and receive information from Server.​

    Server System


    Server system acts as a provider of information. It acts as an Administrator, to control over Network Systems ( Clients ). Provide information depending on Client request and responds to the client request.
    Socket programming, requires you to create two applications
    1) One is Server applications.
    2) Second one is client applications ( number of clients )

    Server application should be capable of handling more than one client if you have a requirement where number of clients will be connecting.

    The below table illustrates server and client related socket functions

    Socket Functions


    Server functions


    • socket
    • bind
    • listen
    • accept
    • read
    • write
    • close

    Client functions


    • socket
    • connect
    • send
    • receive
    • close

    Functionalities


    Socket
    » To Initialize a Socket​
    Bind
    » To attach an Interface​
    Listen
    » Listen for Incoming Connection​
    Accept
    » Permits an Incoming Connection​
    Connect
    » Establish a Connection on a Specified socket​
    Send
    » Send data on Connected Socket​
    Only two types of Specifications supported for Windows

    1) Stream Socket



    It provides sequenced, reliable connection oriented byte stream. It uses TCP [ Transmission control protocol ] to transmit data to the connected system in sequenced manner. It also waits for acknowledgement.

    2) Datagram Socket:



    It provides unreliable connection less service. It uses UDP [ User datagram protocol ] transport layer protocol and its not required to connect the network systems.

    UDP and TCP use the IP address and the port number to uniquely identify a particular process on a networked host.

    Windows Networking API's



    Windows API supports networking capabilities (WNet Functions). WNet functions to add an cancel network connections and to retrieve information about the current configuration of the network. The WNet functions enable your application to query and control network connections directly, or to give direct control of the network connections to the user.

    To enumerates network resource perform the following steps :
    1. Pass the address of a NETRESOURCE structure that represents the resource to the WNetOpenEnum function.
    2. Allocate a buffer large enough to hold the array of NETRESOURCE structures that the WNetEnumResource function returns, plus the strings to which their members point.
    3. Pass the resource handle returned by WNetOpenEnum to the WNetEnumResource function.
    4. Close the resource handle when it is no longer needed by calling the WNetCloseEnum function.

    See the following Code :
    Code:
    NETRESOURCE *p;
    DWORD Open;
    p»dwScope=RESOURCE_GLOBALNET;
    p»dwType=RESOURCETYPE_ANY;
    p»dwDisplayType=RESOURCEDISPLAYTYPE_SHARE|RESOURCEDISPLAYTYPE_DOMAIN;
    p»dwUsage=RESOURCEUSAGE_CONNECTABLE;
    p»lpLocalName="";
    p»lpRemoteName="";
    HANDLE han;
    Open=WNetOpenEnum(RESOURCE_CONNECTED,RESOURCETYPE_ANY,0,p,&han);
    if(Open!=ERROR_SUCCESS)
    	AfxMessageBox("Network connection successful",MB_OK);
    else
    	AfxMessageBox("Failure in opening connection",MB_OK);
    int c=0;
    DWORD Enum;
    DWORD cbBuffer=10000;
    ULONG dob=-1;
    LPNETRESOURCE lpnrLocal = (LPNETRESOURCE) GlobalAlloc(GPTR, cbBuffer);
    Enum=WNetEnumResource(han,&dob,lpnrLocal,&cbBuffer);
    if(Enum!=ERROR_SUCCESS)
    {
    	for(int i=0;i<dob;i++)
    	{	
    		AfxMessageBox(lpnrLocal[i].lpRemoteName,MB_OK); 
    	}
    }
    else
    {
    	AfxMessageBox("Failure",MB_OK);
    }
    ::GlobalFree(HGLOBAL(lpnrLocal));
    ::WNetCloseEnum(han);

    Socket API's explanation



    WSAStartup
    » Initiates use of Ws2_32.lib by a process​

    gethostname
    » Print the host name for the local machine​

    gethostbyname
    » gethostbyname function retrieves host information corresponding to a host name from a host database.​

    Code for finding out Host Name and Ip Address using Windows Socket
    Code:
    void MyFrame::MySocket(WSADATA w)
    {
    	int Res=::WSAStartup(MAKEWORD(2,0),&w);
    	if(Res==0)
    		AfxMessageBox("Success",MB_OK);
    	char name[255];
    	gethostname(name,sizeof(name));
    	CString Ans;
    	char m[]="Host Name: ";
    	Ans.Format("%s %s",m,name);
    	AfxMessageBox(Ans,MB_OK);
    	CString ip;
    	HOSTENT *host;
    	if(Res==0)
    	{
    		if((host=gethostbyname(name))!=0)
    		{
    			ip=inet_ntoa(*(struct in_addr*)*host»h_addr_list);
    		}
    	}
    	CString my;
    	char s[]="Host Address :";
    	my.Format("%s %s",s,ip);
    	AfxMessageBox(my,MB_OK);
    	WSACleanup(); 
    }
    
     
  2. Peter_APIIT

    Peter_APIIT New Member

    Good Job but i don't understand what is going there.

    Thanks for oyur explanations.
     
  3. wrecker

    wrecker New Member

    Could you please explain the code. Really unable to understand
     
  4. ec_ashish

    ec_ashish New Member

    Can't we do this socket programming using C language instead of C++? I think that will be easy to understand. Comments!!!
     
  5. wagmare

    wagmare New Member

    can u do it using threads
     
  6. vikas1234

    vikas1234 New Member

    Unix socket is easy to understand as compared to Windows.... I am a unix users ...but please it would be better if you explain the above code
     
  7. limon

    limon New Member

    Windows API supports networking capabilities (WNet Functions). WNet functions to add an cancel network connections and to retrieve information about the current configuration of the network. The WNet functions enable your application to query and control network connections directly, or to give direct control of the network connections to the user.

    To enumerates network resource perform the following steps :
    1. Pass the address of a NETRESOURCE structure that represents the resource to the WNetOpenEnum function.
    2. Allocate a buffer large enough to hold the array of NETRESOURCE structures that the WNetEnumResource function returns, plus the strings to which their members point.
    3. Pass the resource handle returned by WNetOpenEnum to the WNetEnumResource function.
    4. Close the resource handle when it is no longer needed by calling the WNetCloseEnum function.
     

Share This Page