Topic : Socket Programming
Author : Vijay Mukhi
Page : << Previous 9  Next >>
Go to page :


      strcpy(R,"USER userid\r\n");        // enter your user id here
       i=send(S,R,strlen(R),0);
       sprintf(aa,"send %d ",i);
       abc(aa);
       i=recv(S,R,10000,0);
       sprintf(aa,"recv %d R %s",i,R);
       abc(aa);
       strcpy(R,"PASS password\r\n");   //enter your pass word here
       i=send(S,R,strlen(R),0);
       sprintf(aa,"send %d ",i);
       abc(aa);
       i=recv(S,R,10000,0);
       sprintf(aa,"recv %d R %s",i,R);
       abc(aa);
       strcpy(R,"STAT\r\n");
       i=send(S,R,strlen(R),0);
       sprintf(aa,"send %d ",i);
       abc(aa);
       i=recv(S,R,10000,0);
       sprintf(aa,"recv %d R %s",i,R);
       abc(aa);
       strcpy(R,"RETR 1\r\n");
       i=send(S,R,strlen(R),0);
       sprintf(aa,"send %d ",i);
       abc(aa);
       i=recv(S,R,60000,0);
       sprintf(aa,"recv %d ",i);
       abc(aa);
       sprintf(aa,"R...%s",R);
       abc(aa);
       i=recv(S,R,60000,0);
       sprintf(aa,"recv %d ",i);
       abc(aa);
       sprintf(aa,"R...... %s",R);
       abc(aa);
       MessageBox(0,"Hi","Over",0);
       return 0;
     }



If you quickly run through the program, you'll see that it's really quite similar to the SMTP program. The only difference is that here we have to identify ourselves as authorized users. This is necessary because you wouldn't want someone else reading your mail, now would you? The program has no call back and it runs sequentially.
After receiving our user ID and password, the server will authenticate us. If we pass the test then the server will wait patiently for input from us.

We immediately send the server a STAT command. This command tells the server to send us the STATus of our mailbox. We totally ignore the response we receive and ask for the first mail in the mailbox by sending RETR 1 which means RETRieve mail one. The message is stored in an array and displayed.

PING and TRACEROUTE

In order to understand these programs a little better, let's first try and understand how our packet travels over the wire. When a data packet, all wrapped up in it's TCP/IP cocoon, zooms across the Internet, it passes through many routers. Each router checks the information in the IP header and then sends the packet to the next router on the way to it's final destination. Each time the packet encounters a router in it's path, the value in it's Time To Live field (a one byte field in the IP header) is decrement by one. The TTL field contains a number from 0 - 255 and this is the maximum number of routers the packet can pass through before being destroyed. So when the TTL is 1, the router decrements it by one to make it zero and then it drops it. The TTL field was created to make sure that a packet that got lost wouldn't end up wandering around on the Internet till eternity rolled by. If the number in the TTL is 10, then the packet must reach it's destination within 10 router hops, or else it'll die. When ever a router kills a packet because it's TTL has expired, it sends us an ICMP message informing us of the loss.

ICMP is another protocol we should know about before we begin. ICMP or the Internet Control Message Protocol rides piggy back on IP and is used to deal with routers and to check whether a certain server is up or not. It's not used very often and in fact, in WinSock version 1.01 under in Windows 95 it's a little complicated to implement. That's set to change when WinSock version 2.0 is released.

Now imagine a situation when we want to download a 5MB binary file. Sometimes it takes quite a while to download such a big file over a 14.4 kbps link. At other times the file may come at a fair clip. This happens because the number of routers between different sites and at different times varies. The more the router, the slower the download. It would therefor be wise to check the condition of the line before we start and that's where Ping comes in.

Ping, the program given below, simply sends one packet to the destination site and times it. This tells us how fast or slow a link is and whether the site is up. Ping sends an ICMP (Internet Control Management Protocol) packet, a protocol understood by all machines on the Internet.

Ping

P.C


     #include <windows.h>
     struct o
     {
             unsigned char Ttl,Tos,Flags,OptionsSize,*OptionsData;
     };
     struct
     {
             DWORD Address;
             unsigned long  Status,RoundTripTime;
             unsigned short DataSize,Reserved;
             void *Data;
             struct o  Options;
     } E;

     HANDLE hIP;WSADATA wsa;HANDLE hIcmp;DWORD *dwIPAddr;struct hostent *phostent;
     DWORD d;char aa[100];struct o I;

     HANDLE ( WINAPI *pIcmpCreateFile )( VOID );
     BOOL ( WINAPI *pIcmpCloseHandle )( HANDLE );
     DWORD (WINAPI *pIcmpSendEcho)(HANDLE,DWORD,LPVOID,WORD,LPVOID,
                                             LPVOID,DWORD,DWORD);

     int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrev,
                                     LPSTR lpCmd,int nShow )
     {
             hIcmp = LoadLibrary( "ICMP.DLL" );
             WSAStartup( 0x0101, &wsa );

             phostent = gethostbyname( "www.microsoft.com");
             dwIPAddr = (DWORD *)( *phostent->h_addr_list );

             pIcmpCreateFile=GetProcAddress( hIcmp,"IcmpCreateFile");
             pIcmpCloseHandle=GetProcAddress( hIcmp,"IcmpCloseHandle");
             pIcmpSendEcho =GetProcAddress( hIcmp,"IcmpSendEcho" );

             hIP = pIcmpCreateFile();
             I.Ttl=6;
             pIcmpSendEcho(hIP,*dwIPAddr,0,0,&I,&E,sizeof(E),8000 );
             d=E.Address;
             phostent = gethostbyaddr((char *)&d,4,PF_INET);
             sprintf(aa,"gethostbyaddr %p",phostent );
             MessageBox(0,aa,aa,0);
             if ( phostent  != 0 )
                     MessageBox(0,phostent->h_name,"hi",0);
             wsprintf(aa,"RTT: %dms,TTL:%d", E.RoundTripTime,          E.Options.Ttl );
             MessageBox(0,aa,"hi",0);
             pIcmpCloseHandle( hIP );
             FreeLibrary( hIcmp );
             WSACleanup();
     }




This looks a little complicated and it is! This isn't code we've written, rather we've downloaded it off the Web. The variable names aren't three alphabets

Page : << Previous 9  Next >>