genericopenlibs/cstdlib/TSTLIB/TDGRAM.C
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /*
       
     2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 * Test of AF_LOCAL datagram sockets, based on Datagram example in GCC.HELP
       
    16 * 
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #include <stddef.h>
       
    23 #include <stdio.h>
       
    24 #include <errno.h>
       
    25 #include <stdlib.h>
       
    26 #include <string.h>
       
    27 
       
    28 #ifdef __X86GCC__  
       
    29 #undef WIN32  // Seems to be included in the X86 baseport somewhere (we don't want winsock as this isn't an emulator build)
       
    30 #endif
       
    31 
       
    32 #ifdef WIN32
       
    33 	#include <winsock.h>
       
    34 #else
       
    35 	#include <unistd.h>
       
    36 	#include <sys/socket.h>
       
    37 	#include <sys/ioctl.h>
       
    38 	#include <libc/netinet/in.h>
       
    39 	#include <libc/arpa/inet.h>
       
    40 #endif
       
    41 int
       
    42 make_named_socket (int port)
       
    43 {
       
    44 //  struct sockaddr name;
       
    45   struct sockaddr_in name;
       
    46   int sock;
       
    47   size_t size;
       
    48 
       
    49   /* Create the socket. */
       
    50 
       
    51   sock = socket (AF_INET, SOCK_DGRAM, 0);
       
    52 #ifdef WIN32
       
    53 	if ( INVALID_SOCKET == sock )
       
    54 	{
       
    55 		printf( "%s,%s,%d","CreateSocket", "socket", WSAGetLastError ( ) );
       
    56 		  exit (EXIT_FAILURE);
       
    57 	}
       
    58 #else
       
    59 	if (-1 == sock )
       
    60 	{
       
    61       perror ("make_named_socket");
       
    62 		  exit (EXIT_FAILURE);
       
    63 	}
       
    64 #endif
       
    65 
       
    66   /* Bind a name to the socket. */
       
    67 
       
    68   name.sin_family = AF_INET;
       
    69   name.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
       
    70   name.sin_port = htons((unsigned short)port);
       
    71 
       
    72   size = sizeof(struct sockaddr);
       
    73 
       
    74   if (bind (sock, (struct sockaddr *) &name, size) < 0)
       
    75     {
       
    76       perror ("bind");
       
    77       exit (EXIT_FAILURE);
       
    78     }
       
    79 
       
    80   return sock;
       
    81 }
       
    82 
       
    83 #define SERVER  99
       
    84 #define MAXMSG  512
       
    85 
       
    86 
       
    87 int server(void)
       
    88 	{
       
    89 	int server_sock;
       
    90 	char server_message[MAXMSG];
       
    91 	struct sockaddr name;
       
    92 	size_t size;
       
    93 	int nbytes = -1;
       
    94 
       
    95 	/* Make the socket, then loop endlessly. */
       
    96 
       
    97 	server_sock = make_named_socket (SERVER);
       
    98 	nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size);
       
    99 	if (nbytes < 0)
       
   100 		{
       
   101 		perror ("sendto (server)");
       
   102 		exit (EXIT_FAILURE);
       
   103 		}
       
   104 	while (1)
       
   105 		{
       
   106 		/* Wait for a datagram. */
       
   107 		size = sizeof (name);
       
   108 		nbytes = recvfrom (server_sock, server_message, MAXMSG, 0, (struct sockaddr *) & name, &size);
       
   109 		if (nbytes < 0)
       
   110 			{
       
   111 			perror ("recfrom (server)");
       
   112 			break;
       
   113 			}
       
   114 
       
   115 		/* Give a diagnostic message. */
       
   116 		fprintf (stderr, "Server: got message: %s\n", server_message);
       
   117 
       
   118 		/* Bounce the message back to the sender. */
       
   119 		nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size);
       
   120 		if (nbytes < 0)
       
   121 			{
       
   122 			perror ("sendto (server)");
       
   123 			break;
       
   124 			}
       
   125 		}/* end of - "while (1)" */
       
   126 	exit (EXIT_FAILURE);
       
   127 	return 0;
       
   128 	}
       
   129 
       
   130 #define CLIENT  125
       
   131 #define MESSAGE "Yow!!! Are we having fun yet?!? I'd like a 99 please..."
       
   132 
       
   133 int
       
   134 client (void)
       
   135 {
       
   136   int sock;
       
   137   char message[MAXMSG];
       
   138   struct sockaddr_in name;
       
   139   size_t size;
       
   140   int nbytes;
       
   141 
       
   142   /* Make the socket. */
       
   143   sock = make_named_socket (CLIENT);
       
   144 
       
   145   /* Initialize the server socket address. */
       
   146   name.sin_family = AF_INET;
       
   147   name.sin_port = htons((unsigned short)SERVER);
       
   148   size = sizeof(name);
       
   149 
       
   150   /* Send the datagram. */
       
   151   nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
       
   152                    (struct sockaddr *) & name, size);
       
   153   if (nbytes < 0)
       
   154     {
       
   155       perror ("sendto (client)");
       
   156       exit (EXIT_FAILURE);
       
   157     }
       
   158 
       
   159   /* Wait for a reply. */
       
   160   nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
       
   161   if (nbytes < 0)
       
   162     {
       
   163       perror ("recfrom (client)");
       
   164       exit (EXIT_FAILURE);
       
   165     }
       
   166 
       
   167   /* Print a diagnostic message. */
       
   168   fprintf (stderr, "Client: got message: %s\n", message);
       
   169 
       
   170   /* Clean up. */
       
   171 #ifdef WIN32
       
   172     closesocket (sock);
       
   173 #else
       
   174 	close(sock);
       
   175 #endif
       
   176 	return 0;
       
   177 }
       
   178 
       
   179 /**
       
   180 @SYMTestCaseID          SYSLIB-STDLIB-CT-1046
       
   181 @SYMTestCaseDesc	    Tests for AF_LOCAL datagram sockets
       
   182 @SYMTestPriority 	    High
       
   183 @SYMTestActions  	    Initialize the server socket address,send and receive messages on the socket.
       
   184 @SYMTestExpectedResults Test must not fail
       
   185 @SYMREQ                 REQ0000
       
   186 */		
       
   187 int both_together(void)
       
   188 {
       
   189   int server_sock;
       
   190   char server_message[MAXMSG];
       
   191   struct sockaddr_in name;
       
   192   size_t size;
       
   193   int nbytes;
       
   194   int sock;
       
   195   char message[MAXMSG];
       
   196 
       
   197   /* Make the server socket */
       
   198 
       
   199   server_sock = make_named_socket (SERVER);
       
   200 
       
   201   sock = make_named_socket (CLIENT);
       
   202 
       
   203   /* Initialize the server socket address. */
       
   204   name.sin_family = AF_INET;
       
   205   //name.sin_port = SERVER;
       
   206   name.sin_port = htons((unsigned short)SERVER);
       
   207 	  name.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
       
   208  size = sizeof(name);
       
   209 
       
   210   /* Send the datagram. */
       
   211   nbytes = sendto (server_sock, MESSAGE, strlen (MESSAGE) + 1, 0,
       
   212                    (struct sockaddr *) & name, size);
       
   213 
       
   214 
       
   215 #ifdef WIN32
       
   216   if (nbytes < 0)
       
   217 	{
       
   218 		printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) );
       
   219 		  exit (EXIT_FAILURE);
       
   220 	}
       
   221 #else
       
   222   if (nbytes < 0)
       
   223 	{
       
   224       perror ("sendto (server)");
       
   225 		  exit (EXIT_FAILURE);
       
   226 	}
       
   227 #endif
       
   228   {
       
   229   /* more server stuff */
       
   230 
       
   231       size = sizeof (name);
       
   232       nbytes = recvfrom (server_sock, server_message, MAXMSG, 0,
       
   233                          (struct sockaddr *) & name, &size);
       
   234 #ifdef WIN32
       
   235       if (nbytes < 0)
       
   236 	{
       
   237 		printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) );
       
   238 		  exit (EXIT_FAILURE);
       
   239 	}
       
   240 #else
       
   241       if (nbytes < 0)
       
   242 	{
       
   243       perror ("recvfrom (server)");
       
   244 		  exit (EXIT_FAILURE);
       
   245 	}
       
   246 #endif
       
   247 
       
   248       /* Give a diagnostic message. */
       
   249       fprintf (stderr, "Server: got message: %s\n", server_message);
       
   250 
       
   251       /* Bounce the message back to the sender. */
       
   252   name.sin_port = htons((unsigned short)CLIENT);
       
   253       nbytes = sendto (sock, server_message, nbytes, 0,
       
   254                        (struct sockaddr *) & name, size);
       
   255 #ifdef WIN32
       
   256       if (nbytes < 0)
       
   257         {
       
   258 		printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) );
       
   259           exit (EXIT_FAILURE);
       
   260         }
       
   261 #else
       
   262       if (nbytes < 0)
       
   263 	{
       
   264       perror ("sendto (client)");
       
   265 		  exit (EXIT_FAILURE);
       
   266 	}
       
   267 #endif
       
   268   }
       
   269 
       
   270   /* Wait for a reply. */
       
   271   nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
       
   272 #ifdef WIN32
       
   273       if (nbytes < 0)
       
   274         {
       
   275 		printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) );
       
   276           exit (EXIT_FAILURE);
       
   277         }
       
   278 #else
       
   279       if (nbytes < 0)
       
   280 	{
       
   281       perror ("recvfrom (client)");
       
   282 		  exit (EXIT_FAILURE);
       
   283 	}
       
   284 #endif
       
   285 
       
   286   /* Print a diagnostic message. */
       
   287   fprintf (stderr, "Client: got message: %s\n", message);
       
   288 
       
   289   /* Clean up. */
       
   290 #ifdef WIN32
       
   291   closesocket (sock);
       
   292   closesocket(server_sock);
       
   293 #else
       
   294   close(sock);
       
   295   close(server_sock);
       
   296 #endif
       
   297 
       
   298   return 0;
       
   299 }
       
   300 
       
   301 int main(int argc, char*argv[])
       
   302 	{
       
   303 #ifdef WIN32
       
   304 	WORD wVersionRequested;WSADATA wsaData;int err; 
       
   305 	wVersionRequested = MAKEWORD( 1, 0 ); 
       
   306 	err = WSAStartup( wVersionRequested, &wsaData );
       
   307 #endif
       
   308 	return both_together();
       
   309 }