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