Debian Codegraph @

examples/test-c-server.c

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2017 Haivision Systems Inc.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, see <http://www.gnu.org/licenses/>
17 */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21
22
23 #include "srt.h"
24
25 int main(int argc, char** argv)
26 {
27 int ss, st;
28 struct sockaddr_in sa;
29 int yes = 1;
30 struct sockaddr_storage their_addr;
31
32 if (argc != 3) {
33 fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
34 return 1;
35 }
36
37 printf("srt startup\n");
38 srt_startup();
39
40 printf("srt socket\n");
41 ss = srt_create_socket();
42 if (ss == SRT_ERROR)
43 {
44 fprintf(stderr, "srt_socket: %s\n", srt_getlasterror_str());
45 return 1;
46 }
47
48 printf("srt bind address\n");
49 sa.sin_family = AF_INET;
50 sa.sin_port = htons(atoi(argv[2]));
51 if (inet_pton(AF_INET, argv[1], &sa.sin_addr) != 1)
52 {
53 return 1;
54 }
55
56 printf("srt setsockflag\n");
57 if (SRT_ERROR == srt_setsockflag(ss, SRTO_RCVSYN, &yes, sizeof yes))
58 {
59 fprintf(stderr, "srt_setsockflag: %s\n", srt_getlasterror_str());
60 return 1;
61 }
62
63 printf("srt bind\n");
64 st = srt_bind(ss, (struct sockaddr*)&sa, sizeof sa);
65 if (st == SRT_ERROR)
66 {
67 fprintf(stderr, "srt_bind: %s\n", srt_getlasterror_str());
68 return 1;
69 }
70
71 printf("srt listen\n");
72 st = srt_listen(ss, 2);
73 if (st == SRT_ERROR)
74 {
75 fprintf(stderr, "srt_listen: %s\n", srt_getlasterror_str());
76 return 1;
77 }
78
79 printf("srt accept\n");
80 int addr_size = sizeof their_addr;
81 int their_fd = srt_accept(ss, (struct sockaddr *)&their_addr, &addr_size);
82 if (their_fd == SRT_INVALID_SOCK)
83 {
84 fprintf(stderr, "srt_accept: %s\n", srt_getlasterror_str());
85 return 1;
86 }
87
88 int i;
89 for (i = 0; i < 100; i++)
90 {
91 printf("srt recvmsg #%d... ",i);
92 char msg[2048];
93 st = srt_recvmsg(their_fd, msg, sizeof msg);
94 if (st == SRT_ERROR)
95 {
96 fprintf(stderr, "srt_recvmsg: %s\n", srt_getlasterror_str());
97 goto end;
98 }
99
100 printf("Got msg of len %d << %s\n", st, msg);
101 }
102
103 end:
104 printf("srt close\n");
105 st = srt_close(ss);
106 if (st == SRT_ERROR)
107 {
108 fprintf(stderr, "srt_close: %s\n", srt_getlasterror_str());
109 return 1;
110 }
111
112 printf("srt cleanup\n");
113 srt_cleanup();
114 return 0;
115 }