Preface
This section describes how to use basic Linux/Unix functions and poll calls to compile a complete server and client example that can be run on Linux (UBUNTU) and Unix (FreeBSD, the functions of the client and server are as follows:
The client reads a row from the standard input and sends it to the server.
The server reads a row from the network and then outputs the row to the client.
The client receives a response from the server and outputs this line to the standard output.
Server
CodeAs follows:
# Include <unistd. h>
# Include <sys/types. h> /* Basic System Data Types */
# Include <sys/socket. h> /* Basic socket Definitions */
# Include <netinet/ In . H> /* Sockaddr_in {} and other Internet defns */
# Include <ARPA/inet. h> /* INet (3) Functions */
# Include <stdlib. h>
# Include <errno. h>
# Include <stdio. h>
# Include < String . H>
# Include <poll. h> /* Poll Function */
# Include <limits. h>
# Define Maxline 10240
# Ifndef open_max
# Define Open_max 40960
# Endif
Void Handle ( Struct Pollfd * clients, Int Maxclient,Int Readyclient );
Int Main ( Int Argc, Char ** Argv)
{
Int Servport = 6888 ;
Int Listenq = 1024 ;
Int Listenfd, connfd;
Struct Pollfd clients [open_max];
Int Maxi;
Socklen_t socklen = Sizeof ( Struct Sockaddr_in );
Struct Sockaddr_in cliaddr, servaddr;
Char Buf [maxline];
Int Nready;
Bzero (& servaddr, socklen );
Servaddr. sin_family = af_inet;
Servaddr. sin_addr.s_addr = htonl (inaddr_any );
Servaddr. sin_port = htons (servport );
Listenfd = socket (af_inet, sock_stream, 0 );
If (Listenfd < 0 ){
Perror ( " Socket Error " );
}
Int Opt = 1 ;
If (Setsockopt (listenfd, sol_socket, so_reuseaddr, & OPT, Sizeof (OPT) <0 ){
Perror ( " Setsockopt Error " );
}
If (BIND (listenfd ,( Struct Sockaddr *) & servaddr, socklen) =- 1 ){
Perror ( " BIND error " );
Exit (- 1 );
}
If (Listen (listenfd, listenq) < 0 ){
Perror ( " Listen Error " );
}
Clients [ 0 ]. FD = listenfd;
Clients [ 0 ]. Events = Pollin;
Int I;
For (I =1 ; I <open_max; I ++)
Clients [I]. FD =- 1 ;
Maxi = listenfd + 1 ;
Printf ( " Pollechoserver startup, listen on port: % d \ n " , Servport );
Printf ( " Max connection is % d \ n " , Open_max );
For (;;){
Nready = poll (clients, Maxi + 1 ,- 1 );
// Printf ("nready is % d \ n", nready );
If (Nready =- 1 ){
Perror ( " Poll Error " );
}
If (Clients [ 0 ]. Revents & Pollin ){
Connfd = accept (listenfd ,( Struct Sockaddr *) & cliaddr, & socklen );
Sprintf (BUF, " Accept form % s: % d \ n " , Inet_ntoa (cliaddr. sin_addr), cliaddr. sin_port );
Printf (BUF, "" );
For (I = 0 ; I <open_max; I ++ ){
If (Clients [I]. FD =-1 ){
Clients [I]. FD = connfd;
Clients [I]. Events = Pollin;
Break ;
}
}
If (I = open_max ){
Fprintf (stderr, " Too connected connection, more than % d \ n " , Open_max );
Close (connfd );
Continue ;
}
If (I> Maxi)
Maxi = I;
-- Nready;
}
Handle (clients, Maxi, nready );
}
}
Void Handle ( Struct Pollfd * clients, Int Maxclient, Int Nready ){
Int Connfd;
Int I, nread;
Char Buf [maxline];
If (Nready = 0 )
Return ;
For (I = 1 ; I <maxclient; I ++ ){
Connfd = clients [I]. FD;
If (Connfd =- 1 )
Continue ;
If (Clients [I]. revents & (Pollin | pollerr )){
Nread = read (connfd, Buf, maxline ); // Read client socket stream
If (Nread < 0 ){
Perror ( " Read Error " );
Close (connfd );
Clients [I]. FD =- 1 ;
Continue ;
}
If (Nread = 0 ){
Printf ( " Client close the connection " );
Close (connfd );
Clients [I]. FD =- 1 ;
Continue ;
}
Write (connfd, Buf, nread );// Response Client
If (-- Nready <= 0 ) // No connection to be processed, exit the loop
Break ;
}
}
}
Download and compile
Compile and start the server
GCC pollechoserver. C-o pollechoserver
./Pollechoserver
For the client, refer to the echoclient example of the Linux/Unix server and client socket programming getting started instance in this article to download and compile.