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


    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];SOCKET s;
     WSADATA ws;DWORD e;long ii;
     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)
             {
                     ii=htons(2);
                     sprintf(aa,"ii.htons(2)=..%ld",ii);
                     abc(aa);
                     MessageBox(0,aa,aa,0);
                     ii=htons(512);
                     sprintf(aa,"ii.htons(512)=..%ld",ii);
                     abc(aa);
                     MessageBox(0,aa,aa,0);
                     MessageBox(0,"end","end",0);
             }
             if ( x == WM_DESTROY)
                     PostQuitMessage(0);
             return DefWindowProc(w,x,y,z);
     }


This example should clear up any lingering doubts you might still harbor.
After filling up the structure Sa, we send a request to the Time server, asking for the date and Time. We copy the string " hello how are you " into the array bb. bb can hold any string, the time server doesn't pay attention to it. All it wants is some characters and the senders IP address, which it extracts from the IP header.

We then use sendto() to send the contents of the array over to the Time server. We give the function sendto(), in order, the address of the socket, the array bb, the size of the array, a null, the typecasted address of the structure Sa and finally, the size of the structure Sa. Using the information contained in the structure Sa and in the socket, the function sends the contents of the array over to the Time server on port 13, at IP address 140.252.1.32.

After sending the request, we wait at the function recvfrom() for the answer. Recvfrom() is a blocking function, i.e. program execution doesn't proceed until recvfrom()receives an answer and returns control. Before it does, recvfrom() fills up the array bb with the reply. We've gone the extra mile and made the variable dw equal to the size of Sa, you cannot put sizeof(Sa) as a parameter directly.

We can then access bb and display it's contents using a message box.

So, there you have it. A time client which accesses a time server and asks it for the current time of the area in which the server resides.

The Time Server

#include <windows.h>
#include <winsock.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];char bb[20000];char cc[2000];int xx;
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;
}
WSADATA ws;SOCKET s,s1,s2;sockaddr_in A,A1;int d,d1,d2,dd;
struct sockaddr_in A;
long _stdcall zzz (HWND w,UINT x,WPARAM y,LPARAM z)
{
   if ( x == WM_LBUTTONDOWN)
   {
      d=WSAStartup(0x0101,&ws);
      sprintf(aa,"d = %ld",d);
      abc(aa);
      s=socket(PF_INET, SOCK_DGRAM,0);
      sprintf(aa,"socket %ld",s);
      abc(aa);
      A.sin_family=AF_INET;
      A.sin_port = htons(13);
      A.sin_addr.s_addr =INADDR_ANY;
      d=bind(s,(struct sockaddr *) &A,sizeof(A));
      sprintf(aa,"bind = %ld",d);
      abc(aa);
      MessageBox(0,aa,aa,0);
      int dw=sizeof(A);
      d=recvfrom(s,bb,100,0,(sockaddr *)&A,&dw);
      sprintf(aa,"recvfrom =%ld..bb=%s",d,bb);
      abc(aa);
      d=sendto(s,"hello",5,0,(sockaddr *)&A,sizeof(A));
      sprintf(aa,"sendto=%ld.. ,d);
      abc(aa);
      MessageBox(0,aa,aa,0);
      MessageBox(0,"end","end",0);
   }
   if ( x == WM_DESTROY)
      PostQuitMessage(0);
   return DefWindowProc(w,x,y,z);
}



Now that you've written your first client, it's time for you to write you're very first Internet server. As before, it's not much, but it's substantially more than what you could write before, so I see absolutely no reason to complain !

To run this program, first create a project in Visual C++ ( We're using Visual C++ 4.0, as mentioned before ) and insert these files into the project :-


A2.cpp - That's the file given above
Wsock32.lib -
Build the project. As before, be sure to log onto your local Internet Service Provider before you run the .exe. Now run the file. Clicking in the window starts up the server. Now go back to windows ( Alt + Tab) and click on the start button, click on run and type in winipcfg. This is one of the many nifty little utilities which come with Windows95. Winipcfg is stuffed full of useful information and you'll understand all of those weird words as we proceed. What we're presently interested in is the IP address. Since your IP address changes every time you log on, winipcfg is one of the simpler methods to find out the new IP address. Now open your time client workspace and edit the code, changing the line Sa.sin_addr.s_addr = inet_addr("140.252.1.32"); to Sa.sin_addr.s_addr = inet_addr("666.666.666.665");, 666.666.666.665 being you're current IP address (make sure you don't add one to that number, or you'll end up connected to Gate's server, a definite no no !). Compile and run both the server and client programs, in that order. The words Hello appear in the message box on you screen. Yes ! those words have been sent to you by your server !

N.B - By the way, did you notice a mistake I've made with the IP address? Each number in the Dotted Decimal Notation represents a char and the highest value possible with a char is 256, yet I've miraculously managed to stuff 666.666.666.665 in as the IP address ! If you try this, you will get weird errors and you'll deserve them too !

Instead of obtaining your IP address through winipcfg, change the IP address from 140.252.1.32 to 127.0.0.1. This is a reserved address and it loops the IP packets back to your machine. This process is known as a Local LoopBack. This enables you to run both the client and server on the same machine, without being live on the Internet. This is a common technique used by many software firms to test their software and it's very convenient.

The server code is similar to the client and I see no need to wear my fingers away typing the same thing again as I've already explained the client in detail. There are a few changes though and I'll be explaining those.

The first thing you'll notice is that the third member of the structure A, A.sin_addr.s_addr, is equal to INADDR_ANY. It is a #define, which means that the server will accept requests from any IP address. If we were to replace INADDR_ANY with the IP address of Microsoft's server, then your server will only accept requests that originate from there. The server knows about the source from the IP header.

The function bind(), "binds" a server to a certain port. The image that comes to mind is that of a funnel, with the port number 13 engraved on it, sticking out of the wall. This particular funnel is dripping IP packets in a steady stream because there is no one around to

Page : << Previous 5  Next >>