Thread: winsock

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    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.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    You can use the same socket.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how do i both send and receive at the same time?

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    send(sock, (...));
    recv(sock, (...));
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how do i use this select() function?
    i think it might solve the problem.

  8. #8

  9. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    can you show me an example to use it with send()/revc()
    thx!

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thx!

    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
    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!

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Post the code that you use for sending (from client) and receiving (at server).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    okey but why does it work the other way?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 08:32 AM
  2. Winsock Messaging Program
    By Morgul in forum Windows Programming
    Replies: 13
    Last Post: 04-25-2005, 04:00 PM
  3. Winsock - Where do i start?
    By Brain Cell in forum Networking/Device Communication
    Replies: 5
    Last Post: 02-14-2005, 01:39 PM
  4. Where do I initialize Winsock and catch messages for it?
    By Lithorien in forum Windows Programming
    Replies: 10
    Last Post: 12-30-2004, 12:11 PM
  5. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM