Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Basics for Sockets Programming (http://www.go4expert.com/articles/basics-sockets-programming-t1266/)

Jaihind 26Aug2006 12:42

Basics for Sockets Programming
 

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: CPP

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: CPP

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();
}


Peter_APIIT 20Apr2007 13:34

Re: Basics for Sockets Programming
 
Good Job but i don't understand what is going there.

Thanks for oyur explanations.

wrecker 30Jun2007 19:11

Re: Basics for Sockets Programming
 
Could you please explain the code. Really unable to understand

ec_ashish 21Aug2007 19:11

Re: Basics for Sockets Programming
 
Can't we do this socket programming using C language instead of C++? I think that will be easy to understand. Comments!!!

wagmare 29Jul2008 15:03

Re: Basics for Sockets Programming
 
can u do it using threads

vikas1234 12Sep2008 16:07

Re: Basics for Sockets Programming
 
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

limon 29Sep2008 15:50

Re: Basics for Sockets Programming
 
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.


All times are GMT +5.5. The time now is 12:22.