Sockets Arrrgh!

Sockets Arrrgh!

Post by Mike Dunca » Tue, 06 Jul 1999 04:00:00



I need to build a program (server) that will peridocally write data to a
port where it can be accessed by other processes (clients) on machines
both local and remote.  I've used AF_UNIX sockets locally to do this but
the AF_INET stuff is somehow evading me.   How do I set up the server
and client(s) to do this???  The use of connect, select and listen are
what's getting me confused I think.   Any insight would be immeasurably
appreciated .

Mike D.

Michael Duncan
Staff Scientist
Planning Systems Inc.

 
 
 

Sockets Arrrgh!

Post by Barry Margoli » Wed, 07 Jul 1999 04:00:00




Quote:>I need to build a program (server) that will peridocally write data to a
>port where it can be accessed by other processes (clients) on machines
>both local and remote.  I've used AF_UNIX sockets locally to do this but
>the AF_INET stuff is somehow evading me.   How do I set up the server
>and client(s) to do this???  The use of connect, select and listen are
>what's getting me confused I think.   Any insight would be immeasurably
>appreciated .

Get the book "Unix Network Programming" by W. Richard Stevens.  It should
answer your questions.  It has lots of examples.

--

GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Sockets Arrrgh!

Post by Ken Pizzi » Wed, 07 Jul 1999 04:00:00


On Mon, 05 Jul 1999 22:12:27 +0000,


>I need to build a program (server) that will peridocally write data to a
>port where it can be accessed by other processes (clients) on machines
>both local and remote.  I've used AF_UNIX sockets locally to do this but
>the AF_INET stuff is somehow evading me.   How do I set up the server
>and client(s) to do this???  The use of connect, select and listen are
>what's getting me confused I think.   Any insight would be immeasurably
>appreciated .

Add your own error-checking and services...

The server end:
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #define PORT 8000 /* or whatever you want */
 /*...*/
   fd_set f;
   struct sockaddr_in a;
   int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   listen(s, 5);
   a.sin_family = PF_INET;
   a.sin_addr.s_addr = INADDR_ANY; /* unless doing virtual hosting */
   a.sin_port = htons(PORT);
   bind(s, &a, sizeof a);
   listen(s, 5);
   /* you may skip this select if you just want to block until
      a connection is attempted: */
   FD_ZERO(&f);
   FD_SET(s, &f);
   if (select(s+1, &f, NULL, NULL, NULL) > 0) {
     int c = accept(s, &a, sizeof a);
     /* serve connection on c */
   }

The client end:
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #define PORT 8000 /* or whatever you want */
 /*...*/
   struct sockaddr_in a;
   int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
   a.sin_family = PF_INET;
   a.sin_addr.s_addr = INADDR_ANY; /* unless doing virtual hosting */
   a.sin_port = htons(PORT);
   c = connect(s, &a, sizeof a);
   /* talk with server using c */

The Richard Stevens book "Unix Network Programming" is quite good
for learning/using this kind of stuff, by the way.

                --Ken Pizzini