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.
- 05-12-2005 #1Just Joined!
- Join Date
- May 2005
- Posts
- 3
Simple Socket Server Source Help!
I have this C++ code:
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.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 ) < 0 ) break; // data on stdin? if ( FD_ISSET( STDIN_FILENO, &read_wait_set ) ) { // read data from stdin int l = read( STDIN_FILENO, buf, sizeof( buf ) ); if ( l < 0 ) printf( "Unable to read data from stdin." ); else printf( "Got %d bytes from stdin.", l ); // send data to client l = write( sock_client, buf, l ); if ( l < 0 ) printf( "Unable send data to client." ); else printf( "Sent %d bytes to client.", l ); } // new connection form client? else if ( FD_ISSET( sock_listen, &read_wait_set ) ) { sockaddr_in rsa; int rsa_size = sizeof( rsa ); // accept connection from client sock_client = accept( sock_listen, ( sockaddr * ) &rsa, ( socklen_t * ) &rsa_size ); uint lsa = sizeof( srv_addr ); // get my identification getsockname( sock_client, ( sockaddr * ) &srv_addr, &lsa ); // get client info getpeername( sock_client, ( sockaddr * ) &srv_addr, &lsa ); printf( "Peer name: '%s' port: %d", inet_ntoa( srv_addr.sin_addr ), ntohs( srv_addr.sin_port ) ); printf( "Enter 'quit' to quit server." ); } // data from client? else if ( FD_ISSET( sock_client, &read_wait_set ) ) { // read data from socket int l = read( sock_client, buf, sizeof( buf ) ); if ( !l ) { printf( "Client close socket." ); close( sock_client ); sock_client = 0; break; } else if ( l < 0 ) printf( "Unable to read data from socket." ); else printf( "Read %d bytes from socket.", l ); // send all data to stdout l = write( STDOUT_FILENO, buf, l ); // check, if client ask to close connection if ( !strncasecmp( buf, "close", 5 ) ) { printf( "Client sent 'close' request, connection closed." ); printf( "Now wait for new client." ); close( sock_client ); sock_client = 0; } } // quit request from server or client if ( !strncasecmp( buf, "quit", 4 ) ) { close( sock_client ); printf( "Quit request entered... exiting now...\n" ); break; } } close( sock_listen ); return 0; }
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!
- 05-14-2005 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
No, you let the child process the accept():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!
Code:for (;;) /* loop for connections */ { if ((t= accept(s, NULL, NULL)) < 0) /* get a connection */ { if (errno == EINTR) /* EINTR might happen on accept(), */ continue; /* try again */ perror("accept"); /* bad */ exit(1); } switch(fork()) { /* try to handle connection */ case -1 : /* bad news. scream and die */ perror("fork"); close(s); close(t); exit(1); case 0 : /* we're the child, do something */ close(s); do_something(t); exit(0); default : /* we're the parent so look for */ close(t); /* another connection */ continue; } }
- 05-14-2005 #3Just Joined!
- Join Date
- Apr 2005
- Location
- Romania
- Posts
- 42
Your queue length of pending client connections is 1; extend it with listen():
listen(sock_listen, n), where n is number of connections allowed on the incoming queue ( max n = SOMAXCONN ).


Reply With Quote
