Subject: Re: Fw: Please review my Sockets() code, anyone?
From: [email-masked] (Barney Dalton)

13 Mar 1998 14:42:14 -0800
Message-ID: <[email-masked]>
References: <00a901bd4c36$93d50c80$299f23cb@markread>
Reply-To: [email-masked]
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
To: Mark Read <[email-masked]>
Cc: [email-masked], [email-masked]

on the serve side you need to use the 'ns' socket handle returned by
accept not the original 's' handle. 's' is till listening for other
clients, while 'ns' is a handle to client that just connected.

barney

On Tue, 10 Mar 1998, Mark Read wrote:

> >  // accept a connection if one in the queue
> >  len = sizeof(struct sockaddr_in);
> >  printf("\n  calling accept()...");
> >  if ((ns = accept(s, (struct sockaddr *) &address, &len)) < 0)
> >  {
> >   printf("failed");
> >   break;
> >      }
> >  else
> >  {
> >   printf("ok");
> >  }
> >
> >  n = recv(s, buf, MAXBUFSIZE, 0);
> >         // **********************************************
> >  // this works (doesn't crash system)
> >  // but nothing is rcvd (well, nothing is printed)
> >  while((n = recv(s, buf, MAXBUFSIZE, 0)) > 0)
> >  {
> >   //write(1, buf, n);
> >   printf("\n  s=%d", s);
> >   printf("\n  n=%d", n);
> >   printf("\n  buf=[%s]", buf);
> >   //printf("%d bytes rcvd", n);
> >  }
> >         // **********************************************
> >
> >
> >
> >  //printf("\n-----------------");
> >
> >  // send a reply
> >  printf("\n\n  sending stuff to caller...");
> >  n = send(s, "hello", 5, 0);
> >  printf("\n  n = %d", n);
> >
> >  printf("\n  closing call...");
> >  close(ns);
> >
> >  //printf("\n  sleeping...");
> >  //sleep(1000);
> >  //printf("back from sleeping !!");
> >  if (bEnd == 1) break;
> >  printf("\n\nlistening for a caller on port %d (press 'ctrl-c' to
> >quit)...", PORTNO);
> >}
> >
> >printf("\nclosing socket...");
> >close(s);
> >  exit(0);
> >}
> >
> >
> >// =====================================================
> >// If an op sys signal is rcvd (like ctrl-c) then abort.
> >// =====================================================
> >void handler(int sig)
> >{
> >bEnd = 1;
> >}
> >
> >

/**********************************************************/
/ Barney Dalton                                            /
/ Computer support  [email-masked]                /
/ Personal email    [email-masked]             /
/ Work Phone        (61) (8) 93803058 or 93803051          /            
/ Twist my arm :    http://telerobot.mech.uwa.edu.au       /
/**********************************************************/

-
For help on using this list (especially unsubscribing), send a message to
"[email-masked]" with one line of text: "help".