server.c
*********************************************************************/
/* server.c */
/* */
/* This program implements an echo server.
It takes the port number */
/* as a command line argument. It receives
a message on the */
/* specified port and echoes the message back
to the echo client. */
/* */
/* Usage: server */
/* */
/*********************************************************************/
#include
#include
#include
#include
#include
#include
#include "common.h"
int setupSocket(
int, SocketType );
int getConnection( int, SocketType );
void replyData( int );
void cleanExit( int );
/***********************************************************/
/* */
/* main() */
/* */
/* Our mainline. Correctly sets up the signal
handler and */
/* sockets for our program. It also calls
the procedures */
/* that provide the echo server functionality.
Before */
/* exiting, it closes all sockets. */
/* */
/***********************************************************/
void main( int argc, char *argv[] )
{
int port;
int socketDes, newSocketDes;
SocketType socketInfo;
if( argc !=2 )
{
printf( "Correct Usage:\n\tserver \n"
);
exit( 1 );
}
/* our signal handlers */
signal( SIGTERM, cleanExit );
signal( SIGINT, cleanExit );
/* setup the socket */
socketDes = setupSocket( atoi( argv[1] ),
socketInfo );
/* receive a new connection */
newSocketDes = getConnection( socketDes,
socketInfo );
/* send back any information sent to us
*/
replyData( newSocketDes );
/* clean up */
close( socketDes );
close( newSocketDes );
exit( 0 );
}
TOP
/*****************************************************/
/* */
/* cleanExit() */
/* */
/* Cleanly exits the program. */
/* */
/*****************************************************/
void cleanExit( int val )
{
exit();
}
/*****************************************************/
/* */
/* replyData */
/* */
/* Reads the information from the socket
and echoes */
/* it back to the sender. */
/* */
/*****************************************************/
void replyData( int socketDes )
{
int nbytes = 0;
char messageLength;
char lengthRead = 0;
char lenResponse;
char message[MAXBUF] = "";
char returnMessage[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 */
/* if you wish to test what happens if we
don't read the entire */
/* message. Replace MAXBUF with 1. */
nbytes = read( socketDes, message, MAXBUF
);
if( nbytes == -1 )
{
perror( "Error reading socket"
);
exit( 1 );
}
message[ nbytes ] = '\0';
strcat( returnMessage, message );
lengthRead += nbytes;
}
printf( "Message received: %s\n",
returnMessage );
/* return the string */
nbytes = write( socketDes, &lengthRead,
1 );
if( nbytes == -1 )
{
perror( "Error writing socket"
);
exit( 1 );
}
nbytes = write( socketDes, returnMessage,
lengthRead );
if( nbytes == -1 )
{
perror( "Error writing socket"
);
exit( 1 );
}
}
/*****************************************************/
/* */
/* getConnection() */
/* */
/* Listen for a connection from a created
socket. */
/* Accept the connection and return the
result. */
/* */
/*****************************************************/
int getConnection( int socketDes, SocketType
socketInfo )
{
int length;
int newSocket;
int result;
/* set up the socket for listening */
result = listen( socketDes, 1 );
if( result == -1 )
{
perror( "Error in listening to socket"
);
exit( 1 );
}
/* accept the connection */
length = sizeof( socketInfo );
newSocket = accept( socketDes, (struct sockaddr
*)&socketInfo, &length );
if( newSocket == -1 )
{
perror( "Accept Failed" );
exit( 1 );
}
/* return the socket number of the connection
*/
return newSocket;
}
TOP
/*****************************************************/
/* */
/* setupSocket */
/* */
/* Setup a socket for the server. Correctly
fills */
/* the sock_addr data structure and creates
the */
/* socket. */
/* */
/*****************************************************/
int setupSocket( int port, SocketType socketInfo
)
{
int socketDes;
int result;
/* setup the sock_addr data structure */
socketInfo.sin_family = AF_INET;
socketInfo.sin_port = htons( port );
socketInfo.sin_addr.s_addr = htonl( INADDR_ANY
);
/* create the new socket */
socketDes = socket( AF_INET, SOCK_STREAM,
0 );
if( socketDes == -1 )
{
perror( "Error in socket creation"
);
exit( 1 );
}
/* bind the socket */
result = bind( socketDes, (struct sockaddr
*)&socketInfo, sizeof( socketInfo )
);
if( result == -1 )
{
perror( "Error in binding socket"
);
exit( 1 );
}
/* return the new socket descriptor */
return socketDes;
}
*******************************************************************
Common.h
#ifndef COMMON
#define COMMON
#define MAXBUF 255 /* maximum buffer size
for reading and writing messages */
typedef struct sockaddr_in SocketType;
#endif
TEST
#!/bin/sh
./server 3001 &
./client localhost 3001 "This is a
test"
|