2 Copyright (c) 2012, Broadcom Europe Ltd
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "containers/net/net_sockets.h"
36 #define MAX_BUFFER_LEN 1024
37 #define MAX_NAME_LEN 256
38 #define MAX_PORT_LEN 32
40 int main(int argc, char **argv)
42 VC_CONTAINER_NET_T *server_sock, *sock;
43 vc_container_net_status_t status;
44 char buffer[MAX_BUFFER_LEN];
45 char name[MAX_NAME_LEN];
47 int ii, connections = 1;
52 printf("Usage:\n%s <port> [<connections>]\n", argv[0]);
56 server_sock = vc_container_net_open(NULL, argv[1], VC_CONTAINER_NET_OPEN_FLAG_STREAM, &status);
59 printf("vc_container_net_open failed: %d\n", status);
65 sscanf(argv[2], "%d", &connections);
68 status = vc_container_net_listen(server_sock, connections);
69 if (status != VC_CONTAINER_NET_SUCCESS)
71 printf("vc_container_net_listen failed: %d\n", status);
72 vc_container_net_close(server_sock);
76 for (ii = 0; ii < connections; ii++)
78 status = vc_container_net_accept(server_sock, &sock);
79 if (status != VC_CONTAINER_NET_SUCCESS)
81 printf("vc_container_net_accept failed: %d\n", status);
82 vc_container_net_close(server_sock);
86 strcpy(name, "<unknown>");
87 vc_container_net_get_client_name(sock, name, sizeof(name));
88 vc_container_net_get_client_port(sock, &port);
89 printf("Connection from %s:%hu\n", name, port);
91 while ((received = vc_container_net_read(sock, buffer, sizeof(buffer) - 1)) != 0)
98 /* Flip case and echo data back to client */
99 for (jj = 0; jj < received; jj++, ptr++)
104 if (isalpha((unsigned char)c))
105 *ptr ^= 0x20; /* Swaps case of ASCII alphabetic characters */
115 sent = vc_container_net_write(sock, ptr, received);
118 status = vc_container_net_status(sock);
119 printf("vc_container_net_write failed: %d\n", status);
123 /* Print out bytes actually sent */
132 status = vc_container_net_status(sock);
134 if (status != VC_CONTAINER_NET_SUCCESS && status != VC_CONTAINER_NET_ERROR_CONNECTION_LOST)
137 vc_container_net_close(sock);
140 if (status != VC_CONTAINER_NET_SUCCESS)
142 printf("vc_container_net_read failed: %d\n", status);
145 vc_container_net_close(server_sock);