some work on getting an http response from google.com
This commit is contained in:
@@ -6,19 +6,36 @@
|
||||
#include <cstdio>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef SOCKET Socket;
|
||||
#endif
|
||||
#define DEBUG
|
||||
|
||||
typedef unsigned char byte;
|
||||
|
||||
#ifdef _WIN32
|
||||
typedef SOCKET Socket;
|
||||
bool init_socket_lib() {
|
||||
// TODO: Should do some sort of smart version upgrade via data
|
||||
// See https://learn.microsoft.com/en-us/windows/win32/api/winsock/ns-winsock-wsadata
|
||||
WSADATA data;
|
||||
if (WSAStartup((2 << 8) | 2, &data) != 0) {
|
||||
printf("init_socket_lib failed: %d\n", WSAGetLastError());
|
||||
WSACleanup();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
extern bool socket_lib_initialized = init_socket_lib();
|
||||
|
||||
namespace tcp {
|
||||
|
||||
Socket make_socket();
|
||||
bool connect(Socket& s, const char* hostname, int port);
|
||||
bool connect(Socket &s, const char* hostname, int port);
|
||||
bool send_transmit_stop(Socket &s);
|
||||
bool send_read_stop(Socket &s);
|
||||
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);
|
||||
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
|
||||
|
||||
@@ -56,6 +73,8 @@ bool connect(Socket& s, const char *hostname, int port) {
|
||||
if (code == SOCKET_ERROR) {
|
||||
closesocket(s);
|
||||
s = INVALID_SOCKET;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
ptr = ptr->ai_next;
|
||||
} while (ptr);
|
||||
@@ -65,12 +84,32 @@ bool connect(Socket& s, const char *hostname, int port) {
|
||||
if (s == INVALID_SOCKET) {
|
||||
printf("Unable to connect to %s:%d\n", hostname, port);
|
||||
WSACleanup();
|
||||
return 1;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool send_transmit_stop(Socket &s) {
|
||||
int code = shutdown(s, SD_SEND);
|
||||
if (code == SOCKET_ERROR) {
|
||||
printf("send_transmit_stop failed: %d\n", WSAGetLastError());
|
||||
closesocket(s);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool send_read_stop(Socket &s) {
|
||||
int code = shutdown(s, SD_RECEIVE);
|
||||
if (code == SOCKET_ERROR) {
|
||||
printf("send_read_stop failed: %d\n", WSAGetLastError());
|
||||
closesocket(s);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool disconnect(Socket &s) {
|
||||
int code = shutdown(s, SD_BOTH);
|
||||
if (code == SOCKET_ERROR) {
|
||||
|
||||
Reference in New Issue
Block a user