Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 23 Oct 2000 19:10:59 -0500
From:      "TAZ Gravel, Emmanuel" <EGravel@taz.telusa.com>
To:        "'freebsd-net@freebsd.org'" <freebsd-net@freebsd.org>
Subject:   Socket programming, strange recv reaction
Message-ID:  <6BFFC6F3FB6AD211A9D800A0C99B3E6F01B3DD70@TEAPHX0031>

next in thread | raw e-mail | index | archive | help

[-- Attachment #1 --]
I'm trying to write a small client-server pair using TCP sockets.
I'm not changing the default blocking mechanisms for recv().
The client connects to the server, which sends it a welcome
message. Then, the client sends ASCII "commands" that are
interpreted by the server (recv'ed, strcmp'ed and answered to).
If the command is "unknown" it echoes it back to the client
using send(). The client, on the other end, is "waiting" with a
recv().

Appart from the first message sent by the client, most others
were never echoed back to the client, and when one arrived, it
was from a previous message, anywhere between 2 and 8 iterations
previous to the one that was just sent. Using ethereal to analyze
the traffic, and blocking the server with a 5 second sleep, showed
that the recv in the client was accepting a simple ACK message
as an acceptable message. The "conversation" is all [PSH, ACK]
or [ACK]. Ethereal was set to look at the loopback interface to
see this happening, and for some reason all packets were
"duplicated" (same time frame, same exact packet, always in
pairs). Don't know if this has anything to do with it or not
though.

I know the problem centers around the recv in the client, however
I don't know where to look. Just starting to look at socket
programming, and using tutorials and newbie code found online
(using Beej's Guide to Network Programming right now).

Read the man page for recv() also, and since it's supposed to
be blocking until something is recieved, I don't know what to
make of this.

Inlining my code since I'm using Outlook right now...

Thanks for your help!

Emmanuel

<server code>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>

#define MYPORT 4039    /* the port users will be connecting to */

#define MAXBUFLEN 128

#define BACKLOG 10     /* how many pending connections queue will hold */
#define MAXCHILD 5

/* Global variables */

int sockfd; /* listen on sock_fd */
int child[MAXCHILD];


void usage(newfd)
     int newfd;
{
  char *msg;
  int numbytes;
  printf("Sending usage information.\n");
  msg = "Usage:\n\thello: returns a message.\n\tquit: exits the session.\n";
  if ((numbytes = send(newfd, msg, MAXBUFLEN, 0)) == -1) {
    perror("send");
    exit(1);    
  }
  printf("Sent [%i] bytes.\nMessage sent is %s\n",numbytes,msg);
}

void hello(newfd,buf)
     int newfd;
     char *buf;
{
  char msg[128] = "You said: ";
  int numbytes;
  printf("Entering hello \n");
  printf("Replying to [%s].\n",buf);
  strcat(msg,buf);
  printf("first strcat\n");
  strcat(msg,"\n");
  printf("sending message now\n");
  sleep(5);
  if ((numbytes = send(newfd, msg, MAXBUFLEN, 0)) == -1) {
    perror("send");
    close(newfd);
    exit(1);
  }
  printf("Sent [%i] bytes.\nMessage sent is [%s]\n",numbytes,msg);
}

void bye(newfd)
     int newfd;
{
  /* signal(SIGINT, finalize); */
  /* Only usefull in programs not exiting after signal trap */
  char *msg;
  msg = "Closing connection. Goodbye!\n";
  printf("in bye\n");
  if (send(newfd, msg, sizeof(msg), 0) == -1) {
    printf("error in sending\n");
    perror("send");
    close(newfd);
    exit(1);
  }
  printf("closing now\n");
  close(newfd);
  printf("Closing connection. Child PID is [%i]. \n",getpid());
  exit(0);
}

void finalize()
{  
  printf("Closing all connections\n");
  while(waitpid(-1,NULL,0) > 0); /* clean up all child processes */
  close(sockfd);
  exit(0);
}


int main()
{
    int new_fd;  /* new connection on new_fd */
    int sin_size;
    struct sockaddr_in my_addr;    /* my address information */
    struct sockaddr_in their_addr; /* connector's address information */
    int numberbytes;
    /*    int i, addre_len; */
    char buf[MAXBUFLEN];
    unsigned short int children;

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
        perror("socket");
        exit(1);
    }

    signal(SIGINT, finalize);

    my_addr.sin_family = AF_INET;         /* host byte order */
    my_addr.sin_port = htons(MYPORT);     /* short, network byte order */
    my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP
*/
    bzero(&(my_addr.sin_zero), 8);        /* zero the rest of the struct */

    if (bind(sockfd, (struct sockaddr *)&my_addr,
             sizeof(struct sockaddr)) == -1) {
        perror("bind");
        exit(1);
    }

    if (listen(sockfd, BACKLOG) == -1) {
        perror("listen");
        exit(1);
    }

    children = 0;

    while(1) {  /* main accept() loop */
        sin_size = sizeof(struct sockaddr_in);
	
	if (children < MAXCHILD) {
	  if ((new_fd = accept(sockfd,
                               (struct sockaddr *)&their_addr,
                               &sin_size)) == -1) {
	    perror("accept");
	    continue;
	  }
	  printf("server: got connection from [%s]\n",
                  inet_ntoa(their_addr.sin_addr));
	  if (!fork()) { /* this is the child process */
	    /* Anything that happens here is only executed by the child,
	     * and that's the only thing that the child executes. */
            if (send(new_fd,
                     "Welcome to my world!\nWhat is your pleasure?\n",
                     44, 0) == -1) {
	      perror("send");
	      close(new_fd);
	      exit(1);
	    }
	    
	    while(1) {
	      
	      if ((numberbytes = recv(new_fd,buf,MAXBUFLEN,0)) == -1) {
		perror("recv");
		continue;
	      }
	      
	      buf[numberbytes] = '\0';

	      printf("numberbytes = [%i]\nbuffer = [%s]\n",numberbytes,buf);
	      
	      if(!strcmp(buf, "")) {
		printf("printing usage\n");
		usage(new_fd);
	      }
	      else if(!strncmp(buf, "quit", 4)) {
		printf("calling bye\n");
		bye(new_fd);
	      }
	      else {
		printf("calling hello\n");
		hello(new_fd,buf);
	      }
	    }
	  }
	}
        /* clean up all child processes */
        while(waitpid(-1,NULL,WNOHANG) > 0);
    }
}

<client code>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>

#define PORT 4039    /* the port client will be connecting to */

#define MAXDATASIZE 100 /* max number of bytes we can get at once */

int main(int argc, char *argv[])
{
  int sockfd, numbytes;  
  char buf[MAXDATASIZE],msg[MAXDATASIZE] = "sta";
  struct hostent *he;
  struct sockaddr_in their_addr; /* connector's address information */

  /*  printf("Before init\n");
   */
  if (argc != 2) {
    fprintf(stderr,"usage: manuclient hostname\n"); /* put actual error
message here */
    exit(1);
  }
  
  /*printf("Before gethostbyname\n");
   */
  if ((he=gethostbyname(argv[1])) == NULL) {  /* get the host info */
    perror("gethostbyname");
    exit(1);
  }
  
  /*printf("Host is %s\nBeofre socket creation\n", *((struct in_addr
*)he->h_addr));
   */
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("socket");
    exit(1);
  }
  
  /* printf("Socket created. Before socket connection.\n");
   */
  their_addr.sin_family = AF_INET;         /* host byte order */
  their_addr.sin_port = htons(PORT);     /* short, network byte order */
  their_addr.sin_addr = *((struct in_addr *)he->h_addr);
  bzero(&(their_addr.sin_zero), 8);        /* zero the rest of the struct */

  if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct
sockaddr)) == -1) {
    perror("connect");
    exit(1);
  }
  
  /*printf("Socket connected. Before while loop, value is
%i\n",strcmp(msg,"quit"));
   */
  while(strcmp(msg,"quit")) {   /* Begin chat routine with the server */

    printf("Before recv\n");
    /*sleep(5);*/

    if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {
      perror("recv");
      exit(1);
    }

    printf("After recv\n");

    buf[numbytes] = '\0';
    
    printf("Received: %s\n",buf);
    
    printf("Command > ");
    
    scanf("%s",msg);

    /*   if (sizeof(msg) >= MAXDATASIZE)
      msg[MAXDATASIZE] = '\0';
    */
    printf("Size of message is %i\n",sizeof(msg));

    if ((numbytes = send(sockfd, msg, MAXDATASIZE, 0)) == -1) {
      perror("send");
      exit(1);
    }
    
    printf("sent message %s to %s\nNumber of bytes sent is
%i\n",msg,inet_ntoa(their_addr.sin_addr),numbytes);
    
    if(!strcmp(msg,"quit"))
      printf("Recieved quit command, exiting!\n");
    
  }

  close(sockfd);
  
  return 0;
}

[-- Attachment #2 --]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2650.12">
<TITLE>Socket programming, strange recv reaction</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2 FACE="Arial">I'm trying to write a small client-server pair using TCP sockets.</FONT>
<BR><FONT SIZE=2 FACE="Arial">I'm not changing the default blocking mechanisms for recv().</FONT>
<BR><FONT SIZE=2 FACE="Arial">The client connects to the server, which sends it a welcome</FONT>
<BR><FONT SIZE=2 FACE="Arial">message. Then, the client sends ASCII &quot;commands&quot; that are</FONT>
<BR><FONT SIZE=2 FACE="Arial">interpreted by the server (recv'ed, strcmp'ed and answered to).</FONT>
<BR><FONT SIZE=2 FACE="Arial">If the command is &quot;unknown&quot; it echoes it back to the client</FONT>
<BR><FONT SIZE=2 FACE="Arial">using send(). The client, on the other end, is &quot;waiting&quot; with a</FONT>
<BR><FONT SIZE=2 FACE="Arial">recv().</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Appart from the first message sent by the client, most others</FONT>
<BR><FONT SIZE=2 FACE="Arial">were never echoed back to the client, and when one arrived, it</FONT>
<BR><FONT SIZE=2 FACE="Arial">was from a previous message, anywhere between 2 and 8 iterations</FONT>
<BR><FONT SIZE=2 FACE="Arial">previous to the one that was just sent. Using ethereal to analyze</FONT>
<BR><FONT SIZE=2 FACE="Arial">the traffic, and blocking the server with a 5 second sleep, showed</FONT>
<BR><FONT SIZE=2 FACE="Arial">that the recv in the client was accepting a simple ACK message</FONT>
<BR><FONT SIZE=2 FACE="Arial">as an acceptable message. The &quot;conversation&quot; is all [PSH, ACK]</FONT>
<BR><FONT SIZE=2 FACE="Arial">or [ACK]. Ethereal was set to look at the loopback interface to</FONT>
<BR><FONT SIZE=2 FACE="Arial">see this happening, and for some reason all packets were</FONT>
<BR><FONT SIZE=2 FACE="Arial">&quot;duplicated&quot; (same time frame, same exact packet, always in</FONT>
<BR><FONT SIZE=2 FACE="Arial">pairs). Don't know if this has anything to do with it or not</FONT>
<BR><FONT SIZE=2 FACE="Arial">though.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">I know the problem centers around the recv in the client, however</FONT>
<BR><FONT SIZE=2 FACE="Arial">I don't know where to look. Just starting to look at socket</FONT>
<BR><FONT SIZE=2 FACE="Arial">programming, and using tutorials and newbie code found online</FONT>
<BR><FONT SIZE=2 FACE="Arial">(using Beej's Guide to Network Programming right now).</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Read the man page for recv() also, and since it's supposed to</FONT>
<BR><FONT SIZE=2 FACE="Arial">be blocking until something is recieved, I don't know what to</FONT>
<BR><FONT SIZE=2 FACE="Arial">make of this.</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Inlining my code since I'm using Outlook right now...</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Thanks for your help!</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">Emmanuel</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&lt;server code&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;stdio.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;stdlib.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;errno.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;signal.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;string.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;unistd.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;sys/types.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;netinet/in.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;sys/socket.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;sys/wait.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;arpa/inet.h&gt;</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">#define MYPORT 4039&nbsp;&nbsp;&nbsp; /* the port users will be connecting to */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">#define MAXBUFLEN 128</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">#define BACKLOG 10&nbsp;&nbsp;&nbsp;&nbsp; /* how many pending connections queue will hold */</FONT>
<BR><FONT SIZE=2 FACE="Arial">#define MAXCHILD 5</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">/* Global variables */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">int sockfd; /* listen on sock_fd */</FONT>
<BR><FONT SIZE=2 FACE="Arial">int child[MAXCHILD];</FONT>
</P>
<BR>

<P><FONT SIZE=2 FACE="Arial">void usage(newfd)</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp; int newfd;</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; char *msg;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; int numbytes;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Sending usage information.\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; msg = &quot;Usage:\n\thello: returns a message.\n\tquit: exits the session.\n&quot;;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; if ((numbytes = send(newfd, msg, MAXBUFLEN, 0)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;send&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Sent [%i] bytes.\nMessage sent is %s\n&quot;,numbytes,msg);</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">void hello(newfd,buf)</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp; int newfd;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp; char *buf;</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; char msg[128] = &quot;You said: &quot;;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; int numbytes;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Entering hello \n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Replying to [%s].\n&quot;,buf);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; strcat(msg,buf);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;first strcat\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; strcat(msg,&quot;\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;sending message now\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; sleep(5);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; if ((numbytes = send(newfd, msg, MAXBUFLEN, 0)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;send&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; close(newfd);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Sent [%i] bytes.\nMessage sent is [%s]\n&quot;,numbytes,msg);</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">void bye(newfd)</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp; int newfd;</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; /* signal(SIGINT, finalize); */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; /* Only usefull in programs not exiting after signal trap */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; char *msg;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; msg = &quot;Closing connection. Goodbye!\n&quot;;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;in bye\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; if (send(newfd, msg, sizeof(msg), 0) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;error in sending\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;send&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; close(newfd);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;closing now\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; close(newfd);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Closing connection. Child PID is [%i]. \n&quot;,getpid());</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; exit(0);</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">void finalize()</FONT>
<BR><FONT SIZE=2 FACE="Arial">{&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;Closing all connections\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; while(waitpid(-1,NULL,0) &gt; 0); /* clean up all child processes */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; close(sockfd);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; exit(0);</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>
<BR>

<P><FONT SIZE=2 FACE="Arial">int main()</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; int new_fd;&nbsp; /* new connection on new_fd */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; int sin_size;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; struct sockaddr_in my_addr;&nbsp;&nbsp;&nbsp; /* my address information */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; struct sockaddr_in their_addr; /* connector's address information */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; int numberbytes;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; /*&nbsp;&nbsp;&nbsp; int i, addre_len; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; char buf[MAXBUFLEN];</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; unsigned short int children;</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&quot;socket&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; signal(SIGINT, finalize);</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; my_addr.sin_family = AF_INET;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* host byte order */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; my_addr.sin_port = htons(MYPORT);&nbsp;&nbsp;&nbsp;&nbsp; /* short, network byte order */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; my_addr.sin_addr.s_addr = INADDR_ANY; /* automatically fill with my IP */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; bzero(&amp;(my_addr.sin_zero), 8);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* zero the rest of the struct */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; if (bind(sockfd, (struct sockaddr *)&amp;my_addr,</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sizeof(struct sockaddr)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&quot;bind&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; if (listen(sockfd, BACKLOG) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&quot;listen&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; children = 0;</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; while(1) {&nbsp; /* main accept() loop */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sin_size = sizeof(struct sockaddr_in);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">if (children &lt; MAXCHILD) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp; if ((new_fd = accept(sockfd,</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (struct sockaddr *)&amp;their_addr,</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;sin_size)) == -1) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;accept&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; continue;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp; printf(&quot;server: got connection from [%s]\n&quot;,</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; inet_ntoa(their_addr.sin_addr));</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp; if (!fork()) { /* this is the child process */</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; /* Anything that happens here is only executed by the child,</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp; * and that's the only thing that the child executes. */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (send(new_fd,</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &quot;Welcome to my world!\nWhat is your pleasure?\n&quot;,</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 44, 0) == -1) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&quot;send&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; close(new_fd);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; while(1) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ((numberbytes = recv(new_fd,buf,MAXBUFLEN,0)) == -1) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">perror(&quot;recv&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">continue;</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; buf[numberbytes] = '\0';</FONT>
</P>

<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;numberbytes = [%i]\nbuffer = [%s]\n&quot;,numberbytes,buf);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(!strcmp(buf, &quot;&quot;)) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">printf(&quot;printing usage\n&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">usage(new_fd);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if(!strncmp(buf, &quot;quit&quot;, 4)) {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">printf(&quot;calling bye\n&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">bye(new_fd);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else {</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">printf(&quot;calling hello\n&quot;);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">hello(new_fd,buf);</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT SIZE=2 FACE="Arial">}</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* clean up all child processes */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(waitpid(-1,NULL,WNOHANG) &gt; 0);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&lt;client code&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;stdio.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;stdlib.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;errno.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;string.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;netdb.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;unistd.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;sys/types.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;netinet/in.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;sys/socket.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;sys/wait.h&gt;</FONT>
<BR><FONT SIZE=2 FACE="Arial">#include &lt;arpa/inet.h&gt;</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">#define PORT 4039&nbsp;&nbsp;&nbsp; /* the port client will be connecting to */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">#define MAXDATASIZE 100 /* max number of bytes we can get at once */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">int main(int argc, char *argv[])</FONT>
<BR><FONT SIZE=2 FACE="Arial">{</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; int sockfd, numbytes;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; char buf[MAXDATASIZE],msg[MAXDATASIZE] = &quot;sta&quot;;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; struct hostent *he;</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; struct sockaddr_in their_addr; /* connector's address information */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp; /*&nbsp; printf(&quot;Before init\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; if (argc != 2) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; fprintf(stderr,&quot;usage: manuclient hostname\n&quot;); /* put actual error message here */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; /*printf(&quot;Before gethostbyname\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; if ((he=gethostbyname(argv[1])) == NULL) {&nbsp; /* get the host info */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;gethostbyname&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; /*printf(&quot;Host is %s\nBeofre socket creation\n&quot;, *((struct in_addr *)he-&gt;h_addr));</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;socket&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; /* printf(&quot;Socket created. Before socket connection.\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; their_addr.sin_family = AF_INET;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* host byte order */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; their_addr.sin_port = htons(PORT);&nbsp;&nbsp;&nbsp;&nbsp; /* short, network byte order */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; their_addr.sin_addr = *((struct in_addr *)he-&gt;h_addr);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; bzero(&amp;(their_addr.sin_zero), 8);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /* zero the rest of the struct */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp; if (connect(sockfd, (struct sockaddr *)&amp;their_addr, sizeof(struct sockaddr)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; perror(&quot;connect&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; /*printf(&quot;Socket connected. Before while loop, value is %i\n&quot;,strcmp(msg,&quot;quit&quot;));</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; while(strcmp(msg,&quot;quit&quot;)) {&nbsp;&nbsp; /* Begin chat routine with the server */</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;Before recv\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; /*sleep(5);*/</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&quot;recv&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;After recv\n&quot;);</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; buf[numbytes] = '\0';</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;Received: %s\n&quot;,buf);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;Command &gt; &quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; scanf(&quot;%s&quot;,msg);</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; /*&nbsp;&nbsp; if (sizeof(msg) &gt;= MAXDATASIZE)</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg[MAXDATASIZE] = '\0';</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; */</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;Size of message is %i\n&quot;,sizeof(msg));</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; if ((numbytes = send(sockfd, msg, MAXDATASIZE, 0)) == -1) {</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(&quot;send&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(1);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; }</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; printf(&quot;sent message %s to %s\nNumber of bytes sent is %i\n&quot;,msg,inet_ntoa(their_addr.sin_addr),numbytes);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; if(!strcmp(msg,&quot;quit&quot;))</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Recieved quit command, exiting!\n&quot;);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp;&nbsp;&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; }</FONT>
</P>

<P><FONT SIZE=2 FACE="Arial">&nbsp; close(sockfd);</FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; </FONT>
<BR><FONT SIZE=2 FACE="Arial">&nbsp; return 0;</FONT>
<BR><FONT SIZE=2 FACE="Arial">}</FONT>
</P>

</BODY>
</HTML>

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?6BFFC6F3FB6AD211A9D800A0C99B3E6F01B3DD70>