MCPcopy Create free account
hub / github.com/dermesser/libsocket / main

Function main

examples/echo_dgram_server.c:19–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17 */
18
19int main(void) {
20 int sfd, bytes, ret;
21 char src_host[128], src_service[7], buf[16];
22
23 src_host[127] = 0;
24 src_service[6] = 0;
25
26 sfd = create_inet_server_socket("0.0.0.0", "1234", LIBSOCKET_UDP,
27 LIBSOCKET_IPv4, 0);
28
29 if (-1 == sfd) {
30 perror("couldn't create server\n");
31 exit(1);
32 }
33
34 printf("Socket up and running\n");
35
36 while (1) {
37 memset(buf, 0, 16);
38 ret = bytes = recvfrom_inet_dgram_socket(
39 sfd, buf, 15, src_host, 127, src_service, 6, 0, LIBSOCKET_NUMERIC);
40
41 if (ret < 0) {
42 perror(0);
43 exit(1);
44 }
45
46 ret =
47 sendto_inet_dgram_socket(sfd, buf, bytes, src_host, src_service, 0);
48
49 if (ret < 0) {
50 perror(0);
51 exit(1);
52 }
53
54 printf("Connection from %s port %s: %s (%i)\n", src_host, src_service,
55 buf, bytes);
56 printf("Connection processed\n");
57 }
58
59 ret = destroy_inet_socket(sfd);
60
61 if (ret < 0) {
62 perror(0);
63 exit(1);
64 }
65
66 return 0;
67}

Callers

nothing calls this directly

Calls 4

sendto_inet_dgram_socketFunction · 0.85
destroy_inet_socketFunction · 0.85

Tested by

no test coverage detected