11 Replies - 2104 Views - Last Post: 11 March 2010 - 05:48 AM Rate Topic: -----

#1 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Windows RAW socket Failure

Posted 11 March 2010 - 02:57 AM

I have been working on this code for a few hours & am pulling my hair out. On one machine it returns 400, but on another the socket connect() returns negative one. ARG!

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define DEBUG 1
#pragma comment(lib,"ws2_32.lib")

SOCKET sock;
SOCKADDR_IN i_sock;
WSADATA Data;

void NetPError(void) {
	switch(WSAGetLastError()) {
		case WSANOTINITIALISED:
			printf("A successful WSAStartup call must occur before using this function.\n");
			break;
		case WSAENETDOWN:
			printf("The network subsystem has failed.\n");
			break;
		case WSAEFAULT:
			printf("The buf parameter is not completely contained in a valid part of the user address space.\n");
			break;
		case WSAENOTCONN:
			printf("The socket is not connected.\n");
			break;
		case WSAEINTR:
			printf("The (blocking) call was canceled through WSACancelBlockingCall.\n");
			break;
		case WSAEINPROGRESS:
			printf("A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.\n");
			break;
		case WSAESHUTDOWN:
			printf("The socket has been shut down.\n");
			break;
		case WSAEMSGSIZE:
			printf("The message was too large to fit into the specified buffer and was truncated.\n");
			break;
		case WSAETIMEDOUT:
			printf("The connection has been dropped because of a network failure or because the peer system failed to respond.\n");
			break;
		default:
		printf("Unknown failure.\n");
	}
}

int Connect(char *Host, char *Url, int Port) {
	char RecvBuf[2048]={0}, SendBuf[2048]={0}, HTMLSource[2048]={0};
	int ss=0, i=0;
	struct sockaddr_in addr;
	struct hostent *hp;
	memset((char *) &addr, '\0', sizeof(addr));
	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = inet_addr(Host);
	addr.sin_port= htons(Port);

	WSAStartup(MAKEWORD(2,2), &Data);
	sock=socket(AF_INET,SOCK_STREAM,0);
	if(sock!=INVALID_SOCKET) {
		i_sock.sin_family = AF_INET;
		i_sock.sin_addr.s_addr = inet_ntoa(addr.sin_addr);
        if ((hp = gethostbyname(Host)) != NULL) {
                if (hp->h_length > sizeof(addr.sin_addr)) {
                        hp->h_length = sizeof(addr.sin_addr); 
                 }
                 memcpy((char *) &addr.sin_addr, hp->h_addr, hp->h_length);
                 printf("1.) %s : %s\n",Host,inet_ntoa(addr.sin_addr));
        }
        else {
                if ((addr.sin_addr.s_addr = inet_addr(Host)) < 0) {
                        return 0;
                 }
                 else {
	                 printf("2.) %s : %s\n",Host,inet_ntoa(addr.sin_addr));
             	}
        }
		i_sock.sin_port = htons(Port);
		ss = connect(sock, (struct sockaddr *)&i_sock, sizeof(i_sock));
		if(ss<0) {
			printf("Connection failed : %i\n",ss);
			NetPError();
			return 0;
		}
		sprintf(Url,"http://%s/index.html",Host);
		sprintf(SendBuf, "GET %s\r\nHTTP/1.0 Host: http://%s\r\n", Url, Host);
		printf("%s\n",SendBuf);

		if(send(sock, SendBuf, sizeof(SendBuf), 0)<0) {
			printf("Unable to trasmit : %i\n",ss);
			NetPError();
			return 0;
		}
		if(recv(sock, RecvBuf, 2048, 0)<0) {
			printf("Unable to recieve\n");
			NetPError();
			return 0;
		}
		closesocket(sock);
		printf("%s\n",RecvBuf);
		return 1;
	}
	return 1;
}
	
int main(void) {
	char website[32]="example.com"; //akroncdnr.com
	if(Connect(website, "index.html", 80)) {
	//if(Connect("66.93.12.16", "index.html", 80)) {
		printf("\nCompleted!\n");
	}
	return 0;
}


Quote

1.) example.com : 192.0.32.10
Connection failed : -1
Unknown failure.


Quote

1.) example.com : 192.0.32.10
GET http://example.com/index.html
HTTP/1.0 Host: http://example.com

HTTP/1.0 400 Bad Request
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 74

<TITLE>Error</TITLE>
<BODY>
<H1>Error</H1>
Invalid HTTP request.</BODY>

ple.com/index.html


What am I missing? Before today I thought I knew how to do this :P

Is This A Good Question/Topic? 0
  • +

Replies To: Windows RAW socket Failure

#2 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:01 AM

Well, you have a malformed HTTP request :P. Assuming you have connected to www.example.com using TCP:
GET /index.html HTTP/1.0
Host: www.example.com
You will receive the contents of www.example.com/index.html.
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:07 AM

Correcting the HTTP request doesn't change a thing (maybe I'm doing it wrong?)

		//sprintf(Url,"http://%s/index.html",Host);
		sprintf(SendBuf, "GET /%s\r\nHTTP/1.0 Host: http://%s\r\n", Url, Host);


Gives the following output :

Quote

1.) example.com : 192.0.32.10
GET /index.html
HTTP/1.0 Host: http://example.com

HTTP/1.0 400 Bad Request
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 74

<TITLE>Error</TITLE>
<BODY>
<H1>Error</H1>
Invalid HTTP request.</BODY>


Completed!


& altered it again to make it just like you show :

Quote

1.) example.com : 192.0.32.10
GET /index.html HTTP/1.0
Host: http://example.com

HTTP/1.0 400 Bad Request
Pragma: no-cache
Cache-Control: no-cache
Content-Type: text/html
Content-Length: 74

<TITLE>Error</TITLE>
<BODY>
<H1>Error</H1>
Invalid HTTP request.</BODY>


Completed!

Was This Post Helpful? 0
  • +
  • -

#4 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:07 AM

I know it's not the problem, but it will be! The issue is something about the network (if the computers are on separate connections) or the PC itself.

Here's what I would send:
sprintf(SendBuf, "GET /%s HTTP/1.0\r\n Host: www.%s\r\n", Url, Host);
Resulting in:

Quote

GET /index.html HTTP/1.0
Host: www.example.com
Don't use http:// for the host; use www.example.com. The www is (probably) important.

This post has been edited by PlasticineGuy: 11 March 2010 - 03:08 AM

Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:10 AM

View PostPlasticineGuy, on 11 March 2010 - 04:07 AM, said:

I know it's not the problem, but it will be! The issue is something about the network (if the computers are on separate connections) or the PC itself.

I thought this myself, because like I said it was working yesterday, & today I get different results on separate machines. One doesn't even connect to the socket.

Maybe I'll just sleep on it :P Driving me batty.
Was This Post Helpful? 0
  • +
  • -

#6 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:12 AM

Does everything else on the non-working computer work?

EDIT: I thought you were a Linux fan? :P

This post has been edited by PlasticineGuy: 11 March 2010 - 03:13 AM

Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:14 AM

View PostPlasticineGuy, on 11 March 2010 - 04:12 AM, said:

Does everything else on the non-working computer work?

Yes.

View PostPlasticineGuy, on 11 March 2010 - 04:12 AM, said:

EDIT: I thought you were a Linux fan? :P

People pay for development on both OSes. I am a bigger fan of Benjamin Franklyn than I am Linus Torvalds
Was This Post Helpful? 0
  • +
  • -

#8 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:16 AM

Your code doesn't even compile on my compiler.
1>------ Build started: Project: , Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\helen\documents\visual studio 2008\projects\\\main.cpp(54) : warning C4244: 'argument' : conversion from 'int' to 'u_short', possible loss of data
1>c:\users\helen\documents\visual studio 2008\projects\\\main.cpp(60) : error C2440: '=' : cannot convert from 'char *' to 'ULONG'
1>        There is no context in which this conversion is possible
1>c:\users\helen\documents\visual studio 2008\projects\\\main.cpp(76) : warning C4244: 'argument' : conversion from 'int' to 'u_short', possible loss of data
1>Build log was saved at "file://c:\Users\helen\Documents\Visual Studio 2008\Projects\\\Debug\BuildLog.htm"
1> - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The warnings are not an issue (result of maximum warning level), but what's with the error?

EDIT: Using my std::string-to-double (yes I know, I'm mixing C++) I got it to compile, and I got unknown failure. I have managed to write a page downloader before and it worked.

EDIT 2: I changed it to C compilation and it fixed the error (weird). It starts connecting and lags a while before exiting randomly.

This post has been edited by PlasticineGuy: 11 March 2010 - 03:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:20 AM

I'm using Borland 5.5

It's weird because I pulled this code from a project I was working on yesterday, & the only difference (in the socket code) is that I am using domain names rather than straight IP addresses. Normally I would give up & just use what works, but for one the code works (I thought) as it was & two the socket doesn't even connect. Hrm. I was hoping that someone would look at it & say "You big dumby... look'et here!". :P

Line 54 is the Port, so it could be removed, & Line 60 is the converted domain name to ip address (inet_ntoa function).
Was This Post Helpful? 0
  • +
  • -

#10 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:25 AM

On my computer, running as C:

Quote

1.) example.com : 192.0.32.10
Connection failed : -1
The connection has been dropped because of a network failure or because the peer system failed to respond.


I hope you realise that this line i_sock.sin_addr.s_addr = inet_ntoa(addr.sin_addr); is implicitly converting a char* to an unsigned long int; is this what you want?

This post has been edited by PlasticineGuy: 11 March 2010 - 03:33 AM

Was This Post Helpful? 1
  • +
  • -

#11 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Windows RAW socket Failure

Posted 11 March 2010 - 03:33 AM

I don't know. It was host & I thought it needed to be the ip address. I'll mess w/ it. Thanks :)
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Windows RAW socket Failure

Posted 11 March 2010 - 05:48 AM

View PostPlasticineGuy, on 11 March 2010 - 04:25 AM, said:

I hope you realise that this line i_sock.sin_addr.s_addr = inet_ntoa(addr.sin_addr); is implicitly converting a char* to an unsigned long int; is this what you want?

It's corrected by forcing it with a string :

i_sock.sin_addr.s_addr = inet_addr("66.93.12.164");

So the question is, how can I automatically get the ip address before the socket?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1