include stdio include sys socket include arpa inet include string incl

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <array>
#include <vector>
#include <iostream>
int main() {
int sock = 0;
struct sockaddr_in serv_addr;
std::string streamId = "stream1";
std::string initMsg = "publisher " + streamId + "\r\n"; // \r\n пульт хочет;
char *hello = "publisher stream1\r\n";
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(8085);
if(inet_pton(AF_INET, "192.168.38.94", &serv_addr.sin_addr)<=0) {
printf("\nInvalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("\nConnection Failed \n");
return -1;
}
//send(sock , hello , strlen(hello) , 0 );
std::cout << initMsg.size() << " " << strlen(hello) << "\n";
// send(sock , initMsg.data() , initMsg.size() , 0 );
send(sock , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
while(1) {
const int size = 4096;
std::size_t len;
std::array<uint8_t, size> buf{};
std::vector<uint8_t> input;
FILE *file = fopen("/home/keyliaran/file_example_OOG_2MG.ogg", "rb");
while ((len = std::fread(buf.data(), sizeof(buf[0]), buf.size(), file)) > 0) {
if (std::ferror(file) && !std::feof(file))
return -1;
input.insert(input.end(), buf.data(), buf.data() + len);
}
fclose(file);
int writed_len = 0;
while (writed_len < input.size()) {
ssize_t sended = 0;
if(input.size() - writed_len > 2048) {
sended = send(sock, input.data() + writed_len, 2048, 0);
} else {
sended = send(sock, input.data() + writed_len, input.size() - writed_len, 0);
}
writed_len += sended;
printf("Sended %d\n", sended);
}
}
return 0;
}