SDL and Winsock - Flashing images?

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

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

#16 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:44 PM

Aha!
Found how to link videos. So I made a Youtube channel and video showing what happens.
Here it is:
Client Test
See, It should not do this
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 20 June 2013 - 11:41 PM

Post the latest version of your client code.
Was This Post Helpful? 0
  • +
  • -

#18 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 21 June 2013 - 12:14 PM

Pseudo Client Code:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <conio.h>
#include <signal.h>
#include <stdio.h>
#include <sdl.h>
//Included Files

//SDL Images defined here

bool gamerunning = true;

bool keys[323] = {false};

int player_x = 320;
int player_y = 240;
char player_sX[100];
char player_sY[100];
int player_rX;
int player_rY;

int client_connected;
int amount_of_players;
char client_Xconnected[100];
char client_Sconnected[100];

      //error trapping signals
      #define SIGINT 2
      #define SIGKILL 9
      #define SIGQUIT 3
      // SOCKETS
      SOCKET sock,client;
      
void add_image(int x, int y,
               int srcX, int srcY,
               int width, int height,
               SDL_Surface* image, SDL_Surface* destination) {
     //Add image info here
}

void s_handle(int s) {
    if(sock)
       closesocket(sock);
    if(client)
       closesocket(client);
    WSACleanup();
    exit(0);
}

void set_trans(SDL_Surface* image,int r,int g,int B)/> {
     //For setting transparency on images here
}

void s_cl(char *a, int x) {
    s_handle(x+1000);
}

int main(int argc, char* argv[]) {
   fd_set fdread;
    
   //Window info here
    
   //SDL image destination stuff here
   
    
   //Handling sockets (Ect) here

    int res,i=1,port=999;
    char buf[100];
    char msg[100] = "";
    char ip[15];
    WSADATA data;

    //Signals

    //Server stuff (ect)

    char RecvdData[100] = "";
    int reta;
    int ret;
    
    int client_connected_frame = 0;
    int amount_of_players_frame = 0;

    while(gamerunning == true) {
        amount_of_players_frame +=1;
        
        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);
                  //Receive number of clients online
             }
        }
        
        if (client_connected_frame == 0) {
             client_connected = atoi(client_Xconnected);
             //Receive my client number (#thread I connected to)
             client_connected_frame +=1;
        }
        amount_of_players = atoi(client_Xconnected);
                      
        strcpy(buf,"");
        
        //Here we place the keys (Left, Right, Up, and Down)
        //Upon pressing, it adds/minuses X/Y player position (determined by player_x and player_y)
          
        itoa(player_x,player_sX,10);
        send(sock,player_sX,sizeof(player_sX),0);
        //Send player_x
        
        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,player_sX,sizeof(player_sX),0);
                  //Receive player_x
             }
        }
        player_rX = atoi(player_sX);
        
        itoa(player_y,player_sY,10);
        send(sock,player_sY,sizeof(player_sY),0);
        //Send player_y
        
        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,player_sY,sizeof(player_sY),0);
                  //receive player_y
             }
        }
        player_rY = atoi(player_sY);
        
        //Add image for background here
        //Add image for player here (Using player_x and player_y
        
        //Add image for received positions here (using player_rX and player_rY
        
        fgets(buf,sizeof(buf),stdin);
         
        res = send(sock,buf,sizeof(buf),0); 
        //Send buf

        //Receive and send buf info here (For closing server or closing Client)
       
        //Send and recieve buf data here
        
       SDL_Flip(screen);
    }
    while (gamerunning == false) {
          //Error screen here
    }
    SDL_Quit();
    closesocket(client);
    WSACleanup();
    return 0;
}


Hope that helps a little bit, I know I took a lot out.
Thanks for the help!

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

#19 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1398
  • View blog
  • Posts: 4,873
  • Joined: 19-February 09

Re: SDL and Winsock - Flashing images?

Posted 21 June 2013 - 01:36 PM

It looks as if you are getting wrong data. Is the server sending the number of clients all the time?
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 21 June 2013 - 04:25 PM

My instincts have me looking at the way player_sX and player_sY is being clobbered for both sending and receiving. In the case when you've just sent out your player coordinates, and the select() indicates that there was nothing to receive, then your player_sX and player_sY will still contain the last coordinates that were sent out.

Personally I would do the following things:
- send data in binary
- send and recieve data as a tuple in a structure: player_number, X, Y
- use vector, array, or map of the structures

This post has been edited by Skydiver: 21 June 2013 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -

#21 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 21 June 2013 - 09:15 PM

View Post#define, on 21 June 2013 - 01:36 PM, said:

It looks as if you are getting wrong data. Is the server sending the number of clients all the time?

Yes, It's constantly sending the number of connected clients.
It works, Though!
I had tested this stopping my loop when a number of clients has gone to a certain amount.
It stops once to show your own connected client (So if i was first to connect, Until I disconnect; I am client #1 no matter how many people have connected.) I also have the loop to tell how many people are connected overall.

View PostSkydiver, on 21 June 2013 - 04:25 PM, said:

My instincts have me looking at the way player_sX and player_sY is being clobbered for both sending and receiving. In the case when you've just sent out your player coordinates, and the select() indicates that there was nothing to receive, then your player_sX and player_sY will still contain the last coordinates that were sent out.

Personally I would do the following things:
- send data in binary
- send and recieve data as a tuple in a structure: player_number, X, Y
- use vector, array, or map of the structures

I don't believe that player_sX and player_sY contain the last coordinants - Correct me if I'm wrong, But I have high curiousity that the reason for the flashing is that the coordinants aren't responding as they should :o

As for the steps: I'm not sure how I could do any of this xD Care to help me? (You can give up on me - I'm only piling problems I, myself, can't even fix)

Thanks again!

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

#22 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 21 June 2013 - 09:20 PM

Oh. And one more thing.
I found that when my server is up, No client can connect on a different PC.
I thought it was an IP problem, So I created a code that finds he IP you are connected to, and then uses it as the server/client ser.sin_addr.s_addr=inet_addr(myIPaddress);
This works - Tested - However, The player STILL cannot connect on a different IP or PC.
For the server, I used INADDR_ANY.
This isn't the above problem, But it is a side-problem I had found and would like to share if anyone knows how to also fix.

Thanks!

-Fysez

This post has been edited by fysez: 21 June 2013 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#23 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 21 June 2013 - 09:26 PM

View Postfysez, on 22 June 2013 - 12:15 AM, said:

I don't believe that player_sX and player_sY contain the last coordinants - Correct me if I'm wrong, But I have high curiousity that the reason for the flashing is that the coordinants aren't responding as they should :o/>/>


Here's an example:
114        itoa(player_x,player_sX,10);
115        send(sock,player_sX,sizeof(player_sX),0);
116        //Send player_x
117         
118        FD_ZERO(&fdread);
119        FD_SET(sock, &fdread);
120        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
121                 gamerunning = false;
122        }
123        if (ret > 0) {
124             if (FD_ISSET(sock, &fdread)) {
125                  recv(sock,player_sX,sizeof(player_sX),0);
126                  //Receive player_x
127             }
128        }
129        player_rX = atoi(player_sX);



On line 114 you convert your player's X position to a string, and store it into player_sX. On line 115 you send the position.

If line 124 determines that there is no incoming data from the server yet, then the code will proceed to line 129.

On line 129, you convert the integer that was stored in player_sX into an integer and store that as the other player's X position.

So guess what was the last thing stored in player_sX? It was your player's X position from line 114.

This post has been edited by Skydiver: 21 June 2013 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

#24 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 21 June 2013 - 09:37 PM

Here's an example of sending a structure:
struct PlayerState
{
    int X, Y;
    int HealthLeft;
    int ManaLeft;
};

:

PlayerState me = { 60, 25, 88, 23 };

send(socket, static_cast<char *>(&me), sizeof(me), 0);


Was This Post Helpful? 0
  • +
  • -

#25 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 22 June 2013 - 12:25 AM

View PostSkydiver, on 21 June 2013 - 09:26 PM, said:

View Postfysez, on 22 June 2013 - 12:15 AM, said:

I don't believe that player_sX and player_sY contain the last coordinants - Correct me if I'm wrong, But I have high curiousity that the reason for the flashing is that the coordinants aren't responding as they should :o/>/>/>


Here's an example:
114        itoa(player_x,player_sX,10);
115        send(sock,player_sX,sizeof(player_sX),0);
116        //Send player_x
117         
118        FD_ZERO(&fdread);
119        FD_SET(sock, &fdread);
120        if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
121                 gamerunning = false;
122        }
123        if (ret > 0) {
124             if (FD_ISSET(sock, &fdread)) {
125                  recv(sock,player_sX,sizeof(player_sX),0);
126                  //Receive player_x
127             }
128        }
129        player_rX = atoi(player_sX);



On line 114 you convert your player's X position to a string, and store it into player_sX. On line 115 you send the position.

If line 124 determines that there is no incoming data from the server yet, then the code will proceed to line 129.

On line 129, you convert the integer that was stored in player_sX into an integer and store that as the other player's X position.

So guess what was the last thing stored in player_sX? It was your player's X position from line 114.

Ohhhh I see!!
So my whole select() idea is either wrong, or not right?
How am I able to fix this?
Because I had the complete idea that waiting to receive from the server was the problem.
So I need to take out all of those things I had put in for the select function?

By doing so; It doesn't help. Just slows the flashing process.
Was This Post Helpful? 0
  • +
  • -

#26 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 22 June 2013 - 02:07 PM

Are you sure that you are receiving the correct data at the right time? Right now your communications protocol assumes that for every time through your loop you are getting the following pieces of data in the correct sequence:
number of players connected
player X
player Y



But the pseudo code that you posted in Post #1 showed the server sending out:
player X
player Y


Was This Post Helpful? 0
  • +
  • -

#27 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 22 June 2013 - 08:39 PM

View PostSkydiver, on 22 June 2013 - 02:07 PM, said:

Are you sure that you are receiving the correct data at the right time? Right now your communications protocol assumes that for every time through your loop you are getting the following pieces of data in the correct sequence:
number of players connected
player X
player Y



But the pseudo code that you posted in Post #1 showed the server sending out:
player X
player Y



I tested this by having the Client send the Server the coordinants and players connected.
Then the Server reads it and states the coordinants (using cout << ...).
Everything seems to be correct.
I also made sure the client recieves the right info from the server by sending it back a second time to do the same.
It also has the correct information.
Was This Post Helpful? 0
  • +
  • -

#28 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 23 June 2013 - 09:48 AM

I don't know what else to say if you are sure that you are getting the correct data, other than:

Try replacing your lines of code that say:
player_rX = atoi(player_sX);
player_rY = atoi(player_sY);



with:
player_rX = player_x + 25;
player_rY = player_y + 25;



That should cause you to ignore the data you are getting from the server, and always have the other player's sprite shadow your player's sprite.

If the other player's sprite is still flashing, then that means that your rendering code is at fault and you need to post your rendering code so that we can help. If on the other hand, the other player's sprite stops flashing, then than means that your networking code is at fault and you need to post your complete client and server networking code because your tests obviously missed a few cases.
Was This Post Helpful? 0
  • +
  • -

#29 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 23 June 2013 - 02:30 PM

View PostSkydiver, on 23 June 2013 - 09:48 AM, said:

I don't know what else to say if you are sure that you are getting the correct data, other than:

Try replacing your lines of code that say:
player_rX = atoi(player_sX);
player_rY = atoi(player_sY);



with:
player_rX = player_x + 25;
player_rY = player_y + 25;



That should cause you to ignore the data you are getting from the server, and always have the other player's sprite shadow your player's sprite.

If the other player's sprite is still flashing, then that means that your rendering code is at fault and you need to post your rendering code so that we can help. If on the other hand, the other player's sprite stops flashing, then than means that your networking code is at fault and you need to post your complete client and server networking code because your tests obviously missed a few cases.


I did what you had asked and it seems to STOP the flashing.
I'm not sure what all you're expecting me to post, but here's my...

Client Network Code:
void s_handle(int s) {
    if(sock)
       closesocket(sock);
    if(client)
       closesocket(client);
    WSACleanup();
    exit(0);
}

void s_cl(char *a, int x) {
    s_handle(x+1000);
}

int main(int argc, char* argv[]) {
   fd_set fdread;
    
   HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN|FOREGROUND_INTENSITY);   

    //Declarations
    DWORD poll;
    int res,i=1,port=999;
    char buf[100];
    char msg[100] = "";
    char ip[15];
    WSADATA data;

    signal(SIGINT,s_handle);
    signal(SIGKILL,s_handle);
    signal(SIGQUIT,s_handle);

    sockaddr_in ser;
    sockaddr addr;

    ser.sin_family=AF_INET;
    ser.sin_port=htons(123);                    //Set the port
    ser.sin_addr.s_addr=inet_addr(MY IP HERE - Removed for personal reasons);      //Set the address we want to connect to

    memcpy(&addr,&ser,sizeof(SOCKADDR_IN));
    
    res = WSAStartup(MAKEWORD(1,1),&data);      //Start Winsock
 
    if(res != 0)
        gamerunning = false;

    sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);       //Create the socket

    if(sock==INVALID_SOCKET )
            gamerunning = false;
        else if(sock==SOCKET_ERROR)
            gamerunning = false;

    res=connect(sock,&addr,sizeof(addr));               //Connect to the server
    if(res !=0 ) {
            gamerunning = false;
    } else {
            memcpy(&ser,&addr,sizeof(SOCKADDR));
    }

    char RecvdData[100] = "";
    int reta;
    int ret;

    while(gamerunning == true) {
            
        fgets(buf,sizeof(buf),stdin);
         
        res = send(sock,buf,sizeof(buf),0); 

        if(res==0) {  
            //0 == Server Closed
            closesocket(client);
            client = 0;
            break;
        } else if(res==SOCKET_ERROR) {  
            //-1 == Send Error
            gamerunning = false;
            s_handle(res);
            break;
        }
       
        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)) {
                  reta = recv(sock,RecvdData,sizeof(RecvdData),0);
             }
        }
       if ((reta = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
                gamerunning = false;
       }
       
       if (FD_ISSET(sock, &fdread)) {
            gamerunning = true;
       }
       
       if(reta > 0) { 
            strcpy(RecvdData,"");
       }
        
    }
    SDL_Quit();
    closesocket(client);
    WSACleanup();
    return 0;
}


Server Code:

DWORD WINAPI receive_cmds(LPVOID lpParam) {
  printf("Thread created\r\n");

  // set our socket to the socket passed in as a parameter
  SOCKET current_client = (SOCKET)lpParam;

  // buffer to hold our recived data
  char buf[100];
  // buffer to hold our sent data
  char sendData[100];
  // for error checking
  int res;

  while(true) {
     res = recv(current_client,buf,sizeof(buf),0); // recv cmds

     if(res == 0) {
          MessageBox(0,"error","error",MB_OK);
          closesocket(current_client);
          ExitThread(0);
     }
     
     // clear buffers
     strcpy(sendData,"");
     strcpy(buf,"");
     }
}   

int main() {
    printf("Starting up the server\r\n");

    // our masterSocket(socket that listens for connections)
    SOCKET sock;
   
    // for our thread
    DWORD thread; 

    WSADATA wsaData;
    sockaddr_in server;

    // start winsock
    int ret = WSAStartup(0x101,&wsaData);
   
    if(ret != 0) {
         return 0;
    }

    // fill in winsock struct ... 
    server.sin_family=AF_INET; 
    server.sin_addr.s_addr=INADDR_ANY; 
    server.sin_port=htons(123);
    // create our socket
    sock=socket(AF_INET,SOCK_STREAM,0); 

    if(sock == INVALID_SOCKET) {
         return 0;
    }

    // bind our socket to a port(port 123) 
    if( bind(sock,(sockaddr*)&server,sizeof(server)) !=0 ) {
         return 0;
    }

    // listen for a connection  
    if(listen(sock,5) != 0) {
         return 0;
    }
    
    SOCKET client;
    sockaddr_in from;
    int fromlen = sizeof(from); 

    // loop forever 
    while(true) { 
         // accept connections
         client = accept(sock,(struct sockaddr*)&from,&fromlen);
         printf("Client connected\r\n");

         // create our recv_cmds thread and parse client socket as a parameter
         CreateThread(NULL, 0,receive_cmds,(LPVOID)client, 0, &thread);
     }

     closesocket(sock); 
     WSACleanup(); 

     return 0;
}


It's not my COMPLETE code so if anything is missing, feel free to ask!
I removed almost everything BUT the networking..
Was This Post Helpful? 0
  • +
  • -

#30 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: SDL and Winsock - Flashing images?

Posted 23 June 2013 - 04:04 PM

You stripped out too much code... I don't see any send() in your server code. I'm completely confused by lines 92-98 of your client code. Also what happened to your code that receives the number of players in the client code?
Was This Post Helpful? 0
  • +
  • -

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