Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem receiving packets


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Frostraver   Members   -  Reputation: 174

Like
0Likes
Like

Posted 11 August 2013 - 01:08 PM

Hi everyone,

 

I'm trying to get my client to send a packet with some text and my server to receive the packet and display the text.

Currently I'm trying this locally so what I'm doing now is the following.

 

1) I send some text using sendto()

2) I try to receive the data using recvfrom()

 

The problem is that the recvfrom data doesn't seem to receive a packet. The two methods that I'm using are coded like this:

 

Used in step 1:

bool Networking::SendPacket(const void * message)
{
	// Set up address to send the packet to
	unsigned int a = 127;
    unsigned int b = 0;
    unsigned int c = 0;
    unsigned int d = 1;
    unsigned short port = 2890;

	unsigned int destination_address = ( a << 24 ) | ( b << 16 ) | ( c << 8 ) | d;
    unsigned short destination_port = port;

    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl( destination_address );
    address.sin_port = htons(destination_port);

	// Try to send the packet
	int sent_bytes = sendto(handle, (const char*)message, sizeof(message), 0, (sockaddr*)&address, sizeof(sockaddr_in));

	// Check if the packet was sent successfully
    if (sent_bytes != sizeof(message))
    {
        printf("failed to send packet: return value = %d\n", sent_bytes);
        return false;
    }
	else
	{
		cout << "this packet was successfully sent: " << (const char*)message << "\n";
		return true;
	}

	return true;
}

Used in step 2:

void Networking::ReceivePacket()
{	
	unsigned char packet_data[256];

	#if PLATFORM == PLATFORM_WINDOWS
		typedef int socklen_t;
	#endif
			
	sockaddr_in from;
	socklen_t fromLength = sizeof( from );

	int received_bytes = recvfrom(handle, (char*)packet_data, sizeof(packet_data), 0, (sockaddr*)&from, &fromLength );

	if (received_bytes <= 0)
		return;

	cout << "RECEIVED DATA: " << (char*)packet_data << "\n";
}

Does anyone see what I'm doing wrong?

 

Thanks in advance



Sponsor:

#2 radioteeth   Prime Members   -  Reputation: 914

Like
0Likes
Like

Posted 11 August 2013 - 04:46 PM

replace all your 'destination_address' code with something more like:

// get pointer to address
unsigned char *addr = (unsigned char *)&address.sin_addr.s_addr;

// set address directly
addr[0] = 127;
addr[1] = 0;
addr[2] = 0;
addr[3] = 1;

try using 'sizeof(sockaddr_in)' for 'fromLength' instead of 'sizeof(from)'... try using a #define value for the size of 'packet_data', as well as using it to replace 'sizeof(packet_data)'..

 

 make sure that whatever is receiving the packet has called 'recvfrom' before whatever is sending the packet calls 'sendto', otherwise, you will send the packet, it will disappear in the void of datagram, and the receiver will be called after the packet disappears. that is the nature of UDP/datagram, data disappears if nobody is listening for it before it is sent (along with network noise and collisions causing packets to be disregarded and lost).

 

good luck.



#3 hplus0603   Moderators   -  Reputation: 4960

Like
1Likes
Like

Posted 11 August 2013 - 05:37 PM

sizeof(message) is 4 or 8 depending on your architecture. It's unlikely to be what you actually mean.

 

Also, is "handle" a valid socket? Is it bound to the port that you're trying to send to, before you try to send?

 

What values do the calls to sendto() and recvfrom() actually return?

 

Finally, you typically want to memset() the "address" to 0 before starting to fill it out. This used to matter on some implementations (even though it shouldn't.) I don't know if it still does.


enum Bool { True, False, FileNotFound };

#4 Frostraver   Members   -  Reputation: 174

Like
0Likes
Like

Posted 12 August 2013 - 09:32 AM

Hi guys. Thanks for the quick replies.

 

@RadioTeeth: Why should I do the destination_address differently?

 

@hplus0603: Yes, handle is a valid socket and it's bound to the right ports and everything.

sendto() returns 4

recvfrom() returns -1 in the current state but I'll be changing things now so I'll keep you updated what they return if you want.

 

EDIT: So I changed some things like Radioteeth suggested but that doesn't seem to change anything. I have this code to test out if my program receives it's own messages:

pNetworkObject->ReceivePacket();
pNetworkObject->SendPacket("hello");

pNetworkObject->SendPacket("second hello");
pNetworkObject->ReceivePacket();

Edited by Frostraver, 12 August 2013 - 10:01 AM.


#5 hplus0603   Moderators   -  Reputation: 4960

Like
0Likes
Like

Posted 12 August 2013 - 10:07 AM

If sendto() returns 4, then you're still sending the pointer, not the data. (Also, you're on a 32-bit architecture.)

 

If recvfrom() returns -1, then what does errno say about the specific error?

 

Is "handle" the same socket in both cases, or are you actually running two processes?


enum Bool { True, False, FileNotFound };

#6 Frostraver   Members   -  Reputation: 174

Like
0Likes
Like

Posted 12 August 2013 - 10:33 AM

I'll check how I should send the data properly. 

I'm currently sending this (const char*)message. Where message starts as a void*. 

When I cout this data it outputs the text so I don't think that's the problem?

And I'm running a 64-bit windows 7. But this is just my Visual Studio setup that's 32-bit or not?

 

errno outputs "No Error".

I use printf(strerror(errno)) to show to error on my screen.

 

handle is the same if both cases yes. I don't think this could be a problem?



#7 Washu   Senior Moderators   -  Reputation: 4465

Like
0Likes
Like

Posted 12 August 2013 - 12:00 PM

I'll check how I should send the data properly. 
I'm currently sending this (const char*)message. Where message starts as a void*. 
When I cout this data it outputs the text so I don't think that's the problem?
And I'm running a 64-bit windows 7. But this is just my Visual Studio setup that's 32-bit or not?
 
errno outputs "No Error".
I use printf(strerror(errno)) to show to error on my screen.
 
handle is the same if both cases yes. I don't think this could be a problem?


As already noted, sizeof(message) does not do what you expect. What you are sending is the first four bytes of message, and nothing else. This line is WRONG:
 
	int sent_bytes = sendto(handle, (const char*)message, sizeof(message), 0, (sockaddr*)&address, sizeof(sockaddr_in));
You need to pass to your function the data pointer AND the length of the data. The length of the data cannot be trivially determined from the pointer, assuming binary data, as there is no logical terminator.

Edited by Washu, 12 August 2013 - 12:01 PM.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#8 hplus0603   Moderators   -  Reputation: 4960

Like
0Likes
Like

Posted 12 August 2013 - 08:19 PM

Also, on Windows, use WSAGetLastError() instead of errno.


enum Bool { True, False, FileNotFound };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS