Problem receiving packets

Started by
6 comments, last by hplus0603 10 years, 8 months ago

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

Advertisement

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.

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 };

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();

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 };

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?

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.

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.

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

enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement