Portál AbcLinuxu, 12. května 2025 05:58
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; }Je to socket server. Prijima pripojeni od klientu a vypise do konzole zpravu, kterou klient napise. Ja bych prave potreboval aby server prijimal spojeni od nekolika klientu najednou a kdyz nejaky klient neco napise, server to vypise do konzole a kdyz neco napise server, vypisou to na svou konzoli vsichni klienti. Zatim jsem jen prisel na to, ze musim vlozit radek "int pid = fork()" do kodu za funkci accept() a nechat otcovsky proces at vyrizuje pripojeni a nechat potomka at dela zbytek, ale stale se mi to nedari! Mohli byste mi poradit? Byl bych vdecny za jakoukoliv pomoc. Predem diky.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.