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


PostQuitMessage(0);
             return DefWindowProc(w,x,y,z);
     }


In this program, we've listed out the members of the structure with their respective values before and after WSAStartup function is called. After running the program, check the file z.txt in the root directory.
The next important function is socket(), which initializes the WinSock socket to be used for communication. A socket is a hypothetical entity, created solely to help us understand Internet programming better. It's first parameter, PF_INET is a number that informs the socket that it is to be used for the Internet. WinSock sockets can also be used over Novel and other networks and therefore the intended use has to be specified. SOCK_DGRAM tells the socket that the protocol to be used for communication is UDP/IP, or as it is commonly known, UDP (User Datagram Protocol). UDP, like TCP is an Internet protocol which rides piggy back on IP. Unlike TCP however, UDP is not a very stable or reliable protocol, but it is much faster than the former. UDP is used in cases where reliability is not an issue, like with Real Audio and Internet Phone, where a few lost packets don't really affect sound quality, but where speed is of the essence. Since sending a simple request for the current date and time is not really that demanding a job, UDP works fine. If the socket is created without problems, then the socket function returns a zero, which is caught and displayed by s. ( SOCKET is a macro which stands for 'unsigned int' ).

After creating a socket, we have to initialize the members of a structure called Sa. Sa.sin_family=AF_INET means that the packets will travel on the Internet. Sa.sin_addr.s_addr = inet_addr ("140.252.1.32") fills up this member of the structure with the IP address of the destination server. An IP address is a bit like a phone number. For me to talk to you, I must have a phone ( with its own number ) and I must know your phone number. To be on the Internet, every computer must have an IP address, otherwise the packets don't know where to go. A unique IP address, which is a long number ( 232 bits ), is given to each and every computer logged onto the Internet. If the IP number is permanent, it means that it is the number of a server, which is always on-line. If the number is random, it means it belongs to a dial-up subscriber, who is given an IP number, valid for only for the length of the session.

When packet is sent, the IP header holds the IP address from which the packet has come. That is, it holds the IP address of your computer, e.g. 10. The IP header also holds the IP address of the destination computer, which is, for example, 22. Your data packet is first sent to you ISP's router, who checks the IP header for the destination address. Since all routers know the IP addresses of the other routers and also the range of IP addresses they handle, your router, which handles IP address from 0 to 10 for example, sends the packet to a router in Singapore, which handles IP address from 15 to 25. That router then sends the data to the target computer, whose IP address of 22 falls within it's range. Of course, this is a highly simplified example, involving only two routers. In actuality, your packet may pass through about six to seven routers, going one way and about the same number coming back.

IP addresses are usually given in the Dotted Decimal Notation, where every char of the long is given individually and separated with a dot. There is no compulsion to do so, it just increases the readability of the IP address. However, to use the address, it has to be converted into a long first and the function inet_addr does just that.


   #include <windows.h>
     #include <stdio.h>
     void abc(char *p)
     {
             FILE *fp=fopen("c:\\z.txt","a+");
             fprintf(fp,"%s\n",p);
             fclose(fp);
     }
     WNDCLASS a;HWND b;MSG c;char aa[200];
     WSADATA ws;DWORD e;long dd;
     long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
     int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
     {
             a.lpszClassName="a1";
             a.hInstance=i;
             a.lpfnWndProc=zzz;
             a.hbrBackground=GetStockObject(WHITE_BRUSH);
             RegisterClass(&a);
             b=CreateWindow("a1","aaa",WS_OVERLAPPEDWINDOW,1,1,10,20,0,0,i,0);
             ShowWindow(b,3);
             while ( GetMessage(&c,0,0,0) )
                     DispatchMessage(&c);
             return 1;
     }
     long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
     {
             if ( x == WM_LBUTTONDOWN)
             {
                     dd = inet_addr("140.252.1.32");
                    sprintf(aa,"dd=%ld",dd);
                    abc(aa);
                    MessageBox(0,aa,aa,0);
                    MessageBox(0,"end","end",0);
             }
             if ( x == WM_DESTROY)
                     PostQuitMessage(0);
             return DefWindowProc(w,x,y,z);
     }


Here's a small little program that demonstrates the effect inet_addr has on a number in the Dotted Decimal Format.

Next we have Sa.sin_port = htons( 13 ). This member of the structure holds the port number the packets must go to. The ports referred to here, are not the serial or parallel ports. A port here is simply a number which resides in the TCP header. For example, the Time server uses port 13. This means, whenever a packet appears at the server end's front door, bearing the port number 13, it is passed onto the time server code, which does with it what it may. If there was no such thing as a port number, every packet that arrived would have to be sent to every server on that machine, leading to a massive and unavoidable overhead. So when we say that the Time server " is on port 13 ", we mean that the Time server is listening to packets that hold the number 13 in the TCP header.

The function htons has been created to combat a problem that arose when the creators of these protocols attempted to make them machine independent. The Intel chip stores long numbers as four chars stacked one above the other, smaller numbers first. So a number like 515 will be stored as 3 and 2, i.e. 3x1 plus 2x256, which equals to 515. However, the Motorola chip, used in the Macintosh stores number the other way round, so 515 is stored as 2 and then 3. If a number stored in this format is read by a machine with the Intel chip, instead of 515, the answer will be 2x1 plus 3x256, equals 770 !! So another protocol was established, the Network Byte Order Protocol. From now on, all numbers will be stored the Motorola way. So people like you and me, who use Intel machines, require a function like htons to convert the number 13 into the format that the Macintosh likes. If you don't want to use htons, you can always write, 13*256 and it will still work.


   #include <windows.h>
     #include <stdio.h>


Page : << Previous 4  Next >>