Preface
This section describes how to use basic Linux/Unix functions 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>
# Define Maxline 1024
// Typedef struct sockaddr SA;
Void Handle ( Int Connfd );
Int Main ( Int Argc, Char ** Argv)
{
Int Listenfd, connfd;
Int SERVERPORT = 6888 ;
Int Listenq = 1024 ;
Pid_t childpid;
Char Buf [maxline];
Socklen_t socklen;
Struct Sockaddr_in cliaddr, servaddr;
Socklen = Sizeof (Cliaddr );
Bzero (& servaddr, Sizeof (Servaddr ));
Servaddr. sin_family = af_inet;
Servaddr. sin_addr.s_addr = htonl (inaddr_any );
Servaddr. sin_port = htons (SERVERPORT );
Listenfd = socket (af_inet, sock_stream, 0 );
If (Listenfd < 0 ){
Perror ( " Socket Error " );
Return - 1 ;
}
If (BIND (listenfd ,( Struct Sockaddr *) & servaddr, socklen) < 0 ){
Perror ( " BIND error " );
Return - 1 ;
}
If (Listen (listenfd, listenq) < 0 ){
Perror ( " Listen Error " );
Return - 1 ;
}
Printf ( " Echo server startup, listen on port: % d \ n " , SERVERPORT );
For (;;){
Connfd = accept (listenfd ,( Struct Sockaddr *) & cliaddr, & socklen );
If (Connfd < 0 ){
Perror ( " Accept Error " );
Continue ;
}
Sprintf (BUF, " Accept form % s: % d \ n " , Inet_ntoa (cliaddr. sin_addr), cliaddr. sin_port );
Printf (BUF, "" );
Childpid = fork ();
If (Childpid = 0 ){ /* Child Process */
Close (listenfd ); /* Close listening socket */
Handle (connfd ); /* Process the request */
Exit ( 0 );
} Else If (Childpid> 0 ){
Close (connfd ); /* Parent closes connected socket */
} Else {
Perror ( " Fork Error " );
}
}
}
Void Handle ( Int Connfd)
{
Size_t N;
Char Buf [maxline];
For (;;){
N = read (connfd, Buf, maxline );
If (N < 0 ){
If (Errno! = Eintr ){
Perror ( " Read Error " );
Break ;
}
}
If (N = 0 ){
// Connfd is closed by Client
Close (connfd );
Printf ( " Client exit \ n " );
Break ;
}
// Client exit
If (Strncmp ( " Exit " , Buf, 4 ) = 0 ){
Close (connfd );
Printf ( " Client exit \ n " );
Break ;
}
Write (connfd, Buf, N ); // Write maybe fail, here don't process failed Error
}
}
Client
The Code is as 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 <netdb. h> /* Gethostbyname Function */
# Include <stdlib. h>
# Include <errno. h>
# Include <stdio. h>
# Include <String . H>
# Define Maxline 1024
Void Handle ( Int Connfd );
Int Main ( Int Argc, Char ** Argv)
{
Char * Servinetaddr = " 127.0.0.1 " ;
Int Servport = 6888 ;
Char Buf [maxline];
Int Connfd;
Struct Sockaddr_in servaddr;
If (Argc = 2 ){
Servinetaddr = argv [ 1 ];
}
If (Argc = 3 ){
Servinetaddr = argv [ 1 ];
Servport = atoi (argv [ 2 ]);
}
If (Argc> 3 ){
Printf ( " Usage: echoclient <IPaddress> <port> \ n " );
Return - 1 ;
}
Connfd = socket (af_inet, sock_stream, 0 );
Bzero (& servaddr, Sizeof (Servaddr ));
Servaddr. sin_family = af_inet;
Servaddr. sin_port = htons (servport );
Inet_ton (af_inet, servinetaddr, & servaddr. sin_addr );
If (Connect (connfd ,( Struct Sockaddr *) & servaddr, Sizeof (Servaddr) < 0 ){
Perror (" Connect Error " );
Return - 1 ;
}
Printf ( " Welcome to echoclient \ n " );
Handle (connfd ); /* Do it all */
Close (connfd );
Printf ( " Exit \ n " );
Exit ( 0 );
}
Void Handle ( Int Sockfd)
{
Char Sendline [maxline], recvline [maxline];
Int N;
For (;;){
If (Fgets (sendline, maxline, stdin) = NULL ){
Break ; // Read EOF
}
/*
// You can also directly use system functions without caching without using the buffer stream of the standard library
If (read (stdin_fileno, sendline, maxline) = 0 ){
Break; // read EOF
}
*/
N = write (sockfd, sendline, strlen (sendline ));
N = read (sockfd, recvline, maxline );
If (N = 0 ){
Printf (" Echoclient: Server terminated prematurely \ n " );
Break ;
}
Write (stdout_fileno, recvline, N );
// If the standard library's cache stream output is used, problems may occur sometimes.
// Fputs (recvline, stdout );
}
}
Download and compile
Compile and start the server:
GCC echoserver. C-o echoserver
./Echoserver
Compile and start the client
GCC echoclient. C-o echoclient
./Echoclient