11 Replies - 1012 Views - Last Post: 04 January 2010 - 02:28 PM Rate Topic: -----

#1 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Interesting/Annoying socket problem

Posted 03 January 2010 - 04:14 AM

Lately I have been making a socket class to handle all of the gritty stuff to make it easier for me when I need to use sockets. It is mostly working, apart from a single function, which IMO should work fine.

string ClientSocket::Read(int Length)
{
	char Buffer[Length];
	int Check = recv(this->Socket, Buffer, Length, NULL);
	Buffer[Check] = '\0';
	return Buffer;
}



This method should work, but it returns the data, with the first 4 bytes missing for a reason I cannot work out.

Please note that I did have error handling for sockets, but took it out for the sake of this post, I know that that isn't the problem.

Does anybody know what is going on?

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Interesting/Annoying socket problem

#2 Guest_c.user*


Reputation:

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 09:56 AM

perhaps you have read them before the call

can recv return a negative value on your system ?
Buffer[Check] = '\0';
if so you will get an out of bounds error

This post has been edited by c.user: 03 January 2010 - 09:58 AM

Was This Post Helpful? 0

#3 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 11:18 AM

Yeah it can, and I did have the code to check for all of the checking, but I did take it out for the sake of this post. I'm pretty sure it has nothing to do with that, but it is pretty bizarre, because sometimes it works as well. I'm really confused with it if I be honest.
Was This Post Helpful? 0
  • +
  • -

#4 Guest_c.user*


Reputation:

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 12:39 PM

where do you call it, how do you call it
print out the Check while the program works
Was This Post Helpful? 0

#5 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 02:46 PM

View Postc.user, on 3 Jan, 2010 - 11:39 AM, said:

where do you call it, how do you call it
print out the Check while the program works


The full code for the function is:
string ClientSocket::Read(int Length)
{
	char Buffer[Length];
	int Check = read(this->Socket, Buffer, Length);
	if(Check == -1)
	{
		this->SetErrorFlag(errno);
		return "";
	}
	else if(Check == 0)
	{
		this->SetErrorFlag(errno);
		return "";
	}
	/* Added this part to check */
	cout << "Check = " << Check << endl;
	/***************************/
	
	Buffer[Check] = '\0';
	string Ret = Buffer;
	return Ret;
}



It is called like this:
ClientSocket *Socket = new ClientSocket(argv[1], Port);
Socket->Write("GET / HTTP/1.0\r\n\r\n");
while(Socket->Connected())
{
	if(Socket->DataWaiting())
	{
		string Data = Socket->Read(1024);
		cout << Data << endl; 
	}
}



and the output of `./Get google.co.uk' is:
Check = 809																  
/1.0 302 Found															   
Location: http://www.google.co.uk/										   
Cache-Control: private													   
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=8f48495d373ea4c0:TM=1262555132:LM=1262555132:S=v0gwQOcfdxPhhfJJ; expires=Tue, 03-Jan-2012 21:45:32 GMT; path=/; domain=.google.com
Set-Cookie: NID=30=hCqkYpUwGEtJCjZRMeJ7Ff5r5jZRON0JOHeUYDBEYu69kOch9HaTRNRf9gP0beKItN8sjOvCCNvbWtzdygn9f2zL_mrsFzSV8ghbFWwonDhi41ibrg2z9bhSo0cPATZN; expires=Mon, 05-Jul-2010 21:45:32 GMT; path=/; domain=.google.com; HttpOnly
Date: Sun, 03 Jan 2010 21:45:32 GMT
Server: gws
Content-Length: 221
X-XSS-Protection: 0

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.co.uk/">here</A>.
</BODY></HTML>



As you can see, the HTTP part of the response is missing.
Was This Post Helpful? 0
  • +
  • -

#6 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 02:53 PM

Sorry for the double post, but I thought I should add that the function sometimes returns the full data, but very rarely...
Was This Post Helpful? 0
  • +
  • -

#7 Guest_c.user*


Reputation:

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 03:43 PM

#include <sys/socket.h>

...

int recv(int s, void *buf, size_t len, int flags);

try this

int Check = recv(this->Socket, (void *) Buffer, Length, 0);

This post has been edited by c.user: 03 January 2010 - 03:44 PM

Was This Post Helpful? 0

#8 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 04:18 PM

View Postc.user, on 3 Jan, 2010 - 02:43 PM, said:

#include <sys/socket.h>

...

int recv(int s, void *buf, size_t len, int flags);

try this

int Check = recv(this->Socket, (void *) Buffer, Length, 0);



Tried that, but to no avail. I'm fairly sure I will be doing something stupid along the line, but I cannot find it at all :/

Thanks for the replies.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_c.user*


Reputation:

Re: Interesting/Annoying socket problem

Posted 03 January 2010 - 04:22 PM

did you try telnet ?

also there could be something like cout << '\r';
check this out in the method

	/* Added this part to check */
	cout << "Check = " << Check << endl;
	/***************************/
	
	Buffer[Check] = '\0';

	cout << Buffer[0]
		 << Buffer[1]
		 << Buffer[2]
		 << Buffer[3]
		 << Buffer[4]
		 << endl;


This post has been edited by c.user: 03 January 2010 - 04:32 PM

Was This Post Helpful? 0

#10 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Interesting/Annoying socket problem

Posted 04 January 2010 - 09:04 AM

Adding that gives the output:
Check = 809																  
/1.0  


Webpage data here...



EDIT:
Interestingly, on the rare times that the whole of the webpage is transferred, recv reports that it has read 813 bytes, and when it returns the data with 4 bytes from the start missing, recv returns that it has read 809 bytes. So the only answer I could come up with is that I am inadvertently reading the data somewhere else and not using it.

EDIT 2:
I have found the problem, it was when I was checking if the connection was still present, I didn't do anything with the bytes that I got from the recv call that I used.

Sorry about that, a newbie mistake.

Thank you for all of the help.

This post has been edited by cam888: 04 January 2010 - 09:31 AM

Was This Post Helpful? 0
  • +
  • -

#11 Guest_c.user*


Reputation:

Re: Interesting/Annoying socket problem

Posted 04 January 2010 - 11:21 AM

cam888 said:

I didn't do anything with the bytes that I got from the recv call that I used.


/1.0 perfectly shows that the line was got correctly

cam888 said:

if the connection was still present

there are poll and select system calls in linux
I used poll it is less complicated than select (but in some systems poll bases on select, so select is more reliable)

c.user said:

perhaps you have read them before the call

:)
Was This Post Helpful? 0

#12 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Interesting/Annoying socket problem

Posted 04 January 2010 - 02:28 PM

View Postc.user, on 4 Jan, 2010 - 10:21 AM, said:

cam888 said:

if the connection was still present

there are poll and select system calls in linux
I used poll it is less complicated than select (but in some systems poll bases on select, so select is more reliable)

c.user said:

perhaps you have read them before the call

:)


Yeah I was using a combination of select and recv, which was mentioned on a website. It works fairly well, I just completely didn't register that it would be taking bytes... Silly me.

Thank you for all of your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1