SDL and Winsock - Flashing images?

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

113 Replies - 2922 Views - Last Post: 04 July 2013 - 11:15 PM Rate Topic: -----

#1 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

SDL and Winsock - Flashing images?

Posted 18 June 2013 - 12:19 AM

Hey guys, It's me and my winsock problems again!
So first off, Thanks to skydiver and define for helping me out with my winsock multi-threading problem!
Claps for them!

So what happens now is that I have a server.
The server connects the clients.
The clients send the server their player (SDL image/movable character) x and y locations.
The server then sends BACK the positions, and the clients then add an image showing them or another players location.
Upon doing this, The client recieves the correct data, and the characters each show up on the other clients window!
However, These characters flash every once in a while.
This means that I CAN see other players/clients, But not continuously. The characters images flash every second or two.

How can I fix this or what is the problem???
Here is what I am doing:

----SERVER----
while(true) {
     recv(current_client,player_sX,sizeof(player_sX),0);
         //player_sX is the sent 'player_x' from the client.
     player_x = atoi(player_sX);
         //The server receives the X value and places it as an integer.
     
     recv(current_client,player_sY,sizeof(player_sY),0);
         //player_sY is the sent 'player_y' from the client.
     player_y = atoi(player_sY);
         //The server receives the Y value and places it as an integer.
     
     send(current_client,player_sX,sizeof(player_sX),0);
         //Sends back the player X value to the client
     send(current_client,player_sY,sizeof(player_sY),0);

     //ect


----CLIENT----
//ect...
int main() {
//ect...
while (true) {
        //ect...
        itoa(player_x,player_sX,10);
        //Change int to char for sending.
        send(sock,player_sX,sizeof(player_sX),0);
        //Send player X value to server
        
        itoa(player_y,player_sY,10);
        //Change int to char for sending.
        send(sock,player_sY,sizeof(player_sY),0);
        //Send player Y value to server
        
        add_image(0,0,0,0,800,600,background,screen);
        //Background (Before player)
        add_image(player_x,player_y,0,0,32,32,player,screen);
        //This client's image
        
        recv(sock,player_sX,sizeof(player_sX),0);
        //Recieve the player X value from other clients.
        player_rX = atoi(player_sX);
        //Used later for another image (Used for clients)
        recv(sock,player_sY,sizeof(player_sY),0);
        //Recieve the player Y value from other clients.
        player_rY = atoi(player_sY);
        //Used later for another image (Used for clients)
        
        add_image(player_rX,player_rY,0,0,32,32,player,screen);
        //Used for client's image.

        //ect


As you can see, it all works great. Each player can see eachother in the correct position.
But that comes at a price -- Flashing images (It's not consistant, The flashing happens every once in a few second. If not flashing, it's not showing up)

Hope anyone can help!!
I'm sure anything will=)

-Fysez

Is This A Good Question/Topic? 0
  • +

Replies To: SDL and Winsock - Flashing images?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3617
  • View blog
  • Posts: 11,269
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 18 June 2013 - 05:34 AM

That is because send() and recv() block until data is sent or received. Good game engine architectures are setup for asynchronous transmission and reception.
Was This Post Helpful? 0
  • +
  • -

#3 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 18 June 2013 - 10:53 AM

View PostSkydiver, on 18 June 2013 - 05:34 AM, said:

That is because send() and recv() block until data is sent or received. Good game engine architectures are setup for asynchronous transmission and reception.


So how can I fix this?
It's quite weird because no matter what, I have the image setup - So it'll constantly show that player.
But it's not? It's just flashing.
If it's sending and receiving properly; Wouldn't the 'player_sX' or 'player_sY' (Sent player_x&player_y values) continue the same?
For example, I have the add_image set up correctly, It would continuously add that image in the sent values.

How can I make it so that it doesn't do this and it continuouly (Is that a word?) add's the image of all clients?
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 18 June 2013 - 07:03 PM

Perhaps try the select model.

Winsock 2 I/O Methods 5 Part 1
Was This Post Helpful? 0
  • +
  • -

#5 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 18 June 2013 - 08:47 PM

View Post#define, on 18 June 2013 - 07:03 PM, said:

Perhaps try the select model.

Winsock 2 I/O Methods 5 Part 1


It seems i'm not as smart as i'd please to be.
I don't understand the select method and how it works.
I read the documents, But I don't get what I need to put in the 'select()' function call.
Or even where to put it.
It says:
int select(
    int nfds,
    fd_set FAR * readfds,
    fd_set FAR * writefds,
    fd_set FAR * exceptfds,
    const struct timeval FAR * timeout
);

Do I place EXACTLY this to call the function? Or do I need an 'int' number for nfds, a specific fd_set, a specific timeval, ect?
Or is this the legit code i'd use?

Also, Where would I place it? After I use 'send()'? Before 'recv()'?

Thanks for the link =D

-Fysez
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 19 June 2013 - 01:30 PM

Hi, the select function looks complicated because fd_set can handle multiple sockets. You can imagine fd_set as an array or set. You need to add sockets to the set. You only need to add one socket to the set.

There is an example at the bottom of the link.

A set is created called fread.

fd_set  fdread;




Your socket is added to the set.

    // Always clear the read set before calling select()
    FD_ZERO(&fdread);

    // Add socket s to the read set
    FD_SET(s, &fdread);




The select function is called, the first parameter number is not needed in Windows (there is a count in the set). The read set is added as a parameter and select checks whether there is a read event on the socket.

    if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR)

    {

        // Error condition

    }




You check the set to see if a read event has occurred on your socket.

        if (FD_ISSET(s, &fdread))

        {

            // A read event has occurred on socket s

        }



Was This Post Helpful? 0
  • +
  • -

#7 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 19 June 2013 - 02:04 PM

Okay awesome!
So if i'm understanding correctly,
This is where I place my 'send' and 'recv' function calls?:
if (FD_ISSET(s, &fdread)) {
//send(/*stuff*/);
//recv(/*stuff*/);
}
?
I'll test around with it when I get back from work.
Until then, Is what I said correct? If not, I'll try looking it up and figuring it out or whatever else.

Thanks again! Can't wait to figure this problem out!

-Fysez
Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 19 June 2013 - 02:43 PM

The read set parameter will tell you if you can receive data without being blocked.

if (FD_ISSET(s, &fdread)) {
//recv(/*stuff*/);
}




The write set parameter will tell you if you can send data without being blocked.

if (FD_ISSET(s, &fdwrite)) {
//send(/*stuff*/);
}




select
Was This Post Helpful? 0
  • +
  • -

#9 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 10:48 AM

Okay I hope it works, But i'm having trouble on this part of the code:
if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {

What is 'ret'? It's undefined
Was This Post Helpful? 0
  • +
  • -

#10 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 11:28 AM

Sorry! Realized how crabby I sound on my last reply xD
Thanks so much for the link, I learned a lot!
Just that one problem :P

Thanks!
-Fysez
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 01:52 PM

Hi, ret is an integer which stores the return value of the select function. The select link I gave previously tells you what the return values mean.
Was This Post Helpful? 0
  • +
  • -

#12 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 02:14 PM

Awesome, thanks!
I did not see that part (Other than the 'ret > 0')
I'll test around and see what I can come up with, after work!

You're such great help:)
Wish I could do you a favor back

-Fysez
Was This Post Helpful? 0
  • +
  • -

#13 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 02:55 PM

Okay so I think I have the whole idea about the Select function,
But there is ONE more thing in which I searched and couldn't find.
//Sock info, ect.
int ret;
while (gamerunning) {
        FD_ZERO(&fdread);
    
        FD_SET(sock, &fdread);
        
        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
             gamerunning = false;
        }
        if (ret > 0) {
             if (FD_ISSET(sock, &fdread)) {
                 recv(sock,client_Xconnected,sizeof(client_Xconnected),0);
             }
        }
        //ect

Here I have the read section.
Does there need to be a write section?
Or can I just put 'send(sock,/*ect*/)' before the read section?
If I need that write section, Do I still use 'int ret'?
Or a different integer?

I'm at work so I can't test it yet. Just making sure I'm thinking this through properly.

Thanks!!!
-Fysez
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1368
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 03:11 PM

Yes I think you can use two sets with one select call. You would want to test the set though :

if (FD_ISSET(sock, &fdwrite)) {
  send(...);
}


Was This Post Helpful? 0
  • +
  • -

#15 fysez  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 69
  • Joined: 14-June 13

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 09:43 PM

Okay I've tested it and here are my results:

I've added all the 'FD' stuff to everything that requires 'recv' (I do the FD things for receiving data).
This changed my game, but slightly. For example:
Now there is no delay on flashing. The image is constantly flashing at a fast speed, but not fast enough to stay a constant image.
I tried doing the same to 'send' for sending data, And I got nothing but errors.

I have NOT, however, Done either of these to my server code.
I left that the same.
I'll test around with that now, But so far; Little change.

This must be very complicated even to explain!
I'm glad you haven't given up on me, yet, define!:)

-Fysez
Was This Post Helpful? 0
  • +
  • -

  • (8 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »