Copy over some MSDN code into tcp_client.h. Beginning of project
This commit is contained in:
110
inc/tcp_client.h
Normal file
110
inc/tcp_client.h
Normal file
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <ws2tcpip.h>
|
||||
#include <winsock2.h>
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef SOCKET Socket;
|
||||
#endif
|
||||
|
||||
typedef unsigned char byte;
|
||||
|
||||
namespace tcp {
|
||||
|
||||
Socket make_socket();
|
||||
bool connect(Socket& s, const char* hostname, int port);
|
||||
bool disconnect(Socket &s);
|
||||
bool send_data(Socket& s, byte* buf, int len);
|
||||
bool get_data(Socket& s, byte* out_buf, int buf_size, int& out_num_written);
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
Socket make_socket() { return INVALID_SOCKET; }
|
||||
|
||||
bool connect(Socket& s, const char *hostname, int port) {
|
||||
addrinfo *result, *ptr, hints;
|
||||
ZeroMemory(&hints, sizeof(hints));
|
||||
hints.ai_family = AF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
char port_str[128];
|
||||
sprintf(port_str, "%d", port);
|
||||
int code = getaddrinfo(hostname, port_str, &hints, &result);
|
||||
if (code != 0) {
|
||||
printf("getaddrinfo failed: %d\n", code);
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
|
||||
s = INVALID_SOCKET;
|
||||
ptr = result;
|
||||
s = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
|
||||
if (s == INVALID_SOCKET) {
|
||||
printf("Error at socket(): %ld\n", WSAGetLastError());
|
||||
freeaddrinfo(result);
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
|
||||
// Iterate through the linked list of addresses returned by getaddrinfo
|
||||
do {
|
||||
code = connect(s, ptr->ai_addr, (int)ptr->ai_addrlen);
|
||||
if (code == SOCKET_ERROR) {
|
||||
closesocket(s);
|
||||
s = INVALID_SOCKET;
|
||||
}
|
||||
ptr = ptr->ai_next;
|
||||
} while (ptr);
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
if (s == INVALID_SOCKET) {
|
||||
printf("Unable to connect to %s:%d\n", hostname, port);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool disconnect(Socket &s) {
|
||||
int code = shutdown(s, SD_BOTH);
|
||||
if (code == SOCKET_ERROR) {
|
||||
printf("disconnect failed: %d\n", WSAGetLastError());
|
||||
closesocket(s);
|
||||
WSACleanup();
|
||||
}
|
||||
return code != SOCKET_ERROR;
|
||||
}
|
||||
|
||||
// TODO: Retries? Should this be a param?
|
||||
bool send_data(Socket &s, byte *buf, int len) {
|
||||
int code = send(s, (char*)buf, len, 0);
|
||||
if (code == SOCKET_ERROR) {
|
||||
printf("send_data failed: %d\n", WSAGetLastError());
|
||||
closesocket(s);
|
||||
WSACleanup();
|
||||
}
|
||||
return code != SOCKET_ERROR;
|
||||
}
|
||||
|
||||
bool get_data(Socket &s, byte *out_buf, int buf_size, int &out_num_written) {
|
||||
int code = recv(s, (char*)out_buf, buf_size, 0);
|
||||
if (code >= 0) {
|
||||
out_num_written = code;
|
||||
} else {
|
||||
printf("get_data failed: %d\n", WSAGetLastError());
|
||||
}
|
||||
return code >= 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace tcp
|
||||
|
||||
|
||||
Reference in New Issue
Block a user