Followers 0

# Problem receiving packets

## 7 posts in this topic

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;

// Try to send the packet

// 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

socklen_t fromLength = sizeof( from );

return;

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


Does anyone see what I'm doing wrong?

0

##### Share on other sites

// get pointer to address



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.

0

##### Share on other sites

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.

1

##### Share on other sites

Hi guys. Thanks for the quick replies.

@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
0

##### Share on other sites

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?

0

##### Share on other sites

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?

0

##### Share on other sites

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
0

##### Share on other sites

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

0

## Create an account

Register a new account