🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
recv questions....
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
no you cant
just do this
"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
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
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
Thx for any help
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.
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.
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;
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;
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?
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?
where you have the 2+x.........
i believe this is just the size of the buffer that you are receiving into
example
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
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
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?
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?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement