All pastes #193019 Raw Edit

Miscellany

public c v1 · immutable
·published 2006-10-06 14:21 UTC
rendered paste body
#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <netdb.h>#include <unistd.h>#include <signal.h>#include <stdio.h>#include <fcntl.h>#include <errno.h>#include <sys/time.h>#include <stdlib.h>#include <memory.h>#define BACKLOG 5#define RECVBUFSIZE 64int main( int argc, char *argv[] ){   /* client and server sockets and addresses */   struct sockaddr_in serverAddr;   struct sockaddr_in clientAddr;   unsigned int clientAddrLen;   int portNum, j, numLetter, numNull, numBytesRcvd, k, numTimes, i, randomNum, endingChar, correctLetter, lettersUsed, clientSocket, serverSocket;   lettersUsed=0;   numLetter=0;   numNull=0;   endingChar=0;   randomNum=3;   j=0;   /* buffer into which number received from client is stored */   char SendChar[100];   /* character used to store each byte received from client */   char c;   //send to the client   unsigned int CharLen;   /* buffer in which the incremented number is stored */   char recvBuffer[RECVBUFSIZE];   char allWords[10][10];   char finishedWord[10];   int numToFinish[10];     numToFinish[0]=5;   numToFinish[1]=5;   numToFinish[2]=5;   numToFinish[3]=5;   numToFinish[4]=5;   numToFinish[5]=5;   numToFinish[6]=5;   numToFinish[7]=5;   numToFinish[8]=5;   numToFinish[9]=5;   allWords[3][0]='b';   allWords[3][1]='a';   allWords[3][2]='c';   allWords[3][3]='o';   allWords[3][4]='n';   //array the word will be stored in   char theWord[10];   i=0;   while (i<numToFinish[3]) {		theWord[i]=allWords[3][i];		i++;   }   theWord[i+1]='\0';   printf("The word: %s\n", theWord);   /* number of bytes to send to client in order to send the incremented number */   unsigned int charLen;   if ( argc != 2 )      {         fprintf( stderr, "usage: <port>\n" );         exit( 0 );      }   //define port   portNum=atoi(argv[1]);   /* create server side socket */   serverSocket = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP );   if ( serverSocket < 0 )      {         perror( "socket error" );         exit( 1 );      }   printf( "Socket created.\n" );   /* zer out socket address */   bzero( &serverAddr, sizeof( serverAddr ) );   /* set socket address to server address and port. Note that the constant      INADDR_ANY implies that the client may reach the server through any       incoming interface. */   serverAddr.sin_family = AF_INET;   serverAddr.sin_addr.s_addr = htonl( INADDR_ANY );   serverAddr.sin_port = htons( portNum );   /* bind server address and port to server side socket */   if ( bind( serverSocket, (struct sockaddr *) &serverAddr, sizeof( serverAddr ) ) < 0 )      {         perror( "bind() error" );         exit( 1 );      }   printf( "Address binding successful.\n" );   /* call listen on the server side socket */   if ( listen( serverSocket, BACKLOG ) < 0 )      {         perror( "listen() error" );         exit( 1 );      }   printf( "Server process is listening for connection requests.\n" );   /* Now, prepare to accept connection requests from clients */   clientAddrLen = sizeof( clientAddr );         clientSocket = accept( serverSocket, (struct sockaddr *) &clientAddr, &clientAddrLen );         if ( clientSocket < 0 )            {               perror( "accept() error" );               exit( 1 );            }		SendChar[0]='\0';   printf( "In front of main loop.");   /* You run the for loop forever since as soon as the server serves a client,      it should get ready to serve another client. */   while ((lettersUsed<6) && (SendChar[0] != 'W')) {	   printf( "In main loop.");  	   	     correctLetter=0;		 k=0;		 SendChar[k]='\0';		 numTimes=0;		 numNull=0;         /* Begin receiving bytes from the client */         numBytesRcvd = 0;         c = ' ';         while ( c != '\0' ) {				printf( "In read loop.");               if ( read( clientSocket, &c, 1 ) == 1 )                  {                     recvBuffer[0] = c;                     numBytesRcvd++;                  }               else                  {                     perror( "read() error" );                     exit( 1 );                  }            }		 printf( "Got through that printf!");		 i=0;		 while (theWord[i]!='\0') {			 printf("Reached inner loop for the %i time.", i);			 if (recvBuffer[numLetter] == theWord[i]) {					correctLetter=1;					SendChar[k]='Y';					k++;					SendChar[k]='\0';					k++;					finishedWord[j]=theWord[i];					j++;			 }			 i++;		 }		 printf("Broke inner loop.");		 if (correctLetter!=1) { 			 SendChar[k]='N';			 SendChar[k+1]='\0';			 lettersUsed=lettersUsed+1;		 }		 i=0;		 while (theWord[i]!='\0') {			 if (theWord[i]==finishedWord[i]) {				 numNull++;			 }		 }		 printf( "NumNull: %i\n", numNull);		 if (numNull==numToFinish[3]) {			 SendChar[0]='W';			 SendChar[1]='\0';		 }         /* number of bytes to send to client is one more than the number of             digits in the incremented number since we also count the delimiter '\0' */         CharLen=strlen(SendChar)+1;         /* send incremented number to client */         if ( write( clientSocket, SendChar, CharLen ) != CharLen ) {               perror( "send error" );               exit( 1 );		 }         printf( "Returned %s to the client.\n", SendChar );            }   printf( "Got through.. everything.");   close( clientSocket );}