Written by: Udhaya Kumar.V 
Document  : TCP sample programs in UNIX environment
Date      : December – 2000
Contact   : URL: http://www.udhaya.com , Mail Id: [email protected]

TCP server sample programme for UNIX environment

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <strings.h>

#include <unistd.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <netinet/in.h>

#include <arpa/inet.h>

 

class TCPServer

{

private:

      struct sockaddr_in ServerAddress,ClientAddress;

      int Server,Client,ServerHandle,ServerPort;

      char Data[1024],buff[25];

public:

      TCPServer( char Address[15], int PortNo );

      ~TCPServer () {}

      int         CreateServer( void );

      int         ServerListen ( int BackLog );

      int         ServerAccept ( void );

      void  DataSend ( char *Buf );

      char  *DataRecieve ( void );

};

 

TCPServer :: TCPServer( char Address[15], int PortNo )

{

      ServerAddress.sin_family = AF_INET;

      ServerAddress.sin_port = htons ( PortNo );

      ServerAddress.sin_addr.s_addr = inet_addr ( Address );

      //bzero ( ( char * ) &ServerAddress, sizeof (ServerAddress) );

      bzero ( &ServerAddress.sin_zero, 8 );

}

 

int TCPServer :: CreateServer( void )

{

      if ( ( Server = socket ( AF_INET, SOCK_STREAM, 0 ) ) < 0 )

            printf ( "\nSocket Error..." );

      else

      {

            printf ( "\nServer Socket Created ..." );

            if ( bind ( Server, (struct sockaddr * ) &ServerAddress,16 )<0 )

                  printf ( "\nBind Error..." );

            else

                  printf ( "\nServer Binded ... " );

      }

      return ( Server );

}

 

int TCPServer :: ServerListen ( int BackLog )

{

      int Ret;

      if ( ( Ret = listen ( Server, BackLog ) ) < 0 )

            perror ( "Listen Error:" );

      else

            printf ( "\nListen ... " );

      return ( Ret );

}

 

int TCPServer :: ServerAccept ( void )

{

      if ( ( ServerHandle = accept ( Server , (struct sockaddr *) &ClientAddress, &Client ) ) < 0 )

            perror ( "Accept Error :" );

      else

      {

            printf ( "\nConnection from %s, port %d", inet_ntop ( AF_INET, &ClientAddress.sin_addr, buff, sizeof ( buff )), ntohs ( ClientAddress.sin_port ) );

            printf ( "\nClient Connected ...." );

      }

      return ( ServerHandle );

}

 

void TCPServer :: DataSend ( char *Buf )

{

      FILE  *fp;

      char  Str[1024];

      fp = fopen ( Buf, "r" );

      if ( fp == NULL )

      {

            printf ( "File Not Found " );

      }

      else

      {

            while ( !feof(fp) )

            {

                  fgets ( Str, 1024, fp );

                  puts ( Str );

                  if ( write ( ServerHandle, Str, strlen(Str) ) < 0 )

                        perror ( "Write :" );

            }

            write ( ServerHandle, "end", strlen("end") );

      }

      fclose ( fp );

}

 

char * TCPServer :: DataRecieve ( void )

{

      memset ( Data, 0, sizeof(Data) );

      if ( read ( ServerHandle, Data, sizeof ( Data ) ) <= 0 )

      {

            close ( Client );

            perror ( "Read :" );

            exit ( 1 ) ;

      }

      else

      {

            printf ( "\nData : %s", Data );

            return ( Data );

      }

}

 

main ()

{

      char  *Str;

      int         SHandle;

      int         Cid;

      TCPServer *Server = new TCPServer ( "140.0.0.37", 7000 );

      Server->CreateServer ();

      Server->ServerListen( 10 );

      while ( 1 )

      {

            Str = (char *) malloc ( 1024 );

            SHandle = Server->ServerAccept ();

            Cid = fork ();

            if ( Cid == 0 )

            {

                  while ( 1 )

                  {

                        Str = Server->DataRecieve();             

                        Server->DataSend ( Str );

                        wait ( 0 );

                  }

            }

      }

}

 

TCP Client sample programme for UNIX environment

 

#include <stdio.h>

#include <unistd.h>

#include <string.h>

#include <strings.h>

#include <sys/socket.h>

#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <stdlib.h>

 

main ()

{

      struct sockaddr_in ser;

      char buf[1024],Str[100];

      int sd,size=256;

 

      if ( ( sd = socket ( AF_INET, SOCK_STREAM, 0 ) ) < 0 )

      {

            perror ( "Socket error" );

            exit ( 1 );

      }

 

      ser.sin_family = AF_INET;

      ser.sin_port = htons (7000);

      ser.sin_addr.s_addr = inet_addr ("140.0.0.37");

      bzero ( &ser.sin_zero, 8 );

 

      if ( connect ( sd, ( struct sockaddr * ) &ser, sizeof(ser) ) < 0 )

      {

            perror ( "Connect:" );

            exit (2);

      }

      else

            printf ( "\nClient Connected with server.... \n");

 

      while ( 1 )

      {

            printf ( "\nEnter FileName :" );

            fflush ( stdin );

            gets (buf);

 

            if ( write ( sd, buf, strlen(buf) ) != strlen(buf) )

            {

                  perror ( "Write Error:" );

                  printf ( "Error : " ); getchar ();

            }

            while ( 1 )

            {    

                  read ( sd, buf, size );

                  if ( strcmp ( buf, "end" ) == 0 )

                        break;

                  printf ( " \n%s", buf );

                  memset ( &buf[0], '\0', 1024 );

            }

 

      }

}

 

Get the Host Name


#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

 

main ( int argc, char **argv )

{

      char              LocalHost[ 256 ], LenthTed[ 256 ];

      ulong_t                 addr;

      int                     LocalLen;

      struct hostent    *OurHost;

      LocalHost[ 0 ] = '\0';

 

      if ( argc == 1 )

      {

            if ( gethostname ( LocalHost, sizeof ( LocalHost ) ) == -1 )

            {

                  perror ( "Error :: " );

                  exit ( 1 );

            }

            else

            {

                  OurHost = gethostbyname ( argv[ 2 ] );

                  if ( OurHost != NULL )

                  {

                        printf ( "gethostbyname" );

                        printf ( "\nNombre  %s", OurHost->h_name );

                        printf ( "\nAddr    %i", OurHost->h_addr_list[ 0 ] );

                        printf ( "\nIp Addr    %s", inet_ntoa ( *( struct in_addr * ) OurHost->h_addr_list[ 0 ] ) );

                  }

                  else

                  {

                        printf ( "Error in Host" );

                  }

                  exit ( 2 );

            }

      }

      else

      {

            addr = inet_addr ( argv[ 1 ] );

            if ( addr == -1 )

            {

                  OurHost = gethostbyname ( argv[ 1 ] );

                  if ( OurHost == NULL )

                        printf ( "\nGiven host not found" );

                  else

                  {

                        printf ( "Number %s\n", OurHost->h_name );

                        printf ( "Address %i\n", OurHost->h_addr_list[ 0 ] );

                  }

            }

            else

            {

                  OurHost = gethostbyaddr ( ( char * ) &addr, sizeof ( addr ), AF_INET );

                  if ( OurHost != NULL )

                  {

                        printf ( "\nNo  %s", OurHost->h_name );

                        printf ( "\nAddress %i", OurHost->h_addr_list[ 0 ] );

                        printf ( "Ip Address %s", inet_ntoa ( *( struct in_addr * ) OurHost->h_addr_list[ 0 ] ) );

                  }

                  else

                  {

                        printf ( "\nGiven Host Not Found" );

                  }

            }

      }

}

 

Get  Host By Name

 

#include <netdb.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <stdio.h>

main ()

{

      struct hostent *h;

      h = gethostbyname ( "achilles" );

      if ( h != NULL )

      {

            printf ( "%s", inet_ntoa ( * ( (struct in_addr * ) h->h_addr ) ) );

      }

      else

            printf ( "Not Found" );

}

 

Get Host By Address           

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/socket.h>

#include <netinet/in.h>

#include <netdb.h>

 

main ( int argc, char **argv )

{

      char              LocalHost[ 256 ], LenthTed[ 256 ];

      ulong_t                 addr;

      int                     LocalLen;

      struct hostent    *OurHost;

      LocalHost[ 0 ] = '\0';

 

                  addr = inet_addr ( argv[ 2 ] );

                  OurHost = gethostbyaddr ( ( char * ) &addr, sizeof ( addr ), AF_INET );

                  if ( OurHost != NULL )

                  {

                        printf ( "\nNo  %s", OurHost->h_name );

                        printf ( "\nAddress %i", OurHost->h_addr_list[ 0 ] );

                        printf ( "Ip Address %s", inet_ntoa ( *( struct in_addr * ) OurHost->h_addr_list[ 0 ] ) );

                  }

                  else

                  {

                        printf ( "\nGiven Host Not Found" );

                  }

}

 

Hosted by www.Geocities.ws

1