-
winsock
i need help to send and receive data for my game
so far i have made a client and server, where the server sends
the data to the client.
but i need help so that the client can send back to the server,at the same time. what do i need to do must i open a new socket or can i use the same that is used to receive?
thx.
-
You can use the same socket.
-
how do i both send and receive at the same time?
-
send(sock, (...));
recv(sock, (...));
-
i tried that but the game freezes.
i tried a timer that swithes from send to receive every second that works but its not live :(
-
Er. You DO realize that recv() is a blocking call right? If you call recv() and there's no data, then it will wait until you GET data, which may be soon, or may be never. You can try using select() to check if there's data waiting, or you can use asynchronous sockets of various types. There's a "useful links" sticky at the top of the network forum, you can find some tutorials there that'll teach you how.
-
how do i use this select() function?
i think it might solve the problem.
-
-
can you show me an example to use it with send()/revc()
thx!
-
Code:
bool hasData(SOCKET sock)
{
timeval t = {0,0};
fd_set fds;
FD_ZERO(&fds);
FD_SET(sock, &fds);
return (::select(sock + 1, &fds, NULL, NULL, &t) > 0);
}
...
char buf[256];
int bytes;
if(hasData(sock))
bytes = recv(sock, buf, 256, 0);
That function uses select() to check if the socket is readable, and times out immediately (timeout = 0) if there's no data. The return of select() is the number of sockets with data waiting. You could also include more than 1 socket in the FD_SET to check if any one of them has data, and/or you could make the timeout non-zero so it will wait until either one of the sockets has data or else the timeout runs out; I think you can make the timeout infinite too if you want, though you probably don't.
-
thx!:cool:
does anyone know by any chance why when i send back data from the the client it only shows zero, the string contains a 0 :confused:
when it should be x150 y150(positions)
im using the exact same method as i used to send from the server to the client there isnt any problem from this way.
only from the client to the server
thanx in advance!
-
Post the code that you use for sending (from client) and receiving (at server).
-
both client & server look like this:
Code:
char stemp[8]; //send
char rtemp[8]; //receive
while() //game loop
{
sprintf( stemp, "x%i y%i",G.x ,G.y ); //put in the positions in the string
sen=send(socket,stemp,16,0);
if(hasData(socket)) // :)
rec=recv(socket,rtemp,16,0);
sscanf(rtemp,"x%i y%i",&P.x,&P.y);
...
}
P.x & G.x are a struct for the player, x & y are both ints
& i check if it sends or not
-
Well, think about it. stemp has size 8. "x150 y150" has 9 characters, plus a NULL terminator. Maybe sprintf is putting 0 for error? Then, you're calling recv() with rtemp, and saying that rtemp has size 16 while it only has size 8. Try making stemp and rtemp bigger, say 128 bytes.
-
okey but why does it work the other way?