include stdafx include stdio include WinSock2 include string include f

 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
#include "stdafx.h"
#include <stdio.h>
#include <WinSock2.h>
#include <string.h>
#include <fstream>
#include <array>
#include <vector>
using namespace Win32;
int main() {
WSADATA wsa;
SOCKET _socket;
char *hello = "publisher stream2\r\n";
if (::WSAStartup(MAKEWORD(2, 2), &wsa) != 0)
throw std::exception("WSAStartup failed", ::WSAGetLastError());
if ((_socket = ::socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
throw std::exception("Could not create socket", ::WSAGetLastError());
struct sockaddr_in server;
server.sin_addr.s_addr = ::inet_addr("192.168.36.3");
server.sin_family = AF_INET;
server.sin_port = ::htons(8085);
if (::connect(_socket, reinterpret_cast<struct sockaddr *>(&server), sizeof(server)) == SOCKET_ERROR)
throw std::exception("Connect error");
if (::send(_socket, connectMsg.c_str(), static_cast<int>(connectMsg.length()), 0) == SOCKET_ERROR)
throw std::exception("Send failed");
while(1) {
const int size = 4096;
std::size_t len;
std::array<uint8_t, size> buf{};
std::vector<uint8_t> input;
FILE *file = fopen("example.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(_socket, input.data() + writed_len, 2048, 0);
} else {
sended = ::send(_socket, input.data() + writed_len, input.size() - writed_len, 0);
}
writed_len += sended;
printf("Sended %d\n", sended);
}
}
return 0;
}