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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
#include "stdafx.h"
#include <WinSock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <thread>
#include <string>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define DEFAULT_PORT "80"
#define DEFAULT_BUFFER_LENGTH 2048
void error(char msg[] = "An error occured", SOCKET socket = NULL) {
int e = WSAGetLastError();
closesocket(socket);
std::cout << msg;
std::cout << "\nerror code was: " << e;
WSACleanup();
}
int send_l(SOCKET s, char *host, std::string path) {
int er;
char msg[DEFAULT_BUFFER_LENGTH];
std::string st("GET ");
std::string hostst(host);
st += path;
st += " HTTP/1.1\r\n";
st += "Host: ";
st += hostst;
st += "\r\n\r\n";
strcpy_s(msg, st.c_str());
er = send(s, msg, DEFAULT_BUFFER_LENGTH, NULL);
if (er == SOCKET_ERROR) {
std::cout << "\nerror with sending: your last message was not sent\ntry again\n";
}
memset(msg, '\0', sizeof(msg));
st.clear();
return 0;
}
int recv_l(SOCKET s) {
int er;
char recvstr[DEFAULT_BUFFER_LENGTH];
er = recv(s, recvstr, DEFAULT_BUFFER_LENGTH, NULL);
if (er > 0) {
std::cout << "\n\n___________________________________\n\n" << recvstr;
} else if (er == 0) {
std::cout << "connection terminated\n";
} else {
error("error with recv: the last message was not received\n", s);
return (-1);
}
return 0;
}
int main(int argc, char *argv[]) {
char *host = "";
char *port = DEFAULT_PORT;
int er;
std::string path;
//Large amounts of branching logic for inputs.
if (argc <= 2) {
error("Relaunch the program by typing '-h' and then a VALID host name (default port is 8888, do '-p' to change this).");
return (-1);
} else if (argc == 3) {
if ((strcmp(argv[1], "-h") == 0)) {
host = argv[2];
} else {
error("Relaunch the program by typing '-h' and then a VALID host name (default port is 8888, do '-p' to change this).\n");
return (-1);
}
} else {
error("Please enter valid input.");
return (-1);
}
//declaring the winsock instance
WSADATA WINSOCKYEAA;
er = WSAStartup(MAKEWORD(2, 2), &WINSOCKYEAA);
if (er != 0) {
error("startup failed");
return (-1);
}
//creating the socket
SOCKET bill = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (bill == INVALID_SOCKET) {
error("socket creation failed");
return (-1);
}
addrinfo hints, *result = NULL, *ptr = NULL;
memset(&hints, '\0', sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
er = getaddrinfo(host, port, &hints, &result);
if (er != 0) {
error("getaddrinfo failed (maybe an invalid hostname or port)");
return (-1);
}
std::cout << "connecting to server...\n";
er = connect(bill, result->ai_addr, (int)result->ai_addrlen);
if (er == SOCKET_ERROR) {
error("connection failed with error code ");
return (-1);
}
freeaddrinfo(result);
std::cout << "connected to server\n";
std::cout << "type a path to send to the server\n";
std::cin >> path;
send_l(bill, host, path);
recv_l(bill);
error("\nshutting down", bill);
return 0;
}
|