Http POST c++ [help]

Good afternoon,

I have issues with this i am new to winsock. so i was learning to use winsock for http POST request

Compiles correctly, but doesnt connect i get the error "Connection Failure" what do you think can be wrong

my code goes like this:
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
#define WIN32_LEAN_AND_MEAN

#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <Winsock2.h>
#include <WS2tcpip.h>
#include <windows.h>

#pragma comment (lib,"ws2_32")

int _tmain(int argc, _TCHAR* argv[])
	{
	  WSADATA wsaData;
	  int iResult = WSAStartup(MAKEWORD(2,2),&wsaData);
	  SOCKET m_socket;
	  m_socket = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
	  if(m_socket==INVALID_SOCKET)
		{
		printf("Invalid Socket :WSAGetLastError()");
			}
	sockaddr_in clientService;
	clientService.sin_family = AF_INET;
	clientService.sin_addr.s_addr = inet_addr("127.0.0.1");
	clientService.sin_port = htons(5209);
	LPHOSTENT host = gethostbyname("72.144.89.32");

	if(connect(m_socket,(SOCKADDR*)&clientService,sizeof(clientService))==SOCKET_ERROR)
	{
	  printf("Connection Failure");
	  WSACleanup();
	  return 1;
	}
	
	char buffer[2048];
	
	strcpy(buffer,"POST /dbarea.php HTTP/1.1\n");
	strcat(buffer,"Content - Type:application/x-www-form-urlencoded\n");
	strcat(buffer,"Host: localost\n");
	strcat(buffer,"content-Length:32\n");
	strcat(buffer,"\n");
	strcat(buffer,"username=emeka1&password=laikan112");
	//int n = write(SOCKADDR*,buffer,strlen(buffer));
	
	printf("Data Sent Successfully..");

	return 0;

		}
Is that port (5209) open and excepting connections on the host you are trying to connect to, i.e. your machine?
I tried to telnet earlier didn't allow it.
I will check and see If the port appears to be open.
Don't forget about your firewall. If it isn't told to allow inbound traffic on that port then it will block it.
Ok I ran netstat-a, saw some open ports when I changed the port from 5209, to 5357, it showed the "data sent successfully, so I check now with php and see if it transfers the data. Thanks a whole lot geek, you are the best.
Topic archived. No new replies allowed.