Socket help required

I'm just trying to write a simple check that we are able to connext to a remote host via an internal proxy server, then send a simple HEAD command and receive the response. I can successfully connect but when I attempt to send the HEAD command the connection is terminated.

If I do the same using OpenSSL and not go through the proxy server all works as it should. I would appreciate if anyone could shed some light on what I maybe doing wrong.

PS I'm on a Windows PC, hence the WinSock initiation.

Here's the code for the simple console test program:

practical.h :
1
2
3
4
#pragma once

void DieWithUserMessage(const char* msg, const char* detail);
void DieWithSystemMessage(const char* msg);


practical.cpp :
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
#include <stdio.h>
#include <stdlib.h>
#include "Practical.h"
#include <WinSock2.h>

void DieWithUserMessage(const char* msg, const char* detail)
{
	fputs(msg, stderr);
	fputs(": ", stderr);
	fputs(detail, stderr);
	fputc('\n', stderr);
	exit(1);
}

void DieWithSystemMessage(const char* msg)
{
	int err_code = WSAGetLastError();

	if (err_code)
	{
		LPSTR err_string(NULL);
		int size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
										0,
										err_code,
										0,
										(LPSTR)&err_string,
										0,
										0);
		fputs(msg, stderr);
		fputs(": ", stderr);
		fputs(err_string, stderr);
		fputc('\n', stderr);

		LocalFree(err_string);
	}
	else
		perror(msg);

	exit(1);
}


main source code :
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;
}
Have you used WSAGetLastError to check the value of numBytes after your send?
Have you used WSAGetLastError to check the value of numBytes after your send?


Yes, see line #74 of main source code and line #17 of practical.cpp

I'm currently getting :

"An existing connection was forcibly closed by the remote host"

I currently don't know whether this is the firewall surounding the proxy server or the target host!
Apologies, I missed that.

I'm unsure about this too. May be a bit above my head as far as networking goes, I'm afraid (basic client-server video game is all I've done).
Topic archived. No new replies allowed.