Advertisement

WinSuck, can only send a char* ?

Started by September 12, 2003 01:03 PM
51 comments, last by Wickeeed 21 years, 4 months ago
hmm, can you guys say me what i did wrong ????

/////////////////////////////////////////////////////////
void SendPOS(float a, float b, float c){

int nBytes;

position pos;
message msg;

pos.x=a;
pos.y=b;
pos.z=c;

msg.type=1;
msg.length=sizeof(position);
memcpy(&msg.data,&pos,msg.length);

if ((nBytes = send(mySocket, ((char*)&msg), msg.length+8, 0)) == SOCKET_ERROR){
strcpy(clmassage, "send ERROR");
} else {
strcpy(clmassage, "Ready");
}
}
///////////////////////////////////////////////////////////////
position RecvPOS(){

message msg;
position pos;

char* Pointer=((char*)&msg);
int nBytes;

for (int i=1;i==1;i++) {
timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;

nBytes = select(ServerSet.fd_count, &ServerSet, NULL, NULL, &waitTime);
if (nBytes == 0){ continue; }
nBytes =recv(mySocket, Pointer, sizeof(Pointer), 0);
if(nBytes == SOCKET_ERROR){continue; }
if(nBytes == 0){continue;} else {
msg= *((message*)Pointer);
strcpy(clmassage, "OK msg");
memcpy(&pos,&msg.data,msg.length);
}

}

return pos;
}
///////////////////////////////////////////////////
quote:
Original post by Wickeeed
hmm, can you guys say me what i did wrong ????
for (int i=1;i==1;i++) {
[...........]
return pos;
}


This loop never runs, you immediately drop to the return (you set i=1, then check for i==1 which it does, so you drop). Not exactly sure what this for loop is for But whyever it''s there, you never get into the interesting code.

I think you are getting pretty close. I dislike Kalvin''s header methodology (if that''s what you are using as a template) because it is easily broken (header size changes, or you accidentally put a var in between the header and message type, or any other number of ways to screw it up), but I suppose it''s the easiest way to show how to add a header to your packet. I prefer to use classes and inheritance for the header, but whatever, that''s probably beyond your abilities at the moment

-Ron
Creation is an act of sheer will
Advertisement
- - - CAN SOME ONE SHOW ME HOW TO SEND AND RECV A STRUCT ??????? - - -

PLZ HELP !!!


Ron: plz try to explain how you do it.




MY FUNCTION: (dont workt)
//////////////////////////////////
void SendPOS(float a, float b, float c){
int nBytes;
position pos;
pos.x=a;
pos.y=b;
pos.z=c;
if ((nBytes = send(mySocket, ((char*)&pos), sizeof(pos), 0)) == SOCKET_ERROR) {
strcpy(clmassage, "send gg ERROR");
} else {
strcpy(clmassage, "Ready");
}
}
////////////////////////////////////////
position RecvPOS(){
position pos;
char* Pointer=((char*)&pos);
int nBytes;
float result;

for (int i=1;i==1;i++) {
timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;

nBytes = select(ServerSet.fd_count, &ServerSet, NULL, NULL, &waitTime);
if (nBytes == 0) { continue; }
nBytes =recv(mySocket, Pointer, sizeof(Pointer), 0);
if(nBytes == SOCKET_ERROR){continue; }
if (nBytes == 0){ continue; } else {

pos= *((position*)Pointer);
strcpy(clmassage, "OK msg");
}
}
return pos;
}
/////////////////////////////////////

[edited by - Wickeeed on September 15, 2003 11:55:53 AM]
re-read what ron said:

for (int i=1;i==1;i++) {

that loop NEVER runs. i = 1, check i == 1 -> true, don't run loop

-me

[EDIT: also, i trust you are initializing winsock somewhere before you run those functions? and you are running them on different computers that are attached to a network, or at least in different threads on the same computer?]

[edited by - Palidine on September 15, 2003 12:04:43 PM]
quote:
Original post by Wickeeed
- - - CAN SOME ONE SHOW ME HOW TO SEND AND RECV A STRUCT ??????? - - -

PLZ HELP !!!


MY FUNCTION: (dont workt)

void SendPOS(float a, float b, float c){
int nBytes;
position pos;
pos.x=a;
pos.y=b;
pos.z=c;
if ((nBytes = send(mySocket, ((char*)&pos), sizeof(pos), 0)) == SOCKET_ERROR) {
strcpy(clmassage, "send gg ERROR";
} else {
strcpy(clmassage, "Ready";
}
}

position RecvPOS(){
position pos;
char* Pointer=((char*)&pos);
int nBytes;
float result;

for (int i=1;i==1;i++) {
timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;

nBytes = select(ServerSet.fd_count, &ServerSet, NULL, NULL, &waitTime);
if (nBytes == 0) { continue; }
nBytes =recv(mySocket, Pointer, sizeof(Pointer), 0);
if(nBytes == SOCKET_ERROR){continue; }
if (nBytes == 0){ continue; } else {

pos= *((position*)Pointer);
strcpy(clmassage, "OK msg";
}
}
return pos;
}




<SPAN CLASS=editedby>[edited by - Wickeeed on September 15, 2003 11:53:31 AM]</SPAN>



okay your Send looks good.

but your receive does not. If u look closely, you will see that your only receiving 1 byte ( 1 char ) worth of data from the socket.

looks at this pseudo-snippet and see if it helps

Sending:

Create a Pos instance.
Fill that instance with the data you want.
then using send like this we send it across the network.
send( MySocket, (char*)&MyPos, sizeof(POS), 0 );


Receiving: Like sending in a sense look closely here

Create a Pos Instance.
Get Data from the socket
recv( MySocket, (char *)&MyPos, sizeof(POS), 0 );
Get data from the now filled Pos instance.


if u look @ your code, you are creating a Char* called pointer.. and receiving only that much worth of data...


All in all I hope this helps.


Rigear




quote:
Original post by Wickeeed
Ron: plz try to explain how you do it.



I can''t Wick. Now, I am in no way trying to be mean here, so please don''t read it that way. I think people on these boards tend to get a little snippy with those who are new to the programming gig (not that I blame them sometimes). I try to help where I can, because I figure we were all there once

But the thing is, if you can''t see why your "for" loop isn''t running and have to ask for help on that, then there is no way you are ready for the concepts of inheritance, or probably even classes, which you would need to know in order for me to explain my particular methodology. You are miles away from that yet

For a second reason, I couldn''t even if you were at the appropriate programming level. I''d have to go over my entire buffer class first, then my packet classes, then my network class. It''s too much, there isn''t enough room on the boards You have to develop your own methodology for sending and receiving data. Kalvin''s method will work, even though I don''t particularly like it. Just don''t change the header size or move the structure from in front of the packet structure, or make it a class, or put functions in either of them, and you''ll be okay to use that for a while until you discover why it''s a fragile method, heh.

Oh, and Rigear is right, I didn''t catch that before. Your line
nBytes =recv(mySocket, Pointer, sizeof(Pointer), 0);
should be
nBytes =recv(mySocket, Pointer, sizeof(Pos), 0);
otherwise, you''re not receiving all of the data, only the first 4 bytes of it (the size of a pointer).
Creation is an act of sheer will
Advertisement
can you guys show me your send and recv functions, i want to see how to do it..

about: for(int i=1;1==i;i++){ // its works but only Once but i like it like this.


i did all what you told but it still dont work...


///////// new code /////////////////
void SendPOS(float a, float b, float c){

int nBytes;

position pos;

pos.x=a;
pos.y=b;
pos.z=c;

if ((nBytes = send(mySocket, (char*)&pos, sizeof(pos), 0)) == SOCKET_ERROR) {
strcpy(clmassage, "send ERROR");
} else {
strcpy(clmassage, "Ready");
}
}
/////////////////////////////////////////////////////
float RecvPOS(){

unsigned int geg = mySocket;
FD_SET(geg, &ServerSet);

position pos;
int nBytes;


timeval waitTime;
waitTime.tv_sec = 0;
waitTime.tv_usec = 0;

nBytes = select(ServerSet.fd_count, &ServerSet, NULL, NULL, &waitTime);
FD_CLR(geg, &ServerSet);

if (nBytes == 0) { return -1; }

nBytes =recv(mySocket, (char*)&pos, sizeof(pos), 0);

if(nBytes == SOCKET_ERROR){ return -1; }
if(nBytes == 0){ return -1; } else {

return pos.x;
strcpy(clmassage, "OK msg");

}


}
/////////////////////////////////////////////
I think what you are looking for at this point is a nice solid winsock tutorial with bits of code for you to copy/paste. as usual, google comes to the rescue with "winsock tutorial"

check out:
http://www.hal-pc.org/~johnnie2/winsock.html

or some of the other search results. at some point, hopefully soon, you'll want to read more about the winsock functions (use MSDN: http://msdn.microsoft.com/library/default.asp

-me

[edited by - Palidine on September 15, 2003 2:40:16 PM]
I don''t think you''re ready for Winsock yet. Cutting and pasting code isn''t going to help you.

nBytes =recv(mySocket, (char*)&pos, sizeof(pos), 0);

Won''t work because you''re trying to force a streaming protocol to act like a message based protocol. You''re not guarenteed to get the whole message you send in one call like that and you fail to account for it.

And the tutorial linked to by Palidine is pretty much garbage unless you''re making a MUD. Besides that fact he recieves all of one byte at a time from the buffer in a loop until it''s empty which is just flat out retarded. It''s completely unnecessary and a waste of time. He uses the character ''\n'' to tell if the end of a message has been reached when you need to know how to use a header for the type of project you''re doing.

You have to buffer the data comming in and then figure out a way to tell if a complete message is ready for processing and pass it off into you game code for handling. The way you do that is by using a header.

The Winsock code presented on GameDev is practically unuable it''s so full of bugs.

I have a fully functional plug and play TCP/IP Winsock class posted at www.icarusindie.com in the DevZone which you can use along with a quick tutorial on how to use it. But you really need to just step back and make some single player games first.

Ben


[ IcarusIndie.com | recycledrussianbrides.com | Got Linux? ]


Will Post For Food
quote:
Original post by KalvinB
...because you''re trying to force a streaming protocol to act like a message based protocol.


Hmmm, what makes you assume TCP? I was assuming UDP myself, simply because it''s the most common protocol for games. It''s a good question, though, which makes a difference. Wick, which one are you using?
Creation is an act of sheer will

This topic is closed to new replies.

Advertisement