Find the answer to your Linux question:
Results 1 to 3 of 3
I have this C++ code: Code: int main( int argn, char **arg ) { int port = atoi( arg[ 1 ] ); int sock_listen = socket( AF_INET, SOCK_STREAM, 0 ); ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    May 2005
    Posts
    3

    Simple Socket Server Source Help!


    I have this C++ code:
    Code:
    int main( int argn, char **arg )
    {
     int port = atoi( arg[ 1 ] );
    
     int sock_listen = socket( AF_INET, SOCK_STREAM, 0 );
    
     in_addr addr_any = { INADDR_ANY };
     sockaddr_in srv_addr;
     srv_addr.sin_family = AF_INET;
     srv_addr.sin_port = htons( port );
     srv_addr.sin_addr = addr_any;
    
     // set socket options to reuse address and port again in short time
     int opt = 1;
     setsockopt( sock_listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) );
    
     // assign address and port to socket
     bind( sock_listen, (const sockaddr * ) &srv_addr, sizeof( srv_addr ) );
    
     listen( sock_listen, 1 );
    
     int sock_client = 0;
    
     // go!
     while ( 1 )
     {
         char buf[ 100 ];
    
         fd_set read_wait_set;
         // set all bits to zero
         FD_ZERO( &read_wait_set );
    
         // wait for stdin
         FD_SET( STDIN_FILENO, &read_wait_set );
    
         // select listen or client socket
         if ( sock_client )
             FD_SET( sock_client, &read_wait_set );
         else
             FD_SET( sock_listen, &read_wait_set );
    
         // wait for selected handles
         if ( select( MAX( sock_client, sock_listen ) + 1,
                              &read_wait_set, 0, 0, 0 &#41; < 0 &#41; break;
    
         // data on stdin?
         if &#40; FD_ISSET&#40; STDIN_FILENO, &read_wait_set &#41; &#41;
         &#123;
             //  read data from stdin
             int l = read&#40; STDIN_FILENO, buf, sizeof&#40; buf &#41; &#41;;
             if &#40; l < 0 &#41;
                 printf&#40; "Unable to read data from stdin." &#41;;
             else
                 printf&#40; "Got %d bytes from stdin.", l &#41;;
    
             // send data to client
             l = write&#40; sock_client, buf, l &#41;;
             if &#40; l < 0 &#41;
                 printf&#40; "Unable send data to client." &#41;;
             else
                 printf&#40; "Sent %d bytes to client.", l &#41;;
         &#125;
    
         // new connection form client?
         else if &#40; FD_ISSET&#40; sock_listen, &read_wait_set &#41; &#41;
         &#123;
             sockaddr_in rsa;
             int rsa_size = sizeof&#40; rsa &#41;;
    
             // accept connection from client
             sock_client = accept&#40; sock_listen, &#40; sockaddr * &#41; &rsa, &#40; socklen_t * &#41; &rsa_size &#41;;
    
             uint lsa = sizeof&#40; srv_addr &#41;;
    
             // get my identification
             getsockname&#40; sock_client, &#40; sockaddr * &#41; &srv_addr, &lsa &#41;;
         
             // get client info
             getpeername&#40; sock_client, &#40; sockaddr * &#41; &srv_addr, &lsa &#41;;
    
             printf&#40; "Peer name&#58; '%s'  port&#58; %d",
                      inet_ntoa&#40; srv_addr.sin_addr &#41;, ntohs&#40; srv_addr.sin_port &#41; &#41;;
    
             printf&#40; "Enter 'quit' to quit server." &#41;;
         &#125;
    
         // data from client?
         else if &#40; FD_ISSET&#40; sock_client, &read_wait_set &#41; &#41;
         &#123;
             // read data from socket
             int l = read&#40; sock_client, buf, sizeof&#40; buf &#41; &#41;;
             if &#40; !l &#41;
             &#123;
                 printf&#40; "Client close socket." &#41;;
                 close&#40; sock_client &#41;;
                 sock_client = 0;
                 break;
             &#125;
             else if &#40; l < 0 &#41;
                 printf&#40; "Unable to read data from socket." &#41;;
             else
                 printf&#40; "Read %d bytes from socket.", l &#41;;
    
             // send all data to stdout
             l = write&#40; STDOUT_FILENO, buf, l &#41;;
    
             // check, if client ask to close connection
             if &#40; !strncasecmp&#40; buf, "close", 5 &#41; &#41;
             &#123;
                printf&#40; "Client sent 'close' request, connection closed." &#41;;
                printf&#40; "Now wait for new client." &#41;;
                 close&#40; sock_client &#41;;
                 sock_client = 0;
             &#125;
         &#125;
    
         // quit request from server or client
         if &#40; !strncasecmp&#40; buf, "quit", 4 &#41; &#41;
         &#123;
             close&#40; sock_client &#41;;
             printf&#40; "Quit request entered... exiting now...\n" &#41;;
             break;
         &#125;
     &#125;
    
     close&#40; sock_listen &#41;;
    
     return 0;
    &#125;
    It is a socket server. It accepts a connection from client and prints on the console a message that client writes. What i need it to do, is accept connections from many clients at once. When a client write something, server prints it and when server write a message, all clients print it.
    So far i have figured that i need to place "int pid = fork()" line after accept() function and let the parent process handle accepting connections and child handle the rest, but i still cant make it right!
    Could you please help me? Thanks for any answer!

  2. #2
    Just Joined!
    Join Date
    Feb 2005
    Location
    Delft, Holland
    Posts
    95
    Why can't one process both handle the connections and print/send the data?

    This can be easily done, you can make a simple infinite loop:
    Code:
     1.&#41; Check if there is any data from the sockets. 
         a&#41; If so&#58; see if it is a client requesting a connection&#58;
              i&#41; If so&#58; open he connection with the client.
              ii&#41; If not so&#40;then it is a client sending text&#41;&#58; push the text from the socket to stdout.
         b&#41; If not so&#40;no data in the socketbuffer&#41;, keep going with this iteration.
     2.&#41; Check if there is any data from stdin.
         a&#41; If so&#58; send that data to all the clients.
         b&#41; If not so&#40;no data in stdin&#41;, keep going with this iteration.    
     3.&#41; Get back to the beginning of the loop.
    You might run into one big problem, if the program starts reading from a socket it waits untill data arrives.
    And we can't have that! While we are waiting for data, the rest of program doesn't respond anymore, then data is not read from stdin untill data arrives from a client.
    So what we can do is set the socket to nonblocking.
    When a socket is nonblocking, a read immediately returns when there is no data in the socketbuffer, so then we can go on with the loop and check if there is any data somewhere else.

  3. #3
    Just Joined!
    Join Date
    May 2005
    Posts
    3
    Hmm, dont know. This might work but i need to demonstrate a typical IPC problem with use of this socket server. Because of that, i have to use two processes.
    But thanks anyway!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •