1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericopenlibs/cstdlib/TSTLIB/TDGRAM.C Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,309 @@
1.4 +/*
1.5 +* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* Test of AF_LOCAL datagram sockets, based on Datagram example in GCC.HELP
1.19 +*
1.20 +*
1.21 +*/
1.22 +
1.23 +
1.24 +
1.25 +#include <stddef.h>
1.26 +#include <stdio.h>
1.27 +#include <errno.h>
1.28 +#include <stdlib.h>
1.29 +#include <string.h>
1.30 +
1.31 +#ifdef __X86GCC__
1.32 +#undef WIN32 // Seems to be included in the X86 baseport somewhere (we don't want winsock as this isn't an emulator build)
1.33 +#endif
1.34 +
1.35 +#ifdef WIN32
1.36 + #include <winsock.h>
1.37 +#else
1.38 + #include <unistd.h>
1.39 + #include <sys/socket.h>
1.40 + #include <sys/ioctl.h>
1.41 + #include <libc/netinet/in.h>
1.42 + #include <libc/arpa/inet.h>
1.43 +#endif
1.44 +int
1.45 +make_named_socket (int port)
1.46 +{
1.47 +// struct sockaddr name;
1.48 + struct sockaddr_in name;
1.49 + int sock;
1.50 + size_t size;
1.51 +
1.52 + /* Create the socket. */
1.53 +
1.54 + sock = socket (AF_INET, SOCK_DGRAM, 0);
1.55 +#ifdef WIN32
1.56 + if ( INVALID_SOCKET == sock )
1.57 + {
1.58 + printf( "%s,%s,%d","CreateSocket", "socket", WSAGetLastError ( ) );
1.59 + exit (EXIT_FAILURE);
1.60 + }
1.61 +#else
1.62 + if (-1 == sock )
1.63 + {
1.64 + perror ("make_named_socket");
1.65 + exit (EXIT_FAILURE);
1.66 + }
1.67 +#endif
1.68 +
1.69 + /* Bind a name to the socket. */
1.70 +
1.71 + name.sin_family = AF_INET;
1.72 + name.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
1.73 + name.sin_port = htons((unsigned short)port);
1.74 +
1.75 + size = sizeof(struct sockaddr);
1.76 +
1.77 + if (bind (sock, (struct sockaddr *) &name, size) < 0)
1.78 + {
1.79 + perror ("bind");
1.80 + exit (EXIT_FAILURE);
1.81 + }
1.82 +
1.83 + return sock;
1.84 +}
1.85 +
1.86 +#define SERVER 99
1.87 +#define MAXMSG 512
1.88 +
1.89 +
1.90 +int server(void)
1.91 + {
1.92 + int server_sock;
1.93 + char server_message[MAXMSG];
1.94 + struct sockaddr name;
1.95 + size_t size;
1.96 + int nbytes = -1;
1.97 +
1.98 + /* Make the socket, then loop endlessly. */
1.99 +
1.100 + server_sock = make_named_socket (SERVER);
1.101 + nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size);
1.102 + if (nbytes < 0)
1.103 + {
1.104 + perror ("sendto (server)");
1.105 + exit (EXIT_FAILURE);
1.106 + }
1.107 + while (1)
1.108 + {
1.109 + /* Wait for a datagram. */
1.110 + size = sizeof (name);
1.111 + nbytes = recvfrom (server_sock, server_message, MAXMSG, 0, (struct sockaddr *) & name, &size);
1.112 + if (nbytes < 0)
1.113 + {
1.114 + perror ("recfrom (server)");
1.115 + break;
1.116 + }
1.117 +
1.118 + /* Give a diagnostic message. */
1.119 + fprintf (stderr, "Server: got message: %s\n", server_message);
1.120 +
1.121 + /* Bounce the message back to the sender. */
1.122 + nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size);
1.123 + if (nbytes < 0)
1.124 + {
1.125 + perror ("sendto (server)");
1.126 + break;
1.127 + }
1.128 + }/* end of - "while (1)" */
1.129 + exit (EXIT_FAILURE);
1.130 + return 0;
1.131 + }
1.132 +
1.133 +#define CLIENT 125
1.134 +#define MESSAGE "Yow!!! Are we having fun yet?!? I'd like a 99 please..."
1.135 +
1.136 +int
1.137 +client (void)
1.138 +{
1.139 + int sock;
1.140 + char message[MAXMSG];
1.141 + struct sockaddr_in name;
1.142 + size_t size;
1.143 + int nbytes;
1.144 +
1.145 + /* Make the socket. */
1.146 + sock = make_named_socket (CLIENT);
1.147 +
1.148 + /* Initialize the server socket address. */
1.149 + name.sin_family = AF_INET;
1.150 + name.sin_port = htons((unsigned short)SERVER);
1.151 + size = sizeof(name);
1.152 +
1.153 + /* Send the datagram. */
1.154 + nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
1.155 + (struct sockaddr *) & name, size);
1.156 + if (nbytes < 0)
1.157 + {
1.158 + perror ("sendto (client)");
1.159 + exit (EXIT_FAILURE);
1.160 + }
1.161 +
1.162 + /* Wait for a reply. */
1.163 + nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
1.164 + if (nbytes < 0)
1.165 + {
1.166 + perror ("recfrom (client)");
1.167 + exit (EXIT_FAILURE);
1.168 + }
1.169 +
1.170 + /* Print a diagnostic message. */
1.171 + fprintf (stderr, "Client: got message: %s\n", message);
1.172 +
1.173 + /* Clean up. */
1.174 +#ifdef WIN32
1.175 + closesocket (sock);
1.176 +#else
1.177 + close(sock);
1.178 +#endif
1.179 + return 0;
1.180 +}
1.181 +
1.182 +/**
1.183 +@SYMTestCaseID SYSLIB-STDLIB-CT-1046
1.184 +@SYMTestCaseDesc Tests for AF_LOCAL datagram sockets
1.185 +@SYMTestPriority High
1.186 +@SYMTestActions Initialize the server socket address,send and receive messages on the socket.
1.187 +@SYMTestExpectedResults Test must not fail
1.188 +@SYMREQ REQ0000
1.189 +*/
1.190 +int both_together(void)
1.191 +{
1.192 + int server_sock;
1.193 + char server_message[MAXMSG];
1.194 + struct sockaddr_in name;
1.195 + size_t size;
1.196 + int nbytes;
1.197 + int sock;
1.198 + char message[MAXMSG];
1.199 +
1.200 + /* Make the server socket */
1.201 +
1.202 + server_sock = make_named_socket (SERVER);
1.203 +
1.204 + sock = make_named_socket (CLIENT);
1.205 +
1.206 + /* Initialize the server socket address. */
1.207 + name.sin_family = AF_INET;
1.208 + //name.sin_port = SERVER;
1.209 + name.sin_port = htons((unsigned short)SERVER);
1.210 + name.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
1.211 + size = sizeof(name);
1.212 +
1.213 + /* Send the datagram. */
1.214 + nbytes = sendto (server_sock, MESSAGE, strlen (MESSAGE) + 1, 0,
1.215 + (struct sockaddr *) & name, size);
1.216 +
1.217 +
1.218 +#ifdef WIN32
1.219 + if (nbytes < 0)
1.220 + {
1.221 + printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) );
1.222 + exit (EXIT_FAILURE);
1.223 + }
1.224 +#else
1.225 + if (nbytes < 0)
1.226 + {
1.227 + perror ("sendto (server)");
1.228 + exit (EXIT_FAILURE);
1.229 + }
1.230 +#endif
1.231 + {
1.232 + /* more server stuff */
1.233 +
1.234 + size = sizeof (name);
1.235 + nbytes = recvfrom (server_sock, server_message, MAXMSG, 0,
1.236 + (struct sockaddr *) & name, &size);
1.237 +#ifdef WIN32
1.238 + if (nbytes < 0)
1.239 + {
1.240 + printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) );
1.241 + exit (EXIT_FAILURE);
1.242 + }
1.243 +#else
1.244 + if (nbytes < 0)
1.245 + {
1.246 + perror ("recvfrom (server)");
1.247 + exit (EXIT_FAILURE);
1.248 + }
1.249 +#endif
1.250 +
1.251 + /* Give a diagnostic message. */
1.252 + fprintf (stderr, "Server: got message: %s\n", server_message);
1.253 +
1.254 + /* Bounce the message back to the sender. */
1.255 + name.sin_port = htons((unsigned short)CLIENT);
1.256 + nbytes = sendto (sock, server_message, nbytes, 0,
1.257 + (struct sockaddr *) & name, size);
1.258 +#ifdef WIN32
1.259 + if (nbytes < 0)
1.260 + {
1.261 + printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) );
1.262 + exit (EXIT_FAILURE);
1.263 + }
1.264 +#else
1.265 + if (nbytes < 0)
1.266 + {
1.267 + perror ("sendto (client)");
1.268 + exit (EXIT_FAILURE);
1.269 + }
1.270 +#endif
1.271 + }
1.272 +
1.273 + /* Wait for a reply. */
1.274 + nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
1.275 +#ifdef WIN32
1.276 + if (nbytes < 0)
1.277 + {
1.278 + printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) );
1.279 + exit (EXIT_FAILURE);
1.280 + }
1.281 +#else
1.282 + if (nbytes < 0)
1.283 + {
1.284 + perror ("recvfrom (client)");
1.285 + exit (EXIT_FAILURE);
1.286 + }
1.287 +#endif
1.288 +
1.289 + /* Print a diagnostic message. */
1.290 + fprintf (stderr, "Client: got message: %s\n", message);
1.291 +
1.292 + /* Clean up. */
1.293 +#ifdef WIN32
1.294 + closesocket (sock);
1.295 + closesocket(server_sock);
1.296 +#else
1.297 + close(sock);
1.298 + close(server_sock);
1.299 +#endif
1.300 +
1.301 + return 0;
1.302 +}
1.303 +
1.304 +int main(int argc, char*argv[])
1.305 + {
1.306 +#ifdef WIN32
1.307 + WORD wVersionRequested;WSADATA wsaData;int err;
1.308 + wVersionRequested = MAKEWORD( 1, 0 );
1.309 + err = WSAStartup( wVersionRequested, &wsaData );
1.310 +#endif
1.311 + return both_together();
1.312 +}