18 int bytes_payload_total = 0;
20 while (bytes_payload_total < data_size)
23 &buffer[bytes_payload_total],
24 data_size - bytes_payload_total,
26 std::cout << bytes_payload_count << std::endl;
27 if (bytes_payload_count == -1)
29 perror(
"Error reading Data!");
32 else if (bytes_payload_count == 0)
34 std::cout <<
"Unexpected end of transmission" << std::endl;
37 bytes_payload_total += bytes_payload_count;
55 std::cout <<
"Socket flushed (" << bytes_total / 1024 <<
" kB)" << std::endl;
58 bytes_total += bytes_count;
65 socklen_t addrlen =
sizeof(c_address);
71 void SocketConnector::init_listen(){
72 int bindResult = bind(
rc_socket, (
struct sockaddr*)&address,
sizeof(address));
73 if (bindResult == -1) {
74 handle_socket_errors(
"binding the Socket");
78 if (listenResult == -1) {
79 handle_socket_errors(
"listening on the Socket");
87 void SocketConnector::init_connect(){
89 int error_counter = 0;
94 if (error_counter < 1)
96 handle_socket_errors(
"connecting to Socket");
102 std::cout <<
"Connected by " << inet_ntoa(address.sin_addr) <<
"\n";
112 int error = WSAStartup(0x0202, &w);
120 rc_socket = socket(AF_INET, SOCK_STREAM, 0);
124 handle_socket_errors(
"intitializing Socket");
129 handle_socket_errors(
"setting socket options");
132 address.sin_family = AF_INET;
133 address.sin_addr.s_addr = inet_addr(
ip.c_str());
134 address.sin_port = htons(
port);
147 void SocketConnector::handle_socket_errors(
const std::string &raised_at)
150 FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
151 NULL, WSAGetLastError(),
152 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
153 (LPWSTR)&s, 0, NULL);
154 std::cout <<
"Error occured while " << raised_at <<
"." << std::endl;
155 fprintf(stderr,
"%S\n", s);
166 void SocketConnector::handle_socket_errors(
const std::string &raised_at)
168 std::cout <<
"Error occured while " << raised_at <<
"." << std::endl;
169 std::cout << std::strerror(errno) << std::endl;
int read_data(char *buffer, int data_size)