sl@0: /* sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Test of AF_LOCAL datagram sockets, based on Datagram example in GCC.HELP sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef __X86GCC__ sl@0: #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: #endif sl@0: sl@0: #ifdef WIN32 sl@0: #include sl@0: #else sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #endif sl@0: int sl@0: make_named_socket (int port) sl@0: { sl@0: // struct sockaddr name; sl@0: struct sockaddr_in name; sl@0: int sock; sl@0: size_t size; sl@0: sl@0: /* Create the socket. */ sl@0: sl@0: sock = socket (AF_INET, SOCK_DGRAM, 0); sl@0: #ifdef WIN32 sl@0: if ( INVALID_SOCKET == sock ) sl@0: { sl@0: printf( "%s,%s,%d","CreateSocket", "socket", WSAGetLastError ( ) ); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #else sl@0: if (-1 == sock ) sl@0: { sl@0: perror ("make_named_socket"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #endif sl@0: sl@0: /* Bind a name to the socket. */ sl@0: sl@0: name.sin_family = AF_INET; sl@0: name.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); sl@0: name.sin_port = htons((unsigned short)port); sl@0: sl@0: size = sizeof(struct sockaddr); sl@0: sl@0: if (bind (sock, (struct sockaddr *) &name, size) < 0) sl@0: { sl@0: perror ("bind"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: sl@0: return sock; sl@0: } sl@0: sl@0: #define SERVER 99 sl@0: #define MAXMSG 512 sl@0: sl@0: sl@0: int server(void) sl@0: { sl@0: int server_sock; sl@0: char server_message[MAXMSG]; sl@0: struct sockaddr name; sl@0: size_t size; sl@0: int nbytes = -1; sl@0: sl@0: /* Make the socket, then loop endlessly. */ sl@0: sl@0: server_sock = make_named_socket (SERVER); sl@0: nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size); sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("sendto (server)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: while (1) sl@0: { sl@0: /* Wait for a datagram. */ sl@0: size = sizeof (name); sl@0: nbytes = recvfrom (server_sock, server_message, MAXMSG, 0, (struct sockaddr *) & name, &size); sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("recfrom (server)"); sl@0: break; sl@0: } sl@0: sl@0: /* Give a diagnostic message. */ sl@0: fprintf (stderr, "Server: got message: %s\n", server_message); sl@0: sl@0: /* Bounce the message back to the sender. */ sl@0: nbytes = sendto (server_sock, server_message, nbytes, 0, (struct sockaddr *) & name, size); sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("sendto (server)"); sl@0: break; sl@0: } sl@0: }/* end of - "while (1)" */ sl@0: exit (EXIT_FAILURE); sl@0: return 0; sl@0: } sl@0: sl@0: #define CLIENT 125 sl@0: #define MESSAGE "Yow!!! Are we having fun yet?!? I'd like a 99 please..." sl@0: sl@0: int sl@0: client (void) sl@0: { sl@0: int sock; sl@0: char message[MAXMSG]; sl@0: struct sockaddr_in name; sl@0: size_t size; sl@0: int nbytes; sl@0: sl@0: /* Make the socket. */ sl@0: sock = make_named_socket (CLIENT); sl@0: sl@0: /* Initialize the server socket address. */ sl@0: name.sin_family = AF_INET; sl@0: name.sin_port = htons((unsigned short)SERVER); sl@0: size = sizeof(name); sl@0: sl@0: /* Send the datagram. */ sl@0: nbytes = sendto (sock, MESSAGE, strlen (MESSAGE) + 1, 0, sl@0: (struct sockaddr *) & name, size); sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("sendto (client)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: sl@0: /* Wait for a reply. */ sl@0: nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0); sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("recfrom (client)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: sl@0: /* Print a diagnostic message. */ sl@0: fprintf (stderr, "Client: got message: %s\n", message); sl@0: sl@0: /* Clean up. */ sl@0: #ifdef WIN32 sl@0: closesocket (sock); sl@0: #else sl@0: close(sock); sl@0: #endif sl@0: return 0; sl@0: } sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-1046 sl@0: @SYMTestCaseDesc Tests for AF_LOCAL datagram sockets sl@0: @SYMTestPriority High sl@0: @SYMTestActions Initialize the server socket address,send and receive messages on the socket. sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: int both_together(void) sl@0: { sl@0: int server_sock; sl@0: char server_message[MAXMSG]; sl@0: struct sockaddr_in name; sl@0: size_t size; sl@0: int nbytes; sl@0: int sock; sl@0: char message[MAXMSG]; sl@0: sl@0: /* Make the server socket */ sl@0: sl@0: server_sock = make_named_socket (SERVER); sl@0: sl@0: sock = make_named_socket (CLIENT); sl@0: sl@0: /* Initialize the server socket address. */ sl@0: name.sin_family = AF_INET; sl@0: //name.sin_port = SERVER; sl@0: name.sin_port = htons((unsigned short)SERVER); sl@0: name.sin_addr.S_un.S_addr=inet_addr("127.0.0.1"); sl@0: size = sizeof(name); sl@0: sl@0: /* Send the datagram. */ sl@0: nbytes = sendto (server_sock, MESSAGE, strlen (MESSAGE) + 1, 0, sl@0: (struct sockaddr *) & name, size); sl@0: sl@0: sl@0: #ifdef WIN32 sl@0: if (nbytes < 0) sl@0: { sl@0: printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) ); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #else sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("sendto (server)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #endif sl@0: { sl@0: /* more server stuff */ sl@0: sl@0: size = sizeof (name); sl@0: nbytes = recvfrom (server_sock, server_message, MAXMSG, 0, sl@0: (struct sockaddr *) & name, &size); sl@0: #ifdef WIN32 sl@0: if (nbytes < 0) sl@0: { sl@0: printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) ); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #else sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("recvfrom (server)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #endif sl@0: sl@0: /* Give a diagnostic message. */ sl@0: fprintf (stderr, "Server: got message: %s\n", server_message); sl@0: sl@0: /* Bounce the message back to the sender. */ sl@0: name.sin_port = htons((unsigned short)CLIENT); sl@0: nbytes = sendto (sock, server_message, nbytes, 0, sl@0: (struct sockaddr *) & name, size); sl@0: #ifdef WIN32 sl@0: if (nbytes < 0) sl@0: { sl@0: printf( "%s,%s,%d","sendto", "socket", WSAGetLastError ( ) ); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #else sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("sendto (client)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: /* Wait for a reply. */ sl@0: nbytes = recvfrom (sock, message, MAXMSG, 0, NULL, 0); sl@0: #ifdef WIN32 sl@0: if (nbytes < 0) sl@0: { sl@0: printf( "%s,%s,%d","recvfrom", "socket", WSAGetLastError ( ) ); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #else sl@0: if (nbytes < 0) sl@0: { sl@0: perror ("recvfrom (client)"); sl@0: exit (EXIT_FAILURE); sl@0: } sl@0: #endif sl@0: sl@0: /* Print a diagnostic message. */ sl@0: fprintf (stderr, "Client: got message: %s\n", message); sl@0: sl@0: /* Clean up. */ sl@0: #ifdef WIN32 sl@0: closesocket (sock); sl@0: closesocket(server_sock); sl@0: #else sl@0: close(sock); sl@0: close(server_sock); sl@0: #endif sl@0: sl@0: return 0; sl@0: } sl@0: sl@0: int main(int argc, char*argv[]) sl@0: { sl@0: #ifdef WIN32 sl@0: WORD wVersionRequested;WSADATA wsaData;int err; sl@0: wVersionRequested = MAKEWORD( 1, 0 ); sl@0: err = WSAStartup( wVersionRequested, &wsaData ); sl@0: #endif sl@0: return both_together(); sl@0: }