Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Connection-oriented server for AF_INET / AF_INET6

&pagelevel(4)&pagelevel

Programming of the main program loop is shown in the following examples.
The server uses the following socket interface functions in the example programs:

  • socket(): create socket

  • bind(): assign a socket a name

  • listen(): “listen” to a socket for connection requests

  • accept(): accept a connection on a socket

  • recv(): read data from a socket

  • soc_close(): close socket

 
Example: Connection-oriented server for AF_INET

#include <stdio.h>
#include <sys.types.h>
#include <sys.socket.h>
#include <netinet.in.h>
#include <netdb.h>
 
main(argc, argv)
    int argc;
    char *argv[];
{
#define TESTPORT 2222
  
    int sock, length;
    struct sockaddr_in server;
    struct sockaddr_in client;
    int clientlen;
    int msgsock;
    char buf[1024];
int rval;
    memset(&server,'\0',sizeof(server));
    memset(&client,'\0',sizeof(client));
    clientlen = sizeof(client);
  
/* Create socket */
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0)
       { perror("Create stream socket");
         exit(1);
       }
  
    /* Assign the socket a name */
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(TESTPORT);
   
if (bind(sock, (struct sockaddr *)&server, sizeof (server) ) < 0)
       { perror("Bind stream socket");
         exit(1);
       }
  
/* Start acceptance of connection requests */
    listen(sock, 5);
  
msgsock = accept(sock, (struct sockaddr *)&client, &clientlen);
if (msgsock == -1)
       { perror("Accept connection");
         exit(1);
       }
    else do {
            memset(buf, 0, sizeof buf);
            if ((rval = recv(msgsock, buf, 1024, 0)) < 0)
               { perror("Reading stream message");
                 exit(1);
               }
             if (rval == 0 )
                 fprintf(stderr, "Ending connection\n");
             else
                 fprintf(stdout, "->%s\n", buf);
            } while (rval != 0);
  
  soc_close(msgsock);
  soc_close(sock);
}

 
Example: Connection-oriented server for AF_INET6

#include <stdio.h>
#include <sys.types.h>
#include <sys.socket.h>
#include <netinet.in.h>
#include <netdb.h>
  
main(argc, argv)
     int argc;
     char *argv[];
{
     #define TESTPORT 2222
     int sock, length;
     struct sockaddr_in6 server;
     struct sockaddr_in6 client;
     int clientlen;
     struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
     int msgsock;
     char buf[1024];
     int rval;
     memset(&server,'\0',sizeof(server));
     memset(&client,'\0',sizeof(client));
     clientlen = sizeof(client);
  
     /* Create socket */
     sock = socket(AF_INET6, SOCK_STREAM, 0);
     if (sock < 0)
        { 
        perror("Create stream socket");
        exit(1);
        }
   
     /* Assign the socket a name */
   
     server.sin6_family = AF_INET6;
     memcpy(server.sin6_addr.s6_addr, in6addr_any,16) ;
     server.sin6_port = htons(TESTPORT);
     if (bind(sock, (struct sockaddr *)&server, sizeof (server) ) < 0)
        {
        perror("Bind stream socket");
        exit(1);
        }
  
     /* Start acceptance of connection requests */
  
     listen(sock, 5);
     msgsock = accept(sock, (struct sockaddr *)&client, &clientlen);
   
     if (msgsock == -1)
        { 
        perror("Accept connection");
        exit(1);
        }
   
     else do 
        {
        memset(buf, 0, sizeof buf);
        if ((rval = recv(msgsock, buf, 1024, 0)) < 0)
           { 
           perror("Reading stream message");
           exit(1);
           }
        else if (rval == 0 )
           fprintf(stderr, "Ending connection\n");
        else
           fprintf(stdout, "->%s\n", buf);
        } 
  
     while (rval != 0);
     soc_close(msgsock);
     soc_close(sock);
}

The following steps are executed in the program examples for AF_INET and AF_INET6:

  1. The server uses the socket() function to create a communications endpoint (socket) and the corresponding descriptor.
  2. The server socket is assigned a defined port number with the bind() function. It can then be addressed in the network via this port number.

  3. The server uses the listen() function to determine whether connection requests are pending.

  4. The server can accept connection requests with accept(). The value returned by accept() is tested to ensure that the connection was successfully set up.

  5. As soon as the connection is set up, data is read from the socket with the recv() function.

  6. The server closes the socket with the soc_close() function.