Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Test of AF_LOCAL datagram sockets, based on Datagram example in GCC.HELP
29 #undef WIN32 // Seems to be included in the X86 baseport somewhere (we don't want winsock as this isn't an emulator build)
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <libc/netinet/in.h>
39 #include <libc/arpa/inet.h>
42 make_named_socket (int port)
44 // struct sockaddr name;
45 struct sockaddr_in name;
49 /* Create the socket. */
51 sock = socket (AF_INET, SOCK_DGRAM, 0);
53 if ( INVALID_SOCKET == sock )
55 printf( "%s,%s,%d","CreateSocket", "socket", WSAGetLastError ( ) );
61 perror ("make_named_socket");
66 /* Bind a name to the socket. */
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);
72 size = sizeof(struct sockaddr);
74 if (bind (sock, (struct sockaddr *) &name, size) < 0)
90 char server_message[MAXMSG];
95 /* Make the socket, then loop endlessly. */
97 server_sock = make_named_socket (SERVER);
98 nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size);
101 perror ("sendto (server)");
106 /* Wait for a datagram. */
107 size = sizeof (name);
108 nbytes = recvfrom (server_sock, server_message, MAXMSG, 0, (struct sockaddr *) & name, &size);
111 perror ("recfrom (server)");
115 /* Give a diagnostic message. */
116 fprintf (stderr, "Server: got message: %s\n", server_message);
118 /* Bounce the message back to the sender. */
119 nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size);
122 perror ("sendto (server)");
125 }/* end of - "while (1)" */
131 #define MESSAGE "Yow!!! Are we having fun yet?!? I'd like a 99 please..."
137 char message[MAXMSG];
138 struct sockaddr_in name;
142 /* Make the socket. */
143 sock = make_named_socket (CLIENT);
145 /* Initialize the server socket address. */
146 name.sin_family = AF_INET;
147 name.sin_port = htons((unsigned short)SERVER);
150 /* Send the datagram. */
151 nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0,
152 (struct sockaddr *) & name, size);
155 perror ("sendto (client)");
159 /* Wait for a reply. */
160 nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
163 perror ("recfrom (client)");
167 /* Print a diagnostic message. */
168 fprintf (stderr, "Client: got message: %s\n", message);
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
187 int both_together(void)
190 char server_message[MAXMSG];
191 struct sockaddr_in name;
195 char message[MAXMSG];
197 /* Make the server socket */
199 server_sock = make_named_socket (SERVER);
201 sock = make_named_socket (CLIENT);
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");
210 /* Send the datagram. */
211 nbytes = sendto (server_sock, MESSAGE, strlen (MESSAGE) + 1, 0,
212 (struct sockaddr *) & name, size);
218 printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) );
224 perror ("sendto (server)");
229 /* more server stuff */
231 size = sizeof (name);
232 nbytes = recvfrom (server_sock, server_message, MAXMSG, 0,
233 (struct sockaddr *) & name, &size);
237 printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) );
243 perror ("recvfrom (server)");
248 /* Give a diagnostic message. */
249 fprintf (stderr, "Server: got message: %s\n", server_message);
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);
258 printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) );
264 perror ("sendto (client)");
270 /* Wait for a reply. */
271 nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0);
275 printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) );
281 perror ("recvfrom (client)");
286 /* Print a diagnostic message. */
287 fprintf (stderr, "Client: got message: %s\n", message);
292 closesocket(server_sock);
301 int main(int argc, char*argv[])
304 WORD wVersionRequested;WSADATA wsaData;int err;
305 wVersionRequested = MAKEWORD( 1, 0 );
306 err = WSAStartup( wVersionRequested, &wsaData );
308 return both_together();