Laynetworks  
Web laynetworks.com Google
Home | Site Map | Tell a friends
Management Tutorials
Download
Tutorials
History
Computer Science
Networking
OS - Linux and Unix
Source Code
Script & Languages
Protocols
Glossary
IGNOU
Quiz
About Us
Contact Us
Feedback
 
Sign up for our Email Newsletter
 
Get Paid for Your Tech Turorials / Tips

 

 

Home > Networking > TCP
Page : 1 2 3 4 5 6 7 8 9 10 11 12 13
TCP
BackNext >

Makefile
all: server client
client: client.c
gcc client.c -o client -lsocket -lnsl
server: server.c
gcc server.c -o server -lsocket -lnsl
clean:
rm client server
client.c:
server.c:

client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include "common.h"


void sendMessage( int, char * );
int connectSocket( int, char * );
void cleanExit( int );
void receiveMessage( int );

/****************************************************/
/* */
/* main() */
/* */
/* Our mainline. This function sets up signal */
/* handlers, and calls the procedures that send */
/* and receive messages from the echo server. */
/* */
/****************************************************/

void main( int argc, char *argv[] )
{
int socketDes;
if( argc != 4 ){
printf( "Correct Usage:\n\tclient \n" );
exit( 1 );
}
signal( SIGTERM, cleanExit );
signal( SIGINT, cleanExit );
socketDes = connectSocket( atoi( argv[ 2 ] ), argv[ 1 ] );
sendMessage( socketDes, argv[ 3 ] );
receiveMessage( socketDes );
close( socketDes );
exit( 0 );
}

/*****************************************************/
/* */
/* cleanExit() */
/* */
/* Cleanly exits the program. */
/* */
/*****************************************************/


void cleanExit( int val )
{
exit( 0 );
}

/***********************************************************/
/* */
/* connectSocket() */
/* */
/* Make the connection to the echo server. Return the */
/* newly formed socket desciptor. */
/* */
/***********************************************************/

TOP

int connectSocket( int port, char* server )
{
struct hostent* hp;
SocketType socketInfo;
int socketDes;
int result;
/* create the socket */
socketDes = socket( AF_INET, SOCK_STREAM, 0 );
/* fill in port info */
socketInfo.sin_port = htons( port );
/* resolv the hostname */
hp = (struct hostent *)gethostbyname( (const char *)server );
if( !hp )
{
perror( "Error resolving hostname" );
exit( 1 );
}
/* fill in the socket information */
bcopy( (char *)hp->h_addr, (char *)&socketInfo.sin_addr, hp->h_length );
socketInfo.sin_family = hp->h_addrtype;
/* make the connection */
result = connect( socketDes, (struct sockaddr *)&socketInfo, sizeof( socketInfo ) );<![endif]>

if( result == -1 )
{
perror( "Error connecting to server" );
exit( 1 );
}<![endif]>

/* return the new socket descriptor */
return socketDes;
}
/**************************************************************/
/* */
/* sendMessage() */
/* */
/* Send a message from the echo server. */
/* */
/**************************************************************/

void sendMessage( int socketDes, char *message )
{
int result;
char length;
length = strlen( message );
/* send the length of the message */
result = write( socketDes, &length, 1 );
if( result == -1 )
{
perror( "Error writing to server" );
exit( 1 );
}<![endif]>

/* send the rest of the message */
result = write( socketDes, message, length );
if( result == -1 )
{
perror( "Error writing to server" );
exit( 1 );
}
}

TOP


/**************************************************************/
/* */
/* receiveMessage() */
/* */
/* Receive a message from the echo server. */
/* */
/**************************************************************/

void receiveMessage( int socketDes )
{
int nbytes;
char messageLength, lengthRead = 0;
char message[ MAXBUF ];
char printMessage[ MAXBUF ];
/* get the length of the message */
nbytes = read( socketDes, &messageLength, 1 );
if( nbytes == -1 )
{
perror( "Error reading socket" );
exit( 1 );
}

/* this is to assure we got the entire message */
while( lengthRead < messageLength )
{
/* read the information */
nbytes = read( socketDes, message, MAXBUF );
if( nbytes == -1 )
{
perror( "Error reading socket" );
exit( 1 );
}
/* tack it onto a string */
message[ nbytes ] = '\0';
strcat( printMessage, message );
lengthRead += nbytes;
}

/* print the result */
printf( "Message received: %s\n", printMessage );
}

TOP

 
Page : 1 2 3 4 5 6 7 8 9 10 11 12 13
 
Donation | Useful links | Link to Laynetworks.com | Legal | SharePoint Development
Copyright © 2000-2010 Lay Networks All rights reserved.