• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dries Lamberechts

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;

    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

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
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 this post


Link to post
Share on other sites

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
0

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0