Basics for Sockets Programming

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

  1. Jaihind

    Jaihind New Member

    Joined:
    Aug 4, 2006
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0

    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

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Good Job but i don't understand what is going there.

    Thanks for oyur explanations.
     
  3. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    Could you please explain the code. Really unable to understand
     
  4. ec_ashish

    ec_ashish New Member

    Joined:
    Aug 21, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    May 14, 2008
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    soft.engg
    Location:
    Bangalore
    can u do it using threads
     
  6. vikas1234

    vikas1234 New Member

    Joined:
    Aug 21, 2008
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Sep 29, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice