Advertisement

UDP basics

Started by May 03, 2004 09:52 AM
8 comments, last by chicknstu 20 years, 9 months ago
I want to make a real-time game using a UDP connection in winsock.. I have these two functions Check_Connections() - which will check for new clients trying to connect) Check Messages() - Which will check for messages coming in from the clients These messages... can vary in length (It could be some positional data, or it could be a message that the player wants to disconnect) Is this possible under UDP, any help would be amazing! stu
Yes.

-=[ Megahertz ]=-
-=[Megahertz]=-
Advertisement
Most amusing

Basically, iv written this whole game in TCP/IP, and now im told that that's wrong and I should be using UDP... is there any way to turn off this error checking or speed it up, or am i doomed to fail my degree (Cos im sure iv gone too far to convert the thing now)


Any tips for an easy conversion would be amazing

stu

[edited by - chicknstu on May 3, 2004 11:26:00 AM]
Who told you its wrong?

You can use TCP/IP for a game but its not ideal.

For a good few reasons why, read this article.

http://www.gamasutra.com/features/19990903/lincroft_01.htm

However, just like it says in the article, UDP has no guarantee that packets will be delivered. So if you have data that absolutely must get delivered, your going to have to write your own ack/resend handling stuff. It not too tricky, but it''ll take some time.

Aside from things like this theres really not much diffrence between UDP and TCP other than the fact that TCP is a stream based protocol where as UDP is more packet oriented.

-=[ Megahertz ]=-
-=[Megahertz]=-
Cheers megaherz, you have made me seen the light. Rather than try and convert to UDP, ill just blag some good reasons why i picked TCP instead!

cheers for the link

stu
ÎÒÒÔΪÊǽ²UDPÄØ£¬Ô­À´»¹ÊÇÎÊ¡£
When I saw this,I think it is teaching UDP,but it ask for help!
Advertisement
Basically... this code...

sockaddr_in UDP_me;
SOCKET UDP_s; // Our UDP socket handle
int sa_size = sizeof(UDP_me);

UDP_s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
UDP_me.sin_family = AF_INET;
UDP_me.sin_port = htons (5556);
UDP_me.sin_addr.s_addr = inet_addr (ServerIP);

while
{
sendto(UDP_s, (char*)&p,sizeof(p), 0, (struct sockaddr *) &UDP_me, sizeof(sa_size));
}

Complies fine, but sends nothing to the server... any solutions??? anything iv missed

stu
If ServerIP is the raw IP (not converted) then you need a htonl() around it when assigning it into the address.

Anyway, this question should have been asked in a new thread.
enum Bool { True, False, FileNotFound };
quote:
Original post by chicknstu
int sa_size = sizeof(UDP_me);
....
sendto(UDP_s, (char*)&p,sizeof(p), 0, (struct sockaddr *) &UDP_me, sizeof(sa_size));
}



Two sizeofs? Must be wrong, but I don''t know whether that is the only problem... (sendto() wants the size of the struct, not the size of the size of the struct)
-- Rasmus Neckelmann
quote:
Original post by chicknstu
while
{
sendto(UDP_s, (char*)&p,sizeof(p), 0, (struct sockaddr *) &UDP_me, sizeof(sa_size));
}



You want to pass sizeof(UDP_me) as the last parameter.

For future debugging, check the return value for each socket function. If it returns -1, the value in the global "errno" will tell you why it failed.
Matt Slot / Bitwise Operator / Ambrosia Software, Inc.

This topic is closed to new replies.

Advertisement