views:

119

answers:

2

How can my client send two queries (in two different terminals) to the server at the same time? When i try it, only one works, the other closes socket.

main  ()
  {

    readData ();
    int serverFd, clientFd, clientFd2,serverLen, clientLen;
    struct sockaddr_un serverAddress;/* Server address */
    struct sockaddr_un clientAddress; /* Client address */
    struct sockaddr* serverSockAddrPtr; /* Ptr to server address */
    struct sockaddr* clientSockAddrPtr; /* Ptr to client address */

    /* Ignore death-of-child signals to prevent zombies */
    signal (SIGCHLD, SIG_IGN);

    serverSockAddrPtr = (struct sockaddr*) &serverAddress;
    serverLen = sizeof (serverAddress);

    clientSockAddrPtr = (struct sockaddr*) &clientAddress;
    clientLen = sizeof (clientAddress);

    /* Create a socket, bidirectional, default protocol */
    serverFd = socket (AF_LOCAL, SOCK_STREAM, DEFAULT_PROTOCOL);
    serverAddress.sun_family = AF_LOCAL; /* Set domain type */
    strcpy (serverAddress.sun_path, "countries"); /* Set name */
    unlink ("countries"); /* Remove file if it already exists */
    bind (serverFd, serverSockAddrPtr, serverLen); /* Create file */
    listen (serverFd, 5); /* Maximum pending connection length */

    while (1) /* Loop forever */
      {
        /* Accept a client connection */
        clientFd = accept (serverFd, clientSockAddrPtr, &clientLen);


        while (fork () == 0) /* Create child to send recipe */
          {
int recvquery;
char countrynamereceivedquery[200];
while (recvquery=read(clientFd,countrynamereceivedquery,sizeof(countrynamereceivedquery)))
{
//print results

}
}

Thats my server program. I run it as a background process and then run client program which can search the textfile stored in an array in server. Right now, when i open two terminals and run the client at teh same time, one client quits program, the other client receives the jus-quit-client's query and searches the server. I did create two sockets but the client just quits in both terminals.

A: 

Assuming the server allows more than one connection at a time you can create a different socket and use it for opening another connection to the server.

smichak
My server just creates a socket and waits for clients request for connection. How can it allow more than one connection?
svenus
After the server returns from accept you can run call accept again in order to accept another connection.
smichak
Here's an example for a server that accepts multiple connections and handles each connection on a separate thread : http://www.cs.cf.ac.uk/Dave/C/node32.html#SECTION003270000000000000000
smichak
After the server accepts connection, i am creating a child process to handle the client query.Do you mean accept another connection in the child process?
svenus
No - do it in the parent process. Use the child processes for handling each connection and the parent process for accepting incoming connections in a loop.
smichak
A: 
  1. You use the function socket() to create a TCP socket.

  2. You assign a port number with bind() to the socket.

  3. Using listen() the system allows connections to that port.

  4. Repeat the following:

    a. accept() gets a new socket for each client that is connected.

    b. With send() and recv() you communicate with the client via that new socket.

    c. Finally you close the client connection with function close().

Novemberland