{\scriptsize \begin{lstlisting}
     1	/**
     2	   @file
     3	
     4	   This program tests the low-level tcp-socket-stream library function.
     5	
     6	*/
     7	
     8	/*
     9	  Copyright (c) Andreas Hofmeier
    10	  (www.an-h.de, www.an-h.de.vu, www.lgut.uni-bremen.de/an-h/)
    11	  
    12	  This program is free software; you can redistribute it and/or modify
    13	  it under the terms of the GNU General Public License as published by
    14	  the Free Software Foundation; either version 2 of the License, or
    15	  (at your option) any later version.
    16	
    17	  This program is distributed in the hope that it will be useful, but
    18	  WITHOUT ANY WARRANTY; without even the implied warranty of
    19	  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    20	  General Public License for more details.
    21	
    22	  You should have received a copy of the GNU General Public License
    23	  along with this program; if not, write to the Free Software
    24	  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    25	*/
    26	
    27	
    28	#include <stdio.h>
    29	#include <arpa/inet.h>
    30	#include <sys/types.h>
    31	#include <signal.h>
    32	#include "../lib/libcomm.h"
    33	
    34	/** Port on which the server is listening and the client trys to
    35	    connect. */
    36	#define PORT 1234
    37	/** How much random bytes should be transfered? */
    38	#define bufsize 8192
    39	//#define bufsize 8
    40	
    41	/** test0001sockts: see test0001sockts.c.
    42	*/
    43	
    44	main(int argc, char *argv[]) {
    45	  // PID of the child process
    46	  int fpid;
    47	
    48	  printf("running %s...\n", argv[0]);
    49	  if (!(fpid = fork())) {
    50	    server_test_program(PORT);
    51	  }
    52	
    53	  client_test_program("127.0.0.1", PORT);
    54	  client_test_program("localhost", PORT);
    55	  client_test_program("lblacky", PORT);
    56	  client_test_program("hofmeira.student.sbu.ac.uk", PORT);
    57	
    58	  // kill server terst programm
    59	  kill(fpid, 15);
    60	  sleep(1);
    61	  kill(fpid, 9);
    62	
    63	  exit(0);
    64	}
    65	
    66	
    67	/** test0001sockts: see test0001sockts.c, Client Test Programm.
    68	
    69	   The client test program generates a block of n*(bufsize) random
    70	   bytes, sents theses bytes to the server, receive a block from
    71	   server, invert it and compare it with generated block.
    72	
    73	   BUGS: Receives only one block. If not all data ready and function
    74	   resv() do noct block, data get lost. Test fails.
    75	
    76	   @param server a string which contains the server name
    77	   @param port an integer which specifies the port on the server
    78	*/
    79	client_test_program(char *server, int port) {
    80	  // **************************************************************
    81	  // *** Client Test Programm
    82	
    83	  int i;
    84	
    85	  // FD of socket
    86	  int sock;
    87	
    88	  char buf[bufsize], buf2[bufsize];
    89	
    90	  printf("  Generating random numbers...\n");
    91	  if (block_random(buf, bufsize) == NULL) {
    92	    fail("cannot generate random numbers", 1);
    93	  }
    94	  
    95	  // Connect to server
    96	  printf("  Try server %s...", server);
    97	  if ((sock = socket_connect(server, port)) < 0) {
    98	    fail("Cannot connect.", 1);    
    99	  }
   100	
   101	  // send datablock to server
   102	  send(sock, buf, bufsize, 0);
   103	
   104	  // receive datablock from server
   105	  recv(sock, buf2, bufsize, 0);
   106	
   107	  close(sock);
   108	
   109	  // invert the bits of the local datablock and
   110	  // compare it with the result from the server
   111	  // should be the same.
   112	  for (i = 0; i < bufsize; i++) {
   113	    if (~buf2[i] != buf[i]) {
   114	      fail("some errors occure during the data transfer...");
   115	    }
   116	  }
   117	  printf("OK.\n");
   118	  sleep(1);
   119	}
   120	
   121	
   122	/** test0001sockts: see test0001sockts.c, Server Test Programm.
   123	
   124	   The server test program binds a port and waits for connections. If
   125	   someone connects it reads n*(bufsize) bytes, invert all bits of
   126	   these bytes and send all back.
   127	
   128	   BUGS: Receives only one block. If not all data ready and function
   129	   resv() do noct block, data get lost. Test fails.
   130	
   131	   @param port an integer which specifies the port to bind.
   132	*/
   133	server_test_program(int port) {
   134	  // **************************************************************
   135	  // *** Server Test Program
   136	
   137	  // buffer for storing data.
   138	  char buf[bufsize];
   139	
   140	  // FD of socket which is bounded to the port
   141	  int sockport;
   142	  // FD of socket
   143	  int sock;
   144	
   145	  /* connector's address information */
   146	  struct sockaddr_in their_addr;
   147	  int sin_size;
   148	
   149	    
   150	  // Bind port
   151	  printf("  binding port %d on localhost...", port);
   152	  if ((sockport = socket_bind(port, 10)) < 0) {
   153	    fail("Cannot bind port.", 1);
   154	  }
   155	
   156	  while (1) {
   157	    // accept connection
   158	    sin_size = sizeof(struct sockaddr_in);
   159	    if ((sock = accept(sockport, (struct sockaddr *) &their_addr,
   160			       &sin_size)) != -1) {
   161	      int i, size;
   162	      char *pard = inet_ntoa(their_addr.sin_addr);
   163	      fprintf(stdout, "  got connection from %s\n", pard);
   164	      
   165	      // receive datablock from client
   166	      size = recv(sock, buf, bufsize, 0);
   167	      // invert the bits of thh whole datablock
   168	      for (i = 0; i < size; i++) {
   169		buf[i] = ~buf[i];
   170	      }
   171	      // send datablock to client
   172	      send(sock, buf, size, 0);
   173	
   174	      close(sock);
   175	    }
   176	  }
   177	}  
\end{lstlisting}}
