Debian Codegraph @

examples/fork-test/srt_client.c

raw
1 #include <srt/srt.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <arpa/inet.h>
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/wait.h>
9 #define SERVER_IP "127.0.0.1"
10 #define SERVER_PORT 9000
11 int main() {
12 if (srt_startup() != 0) {
13 fprintf(stderr, "Error initializing SRT.\n");
14 return 1;
15 }
16 SRTSOCKET client_sock = srt_create_socket();
17 if (client_sock == SRT_INVALID_SOCK) {
18 fprintf(stderr, "Error creating a socket: %s\n", srt_getlasterror_str());
19 return 1;
20 }
21 struct sockaddr_in sa;
22 memset(&sa, 0, sizeof sa);
23 sa.sin_family = AF_INET;
24 sa.sin_port = htons(SERVER_PORT);
25 inet_pton(AF_INET, SERVER_IP, &sa.sin_addr);
26 if (srt_connect(client_sock, (struct sockaddr*)&sa, sizeof(sa)) == SRT_ERROR) {
27 fprintf(stderr, "Error: srt_connect: %s\n", srt_getlasterror_str());
28 return 1;
29 }
30 printf("Connected to SRT server %s:%d\n", SERVER_IP, SERVER_PORT);
31 const char* message = "Hello from SRT client!";
32 int bytes = srt_send(client_sock, message, strlen(message));
33 if (bytes == SRT_ERROR) {
34 fprintf(stderr, "Sending error: %s\n", srt_getlasterror_str());
35 } else {
36 printf("Message sent: %s\n", message);
37 }
38
39 while (1)
40 {
41 char buffer[1500];
42 int nb = srt_recv(client_sock, buffer, sizeof(buffer));
43 if (nb <= 0)
44 {
45 printf("Closed from the server !\n");
46 srt_close(client_sock);
47 break;
48 }
49 buffer[nb] = 0;
50 printf("Server has sent: %s\n", buffer);
51 }
52 srt_cleanup();
53 return 0;
54 }
55