Advertisement

Winsock: How to send a struct?

Started by October 28, 2002 12:13 PM
32 comments, last by ktuluorion 22 years, 2 months ago
How do I get the send/recv functions to allow me to transfer an entire struct with Winsock? Thx
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
send (char*)struct, sizeof(struct)-bytesyoudontneed

unsigned char buffer[sizeof(struct)]

recv buffer,bytespending

Then you can reason out how you go from there handling partial messages and multiple messages per packet.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Tiberian Merchandise!!!! | GameShot ]
Advertisement
quote:
Original post by KalvinB
send (char*)struct, sizeof(struct)-bytesyoudontneed

unsigned char buffer[sizeof(struct)]

recv buffer,bytespending

Then you can reason out how you go from there handling partial messages and multiple messages per packet.

Ben


IcarusIndie.com [ The Rabbit Hole | The Labyrinth | DevZone | Gang Wars | The Wall | Hosting | Tiberian Merchandise!!!! | GameShot ]





I get a typecasting error doing that though?

  struct sendmsg{int hp;int mp;int str;};sendmsg msg;msg.hp=500;msg.mp=100;msg.str=10;ret = send(sClient, (char*)msg, sizeof(msg), 0);  


The error i get is:

C:\sockserver\main.cpp(150) : error C2440: ''type cast'' : cannot convert from ''struct sendmsg'' to ''char *''


Thx
[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
ret = send(sClient, (char*)&msg, sizeof(msg), 0); 


Take a closer look at what you''re passing around. This is a basic, fundamental C/C++ programming concept that you haven''t grasped. Don''t walk before you can crawl.
---------------------http://www.stodge.net
quote:
Original post by stodge
ret = send(sClient, (char*)&msg, sizeof(msg), 0);  


Take a closer look at what you''re passing around. This is a basic, fundamental C/C++ programming concept that you haven''t grasped. Don''t walk before you can crawl.


doh! ok thanks lol. I understand it, I just wasn''t thinking. I think my mind is just screwed up by thinking too hard over the last few days about WinSock.

Ok Great!

Thanks for the help.

Gotta love that.

[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
We all have those days.

quote:
Original post by ktuluorion
Original post by stodge
ret = send(sClient, (char*)&msg, sizeof(msg), 0);    


Take a closer look at what you're passing around. This is a basic, fundamental C/C++ programming concept that you haven't grasped. Don't walk before you can crawl.


[edited by - stodge on October 28, 2002 5:52:21 PM]
---------------------http://www.stodge.net
Advertisement
struct sendmsg{int hp;int mp;int str;};
sendmsg msg;
int a;
char *s=(char*)&msg




try this
http://www.8ung.at/basiror/theironcross.html
quote:
Original post by stodge
We all have those days.

Original post by ktuluorion
Original post by stodge
ret = send(sClient, (char*)&msg, sizeof(msg), 0);    


Take a closer look at what you''re passing around. This is a basic, fundamental C/C++ programming concept that you haven''t grasped. Don''t walk before you can crawl.


doh! ok thanks lol. I understand it, I just wasn''t thinking. I think my mind is just screwed up by thinking too hard over the last few days about WinSock.

Ok Great!

Thanks for the help.

Gotta love that.






OK wait.. only thing is.. I see how to typecast the struct to chars, but how do you turn the chars back into a struct?

Maybe this is a dumb question, but I don''t think I have ever run into a problem where I had to typecast a struct into an array of chars.

ret = recv(s, (char *)getmessage, sizeof(sendmsg), 0);
msg=(sendmsg)getmessage;



[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
You know I feel like i''m going to have a problem here too.. because I need to have the recv function have an expected size, but i''m not going to have the exact size of the recieving structure on that end.

Hmm.

I could always have a send before that which indicates what the size is.

[Piebert Entertainment] [Ask The All-Knowing Oracle A Question]------------------------------------------------------------GDSFUBY GameDev Society For UnBanning YodaTheCodaIf you want to see yoda unbanned then put this in your sig ------------------------------------------------------------DAIAGA Dave Astle is a God Association. To join, put this in your sig!Founder and High Priest of DAIAGA[edited by - YodaTheCoda on December 10, 2003 1:57:54 PM]
sendmsg msg;
msg.hp=111;
msg.mp=122;
msg.str=324234234;
char *sjz=(char*)&msg
printf("%s\n",sjz);
sendmsg *g;
g=(sendmsg*)sjz;
printf("%d %d %d",g->hp,g->mp,g->str);



thats how it works correct ^^^^
dudes

[edited by - Basiror on October 28, 2002 2:18:52 PM]
http://www.8ung.at/basiror/theironcross.html

This topic is closed to new replies.

Advertisement