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

claim them. By binding my server to a port, I attach a pipe to the funnel channeling all the port 13 packets to my server, where they can be properly dealt with. The bind function accepts certain parameters, such as the socket address, the typecasted address of the structure A and it's size.

After the bind statement, control passes to recvfrom(), which, since it is blocking, waits for a connection to be established. The moment an UDP/IP packet appears, recvfrom() is activated and it fills up the array bb with the incoming characters. It also stuffs the structure A with details like the IP address of the computer that sent the request.

After recvfrom() is sendto(), which sends the string "hello" to the remote client. In real time, the current time is calculated, put into an array and sent to the client, but you can do that for yourself. The structure A is necessary because that's where the IP address of the remote client is stored. Infact, here's an exercise to the user. Open the header file and find out the other members of the structure sockaddr_in and display them using message boxes.

This server can only handle one user request at one time, unlike the other servers on the Internet who can chew through a hundred requests every second. But we'll get to that later.

And that just about wraps it up for now, on to the Web browser !!

A Web Browser !! Well, almost...

#include <windows.h>
#include <stdio.h>
void abc (char *p)
        FILE *fp=fopen("z.txt","a+");
WNDCLASS a;HWND b;MSG c;int d;char aa[100];char bb[100];
WSADATA ws; SOCKET s;struct sockaddr_in A;int ii;long gg;
char cc[100];
long _stdcall zzz (HWND,UINT,WPARAM,LPARAM);
int _stdcall WinMain(HINSTANCE i,HINSTANCE j,char *k,int l)
        while ( GetMessage(&c,0,0,0) )
        return 1;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
        sprintf(aa,"WSAStartup ..%ld",gg);
        s = socket(AF_INET,SOCK_STREAM,0);
        sprintf(aa,"socket s = %ld",s);
        A.sin_family = AF_INET;
        A.sin_port = htons(80);
        A.sin_addr.s_addr = inet_addr("");
        d=connect(s,(struct sockaddr *)&A,sizeof(A));
        sprintf(aa,"d = %ld",d);
        strcpy(bb,"GET / \r\n");
        while(ii !=0)
                strset(cc,' ');
if ( x == WM_DESTROY)
return DefWindowProc(w,x,y,z);

All right, so this isn't much of a browser !! All it does is connects to the Microsoft's site and downloads an HTML file. Though simplistic, this is what really happens under the fancy hoods of Netscape Navigator or Internet Explorer. Ofcourse, they have a lot more code and they do a lot more, ( like display the HTML file on the screen, for starters !! ).

HTTP is the real protocol behind the popularity of the Web. You can read this document because it exists. HTTP, like most other high level protocols, uses TCP/IP to communicate between the client ( any browser ) and the server. The reason most top-level protocols use TCP/IP instead of UDP is because it is reliable and reliability is often more important than speed.

Anyway, lets understand the code.

We first have our usual WSAStartup() with the version number and the address of WSADATA. After that we define a socket. As before, AF_INET ( same as PF_INET ) means that this socket will be used for the Internet and not for any other protocol like Novel etc. However, in this program, instead of SOCK_DGRAM, we have a SOCK_STREAM. SOCK_DGRAM stood for UDP while the SOCK_STREAM stands for the TCP, which is a 'streaming protocol' (whatever that means !)

After the socket(), we have to fill up the members of A. Sin_family is equal to AF_INET and sin_port is equal to 80, the port number of HTTP. As before, the address is that of Microsoft's site on the Internet.

But now, instead of copying the string and sending it immediately, as we did in UDP, we first connect to the other server. For that we have the connect(). To connect all the necessary information like the socket, the address of structure A and it's size is supplied. Now we use send() instead of sendto(). Check the value of d. If it is 0, then all is well, it the value is non-zero, then an error has occurred.

Connect() is not used with UDP because UDP is a connectionless protocol, one which needs no connection to be made. All you do is,go directly to the server and clobber it with something or the other. This approach can and does lead to problems. What if the server is down? Or if it's too busy to respond? You'll never know the answers to these. Your recvfrom() will just keep waiting indefinitely for a response. Since UDP refuses to be polite and make connections before it opens it's big mouth, it's termed a Datagram Oriented Protocol.

Right after the connect(), we copy the command GET / \r\n into the array bb. The \r\n is an absolute must and cannot be omitted. We then have a send() statement, which uses s to extract all the relevant information about the connection. The GET command, a part of the HTTP syntax, does exactly what you think it does, it gets a file specified by you. The / is to pick up the default .htm or .html file from the server. Some sites have index.htm or index.html or default.htm or default.html specified as the default file. To get the default file no matter what, send a GET / \r\n ). Netscape and IExplorer both get this file as soon as they connect to a site. Since the receive() returns the number of bytes it receives, we get a 0 when it's all over. We can, therefore, set up a loop as shown and copy all the data into a file on disk. If you want, you can write some more code to strip off everything in angle brackets (<>) and display what remains on the screen. That's all there is to it !!

Just what exactly is a stream oriented protocol? This is a quoestion you may ask. Well, in a stream oriented protocol, the server you're connected to sends the data as large a chunk as it deems fit and you receive it in as many chunks as you want. Here for instance, the server could send us the data in chunks of 20 bytes each, yet, since we have an array 100 bytes large, we grab them all in one fell swoop. In a Datagram oriented protocol, if the chunks are 20 bytes large, then we need to grab them in 20 byte chunks, no alternatives here.

The TCP/IP Stack

TCP and IP both cocoon your data in their headers used to protect it during transit and to hold valuable information about it. However, once the data packet reaches your computer, something has to be done about these headers or they may be considered part of your data and displayed on screen !! Some way has to be found to get rid of them. That's where the TCP/IP stack comes in.

The TCP/IP stack is part of the Windows 95 operating system and it is the one who strips away the TCP/IP headers. The

Page : << Previous 6  Next >>