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


the Internet and in various publications. There are just too many names to be named ( and, to tell the truth, a lot of the input was anonymous ) and we can't put them all up. But here are the names of some of the people whose work helped us bring this tutorial to you :-

Sorry, but we haven't compiled the names yet ! Give us a little time.

Taking POTShots at the Internet...

Before we start off with the first of our programs, we'll take a short break and tell you a little bit about how the Internet works. What we'll be telling you here is full of holes, so if you know all this already, skip it.

You'll understand how the Internet works better if you have a basic understanding of how the plain old telephone system ( POTS ) functions. For example, I pick up my phone in Bombay and dial up someone in Delhi. Is my phone connected to the phone in Delhi by a physical line ? of course not. The connection can be considered physical, but only in the loosest sense. What we actually have here is a virtual connection one which only lasts for as long as the communication itself. Even the route my voice takes along the wires can vary, not only between one call and the next, but also within a call. So one moment my voice will be routed via Bombay, Madras, Delhi and ten seconds later, via Bombay, Calcutta, Delhi, depending on the line conditions and the load.

This concept of a virtual connection is at the very heart of the Internet. The routes my TCP/IP packets take to and from a certain site can vary. One moment they'll come down one pathway and a few seconds later, by another. This routing of information packets is what information transfer on the 'net is based on. The router is what telephone companies spend the most amount of time and money on, because it is the router that calculates the load factors on each line and decides which route a packet should take, optimizing the limited number of telephone cables and speeding up information exchange. Infact, routers often send the packets along convoluted and extremely complex routes in order to bypass busy exchanges and land lines. In fact, most telephone exchanges in the world today use the Packet Switching Networks (PSN's) explain above. This means that every word you say is converted into a packet and send winging it's way to it's destination.

Since the routers ( in their never ending quest for the fastest route ) force the information to take such convoluted paths, these packets sometimes wander off and go AWOL. This is OK if you're having a conversation with someone and you miss a vowel or two, but if, for instance, you're transporting binary files the results can be disastrous. It was to combat this problem of missing packets that the TCP/IP protocol was established. A protocol, by the way, is any form of communication which follows certain predefined rules.

The TCP/IP protocol is actually two protocols sandwiched one above the other. The IP (Internet Protocol) protocol deals exclusively with the routers. It's IP's job is to make sure your packet goes from one end to the other in the shortest possible time. IP is the one who informs the router about the location of it's destination, it's source and other such details. The IP protocols primary concern is speed. It has to try and get to the destination as fast as possible and it cares about nothing else. The IP protocol has sacrificed reliability for speed and it shows. If the Internet we're to rely exclusively on IP, the result would be absolutely chaotic !

It was to combat this problem of unreliability that the TCP ( Transmission Control Protocol ) protocol was established. The TCP protocol is the exact opposite of the IP protocol. It's primary concern is reliability and it's makers forfeited speed to get it. It is the TCP protocol that takes care of checksums and sequence, to make sure that every packet reaches and is used in the correct order.

When TCP/IP was invented, the architects made it quite clear that TCP/IP would be entirely machine and operating system independent. So these furious debates about the supremacy of the Macintosh over Wintel machines or vice versa, no longer makes any sense. On the Internet, it's impossible to distinguish between the two.

A misconception prevalent in the programming world is that something called "TCP/IP programming" exists. TCP/IP is a very low level protocol and practically no one writes TCP/IP code. It's like trying to write a word processor for Windows95, in Assembler ! It's possible, but the people who try and do it often end up in large comfortable farmhouse surrounded with electrified barbed wire, wearing strange white jackets and learning to weave straw baskets !!

Of course, we were crazy enough to take the plunge ! Check out our tutorial's on the various Internet Protocols, but read this tutorial first.

The only sane way to write Internet related code is to use Sockets Programming or as it is called under Windows, WinSock Programming. The WinSock internally calls TCP/IP, but it's easier to use because it equates Internet information transfers to file transfers to and from your hard disk. So we'll be teaching you WinSock programming in this tutorial and not direct TCP/IP.

Getting our feet wet...

Let's not waste anymore time over theoretical discussions on the workings of the Internet, instead, let's jump right in and I promise I'll explain all the important concepts as we proceed. After all, it makes more sense to explain the theory after you've seen the source code and tried it out.

Here's a program that leads up to our first true Internet client. If you're already familiar with C-SDK Windows programing, then jump ahead to the Time Client.

Note:-To run this program, create a project in Visual C++ ( We're using Visual C++ 4.0 ) and insert the file a1.cpp into the project a1.

Now build the project.

a1.cpp

     #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;
     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)
             {
                MessageBox(0,"end","end",0);
             }
             if ( x == WM_DESTROY)
                     PostQuitMessage(0);
             return DefWindowProc(w,x,y,z);
     }



This is a basic Windows program. We've initialized lpfnWndProc, a member of the structure a which looks like WNDCLASS to the function zzz. This function will be our callback function.So, whenever anything of importance happens to the window (the window is minimized, maximized etc.), our call back function is called.

We'll be putting all our WinSock code in the callback function.

The Time Client


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

void abc(char *p)
{
      FILE *fp=fopen("z.txt","a+");
   fprintf(fp,"%s\n",p);
   fclose(fp);
}

WNDCLASS a; HWND b; MSG c; char aa[200]; SOCKET s; struct hostent h;
WSADATA ws; DWORD e; int ii,dw; char bb[100]; struct sockaddr_in


Page : << Previous 2  Next >>