🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.wizards
3 messages
3 total messages Started by ARIT ISMAIL Tue, 26 Jan 1993 22:21
client/server question...........
#6954
Author: ARIT ISMAIL
Date: Tue, 26 Jan 1993 22:21
46 lines
1930 bytes
Hi,

I don't know if this is the right place to post this, but
I need some help about writing a simple client/server program.

Here is what I am trying to do;

I want to write a program that will accept connections from
other programs and will communicate with them without forking.
I don't want to fork because the info should be generated on one
program, send to server and the server has to respond to every
program connected with the same info( if I fork, how  can I transfer
that info to child?).

  main                     main
  client <--> server <--> client
                |
                |
         _______________________________
         |         |        |        |
         v         v        v        v
       client1  client2  client3     ......

(main clients generate the info)
I know this is not an easy thing to do, but actually, I am having
problems with accept(..);
can I do something like this;

while(1)
{
 check_new_connection(); /* how can I check if there is any new
connection
                           without waiting, as far as I know accept(..)
                           blocks you 'till you get some connection*/
 check_clients();       /* I can do these, no problem. read if there is
                           any new info */
 if(thereIsNewInfo)
   inform_clients();     /* if I can create a list of handles for clients
                           this is no big deal */

}
My problem is getting new connections without waiting.

I appreciate any help.

iarit@slate.mines.colorado.edu
Re: client/server question...........
#6950
Author: 0000-Admin(0000)
Date: Wed, 27 Jan 1993 16:21
57 lines
2845 bytes
In article <1993Jan26.222115.28187@slate.mines.colorado.edu>, iarit@slate.mines.colorado.edu (ARIT ISMAIL) writes:
|> Hi,
|> 
|> I don't know if this is the right place to post this, but
|> I need some help about writing a simple client/server program.
|> 
|> Here is what I am trying to do;
|> 
|> I want to write a program that will accept connections from
|> other programs and will communicate with them without forking.
|> I don't want to fork because the info should be generated on one
|> program, send to server and the server has to respond to every
|> program connected with the same info( if I fork, how  can I transfer
|> that info to child?).
|> 
|>   main                     main
|>   client <--> server <--> client
|>                 |
|>                 |
|>          _______________________________
|>          |         |        |        |
|>          v         v        v        v
|>        client1  client2  client3     ......
|> 
|> (main clients generate the info)
|> I know this is not an easy thing to do, but actually, I am having
|> problems with accept(..);
|> can I do something like this;
|> 
|> while(1)
|> {
|>  check_new_connection(); /* how can I check if there is any new
|> connection
|>                            without waiting, as far as I know accept(..)
|>                            blocks you 'till you get some connection*/
|>  check_clients();       /* I can do these, no problem. read if there is
|>                            any new info */
|>  if(thereIsNewInfo)
|>    inform_clients();     /* if I can create a list of handles for clients
|>                            this is no big deal */
|> 
|> }
|> My problem is getting new connections without waiting.
	If your problem is only about having a non-blocking "accept()", then
you mark the socket as non-blocking. If you then try accept() , you will see
that accept will accept the connection, if there is a pending connection request.
On the other hand if no pending connections are there, then accept 
will return immediately with an error (I think EWOULDBLOCK).
You can use fcntl (O_NDELAY or O_NONBLOCK) for the server socket to get this
feature.

|> 
|> I appreciate any help.
|> 
|> iarit@slate.mines.colorado.edu
-raj
1/27/93
Re: client/server question...........
#6966
Author: Jeremy Gaffney
Date: Thu, 28 Jan 1993 23:08
79 lines
3169 bytes
In article <1993Jan26.222115.28187@slate.mines.colorado.edu>, iarit@slate.mines.colorado.edu (ARIT ISMAIL) writes:
|> Hi,
Hi.
|> 
|> I don't know if this is the right place to post this, but
|> I need some help about writing a simple client/server program.

Comp.unix.questions is usually a better forum for 
questions of this level.  I also highly recommend 
getting W. Richard Stevens' Unix_Network_Programming, 
which is an excellent reference with a large amount of
robust source code included.  The book is the best networking
reference I have seen.  It will be a well-spent $50.

|> 
|> Here is what I am trying to do;
|> 
|> I want to write a program that will accept connections from
|> other programs and will communicate with them without forking.
|> I don't want to fork because the info should be generated on one
|> program, send to server and the server has to respond to every
|> program connected with the same info( if I fork, how  can I transfer
|> that info to child?).

Shared memory. Sockets. Pipes.  Man pages are:

shmctl (2)              - shared memory control operations
shmget (2)              - get shared memory segment identifier
shmop, shmat, shmdt (2) - shared memory operations

for shared memory,

socket(2), connect(2), et al. for sockets, and 

pipe (2V)               - create an interprocess communication channel
popen, pclose (3S)      - open or close a pipe (for I/O) from or to a process

for pipes.  These three are common and reasonably portable. 

|> 
|> while(1)
|> {
|>  check_new_connection(); /* how can I check if there is any new
|> connection
|>                            without waiting, as far as I know accept(..)
|>                            blocks you 'till you get some connection*/
|>  check_clients();       /* I can do these, no problem. read if there is
|>                            any new info */
|>  if(thereIsNewInfo)
|>    inform_clients();     /* if I can create a list of handles for clients
|>                            this is no big deal */
|> 
|> }
|> My problem is getting new connections without waiting.

To get new connections without waiting, you can set the socket to
be non-blocking using the ioctl or fnctl calls, like so:

#include <sys/ioctl.h>
#include <errno.h>

char foo=(char) 1;

if (ioctl(sockfd, FIONBIO, &foo  ) < 0)
	printf("Error setting socket %d with code: %d", sockfd, errno);

The third argument is a char * which is expected to point
to the argument to ioctl; arg needs merely to be non-zero in
this case to set nonblocking IO for sockfd.

|> 
|> I appreciate any help.
|> 
|> iarit@slate.mines.colorado.edu

Anytime.
-jg
-- 
/*  Jeremy Gaffney [jg@cs.brown.edu gaffer@brownvm.brown.edu] */
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads