Вы находитесь на странице: 1из 9

Network Programming Assignment 5

Mateusz Lewandowski C11472948 C11472948@mydit.ie Computing Year 3

Report
In your opinion, how well have you completed the assignment ? My assignment follows the criteria fully and behaves exactly the same as stated in the description. When you run the server it stores the default message(no message stored). It can store the message up to 120 characters. Also, it can queue up 5 clients and communicate only with one at a time. When client connects, he gets a prompt message saying that he can send a message up to 120 characters. Then it reads the message from the server and displays it to the screen. Then, user can now either type in the message, hit enter and it gets send to the server or hit enter or C/R to exit gracefully without sending any message. The last message send is always stored on the server until its closed. In my opinion the quality of code is really good. Its easy to read and understand. I have used basic C functions like strcpy or memset which copy the string and clears it. Also, I have used the code I learned in labs. The communication between the server and client. I have commented out the whole code so that the person can have a quick look up what the block of code is doing. Have you encountered any difficulties in completing this assignment ? The only difficulty I have encountered was when I used the strcpy() function. It suppose to overwrite the string, but it only did it for the size of the string. So eg. You type in message hello, second client connects and types in hi. When third client connects the message would be hillo. I solved it by calling a memset() function which cleared the buffer string fully. Then when I copied it to the main message, everything worked perfect. Time spent working on assignment, what you feel you learned and evaluation... I have completed this assignment in 4 days. I spent from 2 to 5 hours a day. For the first 3 days I worked on code and on the last day I wrote up the report and submitted everything.
I feel I have learned how to code up a basic client server communication. How to send a message across, receive it and display it to the user.

I think the assignment was just right. It made us use the knowledge taken from the labs and use it for certain purpose. The difficulty of the assignment was right taking into account how many marks its worth.

CODE
Mateusz Lewandowski Ken OBrien Network Programming TCP Chat Client and Server 03/04/2014
I declare that this work, which is submitted as part of my coursework, is entirely my

own, except where clearly and explicitly stated. I fully understand that this is not a group assignment and accept the penalties for both late submission and plagiarism.

_____________________________________________ Mateusz Lewandowski Client code

#include <stdio.h> #include <errno.h> #include <netdb.h> #include <stdlib.h> #include <string.h> extern int errno; #define LINELEN 120 /*-----------------------------------------------------------------------* TCPecho Client - send input to ECHO service on specified host and print reply *-----------------------------------------------------------------------*/ int main( argc, argv )

int argc; char *argv[]; { char *host; /* will hold the host name */ char *service; /* will hold the port number */ char buf[ LINELEN+1 ]; /* buffer for one line of text */ char bufin [ LINELEN+1 ] ; /* buffer for reading line of test */ int s, n; /* socket descriptor, read count*/ int outchars= LINELEN, inchars=LINELEN; /* max characters sent & received

*/

if (argc != 3 ) { fprintf( stderr, "usage: tcpechoc host port \n" ); exit( 1 ); } else host = argv[1]; service = argv[2]; /*connectsock creates and connects a socket to specified host & port no. */ s = connectsock ( host, service ); while(1){ /* n is the number returned from read method if it's less than 0 then there was no message read */ n = read(s, bufin, inchars); bufin[ n ] = '\0'; if( n < 0 ) { printf("Error - couldn't get any message ! \n"); close(s); exit( 1 ); } printf("\nThe message can store up to 120 characters only !\n"); printf("Server message: %s\n", bufin); printf("Enter new server message ! \n"); /* Read input from the keyboard */ fgets( buf, sizeof( buf ), stdin ); /* Removing new line character */ buf[strlen(buf) - 1 ] = '\0';

// Sending message to the server

outchars = strlen( buf ); write(s,buf, outchars); // Client exits exit(0); } } /*-----------------------------------------------------------------------* connectsock - allocate & connect a socket using TCP *-----------------------------------------------------------------------*/ int connectsock( char *host, /* name of host to which connection is desired */ char *service /* port number as a character string */ ) { struct hostent *phe; /* pointer to data structure containing IP addre byte integer*/ struct sockaddr_in sin; /* will hold a full Internet endpoint address*/ /* full address comprises address family, port n address */ int s ; /* socket descriptor */ bzero( (char *)&sin, sizeof( sin ) ); /* initialise sin with zeros */ sin.sin_family = AF_INET; /* place address family in sin.sin_family */ /* function atoi converts port number from chara integer*/ /* htons converts from host to network byte orde /* place resultant port number into sin.sin_port if ( ( sin.sin_port = htons( (ushort)atoi( service ) ) ) == 0 ) { fprintf( stderr, "can't get \"%s\" service entry\n", service ); exit( 1 ); } /* gethostbyname converts host name to 4-byte IP address and places it in phe>h_addr */ /* bcopy copies 4-byte host IP address into sin.sin_addr */ if ( phe = gethostbyname( host ) ) bcopy( phe->h_addr, &sin.sin_addr,4 ); else { fprintf( stderr, "can't get \"%s\" host entry\n", host ); r*/ */ ss as 4-

umber and IP

cter string to

exit( 1 ); } /* Create a socket with the socket function call */ s = socket(PF_INET, SOCK_STREAM, 0); /* TCP/IP sockets can be type SOCK_STREAM (TCP) or SOCK_DGRAM (UDP) */ /* SOCK_STREAM is a symbolic constant = 1 */ /* AF_INET is a symbolic constant = 2 indicating address family TCP/IP */ /* Put socket function call in here. Let s = socket descriptor */ if ( s < 0 ) { fprintf( stderr, "can't create socket: %s\n", strerror( errno ) ); exit( 1 ); } /* Connect the socket */ if(connect(s,(struct sockaddr*)&sin,sizeof(sin)) < 0) /* put connect function call in here to connect to server , enclose it in an i statement so that if */ /* the connect call returns a value of -1 then the error handling code immedia following is executed */ { fprintf( stderr, "can't connect to %s.%s: %s\n", host, service, strerror( errno ) ); exit( 1 ); } return s; }

f tely

SERVER CODE

/* itcpechos.c - main, */ #include <netdb.h> #include <errno.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #define QLEN 5 /* maximum connection queue length #define BUFSIZE 120 extern int errno;

*/

/*-----------------------------------------------------------------------* main - Iterative TCP server for ECHO service *-----------------------------------------------------------------------*/ int main( int argc, char *argv[] ) { char *service; /* service port number */ struct sockaddr_in fsin; /* the address of a client */ int alen; /* length of client's address */ int msock; /* master server socket */ int ssock; /* slave server socket */ int ccount; char message[BUFSIZE] = "*** No messages stored ! ***"; /* Creating a string to hold the message */ char temp_msg[BUFSIZE]; /* String used for temporary message */ // Print error message if port no. is not supplied if (argc != 2 ) { fprintf( stderr, "usage: tcpechos port \n" ); exit( 1 ); } service = argv[1]; /* call passivetcp to create socket, and put it in passive listening mode */ msock = passivetcp( service, QLEN ); while ( 1 ) { alen = sizeof( fsin ); ssock = accept( msock, ( struct sockaddr * ) &fsin, &alen ); if ( ssock < 0 ) { fprintf( stderr, "accept: %s\n", strerror( errno ) ); exit( 1 ); } // Write message to the client write(ssock, message, BUFSIZE); // Make sure there is end of string character at the end message[ BUFSIZE ] = '\0'; // ccount is the number returned from the read method // it is less than 0 then there is no message to be read ccount = read(ssock, temp_msg, sizeof( temp_msg ) ); if ( ccount < 0 ) {

fprintf( stderr, "echo read: %s \n", strerror(errno ) ); exit( 1 ); } // If the string read from the client is not blank... // overwrite the temp_msg with the message that will be displayed // to the client // clear the temp string if(strcmp(temp_msg,"") != 0) { strcpy(message, temp_msg); memset(temp_msg, 0, BUFSIZE); } // close the connection ( void ) close( ssock ); } } /*-----------------------------------------------------------------------* passivetcp - allocate & bind a server socket using TCP *-----------------------------------------------------------------------*/ int passivetcp( char *service, /* port no. of the desired port */ int qlen ) /* maximum length of the server request queue */ { struct sockaddr_in sin; /* an Internet endpoint address */ int s ; /* socket descriptor */ int retcode; /* return code from function call */ /* initialise sin with 0's */ bzero( (char *)&sin, sizeof( sin ) ); /* put address family AF_INET into sin.sin_family*/ sin.sin_family = AF_INET; /* put in INADDR_ANY into IP address element of sin so socket software can use address */ sin.sin_addr.s_addr = INADDR_ANY; /* convert service (port no.) to integer */ /* atoi converts ascii string service to integer */ /* htons converts short integer from host to network byte order*/ /* put port number in sin.sin_port to complete the address in sin */ retcode = sin.sin_port = htons( (ushort)atoi( service ) ) ; if (retcode == 0) { fprintf( stderr, "can't get \"%s\" service entry\n", service ); exit( 1 );

any local IP

} /* Allocate a socket */ s = socket( PF_INET, SOCK_STREAM, 0); if ( s < 0 ) { fprintf( stderr, "can't create socket: %s\n", strerror( errno ) ); exit( 1 ); } /* Bind the socket i.e fill in the local address into it*/ if ( bind( s, (struct sockaddr *)&sin, sizeof( sin ) ) < 0 ) { fprintf( stderr, "can't bind to %s port: %s\n", service, strerror( errno ) ); exit( 1 ); } /* put the socket in passive listening mode */ /* allow up to 5 requests to be queued */ if (listen( s, qlen ) < 0 ) { fprintf( stderr, "can't listen on %s port: %s\n", service, strerror( errno ) ); exit( 1 ); } return s; }

Вам также может понравиться