Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

recv questions....

This topic is 6332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can you call more than one recv on a single packet????? i tried it on my "game" and it made the player teleport around which isnt good..... in other words.... can i do this: ///////////sender struct pos{ short x; short y; }pos; send((char *)pos,sizeof(pos)); /////////reciver short x; short y; recv((char *)&x,sizeof(short)); recv((char *)&y,sizeof(short)); thx for any help infestedfurby.cjb.net

Share this post


Link to post
Share on other sites
Advertisement
no you cant
just do this
  
short x;
short y;

recv((char *)&x,sizeof(short));
memcpy(&y, &x, sizeof(short));




"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
We are creating a Multi-player space strategy/shoot-em-up/RPG game.
Development is well under way and we do have a playable demo.
Always looking for help.
Digital Euphoria Soft

Share this post


Link to post
Share on other sites
I guess what im really wondering is the best way to send and recieve a variable sized packet..... It seems the only way to do it with tcp is to send 2 packets, the first with the size and the second with the data. This seems very wasteful and time consuming. Is there any way to recv a dynamic amount of data or is there some other way to do it. Or maybe its just not possible and I was skrewed from the start

Thx for any help

Share this post


Link to post
Share on other sites
You just need to send the size of the data payload as first 4 (or 2 if you wish) bytes of the data.

Of course, if you''re sending different types of data, what you really want is a tag ID that identifies what kind of data is being sent.

Share this post


Link to post
Share on other sites
no u cannot just read 2 short .. since a struct must be aligned.
it may have padding if the alignement is not 4.


use this...

pos newpos;
int x,y;

recv((char*)newpos,sizeof(pos));
x = pos.x;
y = pos.y;

Share this post


Link to post
Share on other sites
but how is the receiver supposed to get the first 2 bytes of data?

if you have a packet with the first 2 bytes storing the size and the next x bytes storing data......

reciving.......

recv(buff, 2+x);//x isnt known so this isnt possible

how am i supposed to do this????

do i just have to send 2 packets?

Share this post


Link to post
Share on other sites
where you have the 2+x.........
i believe this is just the size of the buffer that you are receiving into
example
  
char buf[10000];
recv(buf, 10000);

now if the size of the packet being received is only 100 bytes then i believe the first 100 bytes of the buf will be filled and the rest will remain unchanged. A UDP packet knows its own size and it will tell the receive function how much data it is actually receiving. You still need to follow the advice of the guy before me and sent a packet ID and packet size with each packet you send for your own personal processing. Like
packet.ID = MESSAGE;
packet.ID = POSITION;
packet.ID = NEW_PLAYER;
stuff like that and then
packet.size = 100;
packet.size = whateverAmountOfDataYouAreSending;
so that on the receiving side
you receive into your buffer (note....it doesnt have to be a char array...could be
PACKET myPacket;
recv((char*)&myPacket, sizeof(myPACKET)); )
then when you recv it you can check
if (myPacket.ID == MESSAGE)
{
displayMessageOnScreen();
}
and then packet.size should equal the num of characters to be displayed.......or somethign like that

if i am wrong here guys then please correct me.


"Yo yo ma"
-Kramer

Share this post


Link to post
Share on other sites
does that work with tcp/ip or only udp?

btw.... in a simple text based thing i tried the following


server.........

do all the stuff to set up a tcp/ip connection to the client

send("hi",3);






client............
char str[3];

do all the stuff to set up a tcp/ip connection to the client

recv( str,1);
recv(str+1,1);
str[2]=''\0'';

cout<
and it got the message and printed hi on the client
is this supposed to work?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!