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
|
#include <stdio.h>
#include <stdlib.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include "Practical.h"
int main(int argc, char* argv[])
{
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == 0)
{
char* servIP = "172.19.32.2"; // the IP address for our proxy server
unsigned short servPort = 8080; // on port 8080
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
DieWithSystemMessage("socket() failed");
struct sockaddr_in servAddr;
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
int retval = InetPton(AF_INET, servIP, &servAddr.sin_addr.s_addr);
if (retval ==0)
DieWithUserMessage("InetPton() failed", "invalid address string");
else if (retval < 0)
DieWithSystemMessage("InetPton() failed");
servAddr.sin_port = htons(servPort);
if (connect(sock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0)
DieWithSystemMessage("connect() failed");
{//this connect works
char* cmd = "CONNECT https://secure.dev.gateway.gov.uk:443 HTTP/1.1\r\n\r\n";
int cmd_len = strlen(cmd);
int numBytes = send(sock, cmd, cmd_len, 0);
if (numBytes < 0)
DieWithUserMessage("send()", "sent unexpected number of bytes");
int totalBytesRecvd = 0;
fputs("Received: ", stdout);
while (totalBytesRecvd < cmd_len)
{
char buffer[0x400+1];
memset(buffer, 0, 0x400);
numBytes = recv(sock, buffer, 0x400, 0);
if (numBytes < 0)
DieWithSystemMessage("recv(), failed");
else if (numBytes == 0)
DieWithUserMessage("recv()", "connection closed prematurely");
totalBytesRecvd += numBytes;
buffer[numBytes] = '\0';
fputs(buffer, stdout);
}
fputc('\n', stdout);
}
//////////////////////////////////////////////////////////////////////////////
{// then this send command fails!!!
char cmd[2000];
memset(cmd, 0, 2000);
sprintf_s(cmd, 2000, "HEAD HTTP/1.1\r\nHost: secure.dev.gateway.gov.uk\r\n\r\n");
int cmd_len = strlen(cmd);
int numBytes = send(sock, cmd, cmd_len, 0);
if (numBytes < 0)
DieWithUserMessage("send()", "sent unexpected number of bytes");
int totalBytesRecvd = 0;
fputs("Received: ", stdout);
while (totalBytesRecvd < cmd_len)
{
char buffer[0x400+1];
memset(buffer, 0, 0x400);
numBytes = recv(sock, buffer, 0x400, 0);
if (numBytes < 0)
DieWithSystemMessage("recv(), failed");
else if (numBytes == 0)
DieWithUserMessage("recv()", "connection closed prematurely");
totalBytesRecvd += numBytes;
buffer[numBytes] = '\0';
fputs(buffer, stdout);
}
fputc('\n', stdout);
}
closesocket(sock);
}
WSACleanup();
return 0;
}
|