113 Replies - 6620 Views - Last Post: 04 July 2013 - 11:15 PM
#31
Re: SDL and Winsock - Flashing images?
Posted 23 June 2013 - 04:12 PM
#32
Re: SDL and Winsock - Flashing images?
Posted 25 June 2013 - 02:07 PM
You said that I stripped out too much code, But I don't think I did
Anyways, Here's the send and receive part, I think I stated before:
Server:
while(true) {
itoa(clients_Xconnected,clients_connected,10);
send(current_client,clients_connected,sizeof(clients_connected),0);
recv(current_client,player_sX,sizeof(player_sX),0);
send(current_client,player_sX,sizeof(player_sX),0);
recv(current_client,player_sY,sizeof(player_sY),0);
send(current_client,player_sY,sizeof(player_sY),0);
//ect...
Client:
while(gamerunning == true) {
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);
}
}
if (client_connected_frame == 0) {
client_connected = atoi(client_Xconnected);
client_connected_frame +=1;
}
amount_of_players = atoi(client_Xconnected);
if (amount_of_players_frame == client_connected) {
amount_of_players_frame +=1;
}
if (amount_of_players_frame > amount_of_players) {
amount_of_players_frame = 0;
}
//Moving code (Up,Down,Left,Right)
itoa(player_x,player_sX,10);
send(sock,player_sX,sizeof(player_sX),0);
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);
}
}
player_rX = atoi(player_sX);
itoa(player_y,player_sY,10);
send(sock,player_sY,sizeof(player_sY),0);
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);
}
}
player_rY = atoi(player_sY);
add_image(0,0,0,0,800,600,background,screen);
add_image(player_x,player_y,0,0,32,32,player,screen);
if (player_rX != player_x && player_rY != player_y) {
gamerunning = true;
} else {
gamerunning = false;
}
add_image(player_rX,player_rY,0,0,32,32,player,screen);
//ect...
Anything else I may have left out, Please let me know!
Thanks,
-Fysez
#33
Re: SDL and Winsock - Flashing images?
Posted 25 June 2013 - 05:34 PM
struct mydata
{
int connected;
int x, y;
};
and a function to read :
bool my_receive( SOCKET &sock, struct mydata &data)
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(sock, &fdread);
bool gamerunning = true;
if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
gamerunning = false;
}
if (ret > 0) {
if (FD_ISSET(sock, &fdread)) {
recv(sock, data, sizeof(struct mydata), 0);
}
return gamerunning;
}
perhaps one to send as well.
#34
Re: SDL and Winsock - Flashing images?
Posted 25 June 2013 - 09:16 PM
#define, on 25 June 2013 - 05:34 PM, said:
struct mydata
{
int connected;
int x, y;
};
and a function to read :
bool my_receive( SOCKET &sock, struct mydata &data)
fd_set fdread;
FD_ZERO(&fdread);
FD_SET(sock, &fdread);
bool gamerunning = true;
if ((ret = select(0, &fdread, NULL, NULL, NULL)) == SOCKET_ERROR) {
gamerunning = false;
}
if (ret > 0) {
if (FD_ISSET(sock, &fdread)) {
recv(sock, data, sizeof(struct mydata), 0);
}
return gamerunning;
}
perhaps one to send as well.
Okay thanks =P
I'll test around with this and let you know my results
(Only said this to let you know I read it).
-Fysez
#35
Re: SDL and Winsock - Flashing images?
Posted 25 June 2013 - 10:53 PM
This is making me draw a blank and I know it's a dumb question.
But how do I receive all of the things in the struct?
For example, Here's my send(...) code:
struct send_data1 {
int send_clients_connected1;
int send_player_x1;
int send_player_y1;
};
send_data1 SD1;
SD1.send_clients_connected1 = clients_connected;
SD1.send_player_x1 = player_x;
SD1.send_player_y1 = player_y;
send(current_client, (char *)&SD1, sizeof(send_data1), 0);
First of all... Is this even correct?
Second,
How do I receive this?
Here's what I'm having troubles on:
bool my_receive(SOCKET &sock, struct mydata &data);
fd_set fdread;
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, data, sizeof(struct mydata), 0);
}
}
And I get the following errors:
cannot convert `data' from type `WSADATA' to type `char*'
and
invalid application of `sizeof' to incomplete type `mydata'
My mind is losing me when I think of this.
Question:
After I do the recv(...)
how do I add each 'int' individually like int receivedX = player_xInsideOfStruct?
Sorry for my "noob-ish" reply.
But thank you for letting me know what else I could do:)
-Fysez
P.S.
Would adding all of this info into a structure an sending it that way fix my problem? Or is this just to fix up my code a bit and use less lines?
Thanks again
This post has been edited by fysez: 25 June 2013 - 10:54 PM
#36
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 05:25 AM
recv(..., static_cast<char *>(&data), ...)
#37
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 05:38 AM
Consider what happens in this timeline when you used to send things separately
you server
: :
send connected
receive X
try get connected
receive connected
try get X <-- no X available because server is still blocked
send X
try get Y <-- Y now contains X.
recieve Y
: :
#38
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 09:32 AM
struct Package
{
int clients_connected;
int player_x;
int player_y;
};
#define PACKAGE_SIZE sizeof(struct Package)
int main()
{
...
}
The struct Package can be updated with minimal effect on already written code.
Using the struct.
int main()
{
...
struct Package package_receive;
struct Package package_send;
int player_x_variable;
// update package
package_send.clients_connected = 1;
// send package
ret = send(client,
reinterpret_cast<char *> (&package_send),
PACKAGE_SIZE , 0);
...
// receive package
ret = recv(client,
reinterpret_cast<char *> (&package_receive),
PACKAGE_SIZE , 0);
// update variable
player_x_variable = package_receive.player_x;
}
fysez, on 26 June 2013 - 06:53 AM, said:
Thanks again
I'm not sure why the graphics are flashing. It looks as if the x and y could be swapping (x and y getting read at wrong time - the struct will help there), it could be something to do with your code for SDL, or both. You could try using SDL_Delay (maybe 500ms to 1000ms) temporarily after the flip, to slow the graphics down which might give a clue to what's happening.
The struct simplifies code and functions simplify code making it easier to write and debug. The struct helps you to be certain which piece of data you are receiving.
~
This post has been edited by #define: 26 June 2013 - 02:11 PM
#39
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 12:41 PM
#define, on 26 June 2013 - 09:32 AM, said:
struct Package
{
int clients_connected;
int player_x;
int player_y;
};
#define PACKAGE_SIZE sizeof(struct Package)
int main()
{
...
}
The struct Package can be updated with minimal effect on already written code.
Using the struct.
int main()
{
...
struct Package package_receive;
struct Package package_send;
int player_x_variable;
// update package
package_send.clients_connected = 1;
// send package
ret = send(client, reinterpret_cast<char *>(&package_send),
PACKAGE_SIZE , 0);
...
// receive package
ret = recv(client, reinterpret_cast<char *>(&package_receive),
PACKAGE_SIZE , 0);
// update variable
player_x_variable = package_receive.player_x;
}
Hi! Thanks for your awesome reply!
Although I am not using the "Package" structure, I am trying to do just the regular send and receive struct.
I do have another question on this (Yes, I tried to look it up first: No results).
Here's my...
Send Code:
struct send_data1 {
int send_clients_connected1;
int send_player_x1;
int send_player_y1;
};
send_data1 SD1;
SD1.send_clients_connected1 = clients_connected;
SD1.send_player_x1 = player_x;
SD1.send_player_y1 = player_y;
send(current_client, (char *)&SD1, sizeof(send_data1), 0);
Receive Code:
bool my_receive(SOCKET &sock, struct mydata &data);
fd_set fdread;
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, reinterpret_cast<char *>(&data), sizeof(&my_receive), 0);
}
}
int RC1 = data.send_clients_connected1;//This is my problem
I beleive that my sending and receiving works correctly (Not tested - But I don't get errors)
However, My last line for receiving; I get the following error:
'struct WSADATA' has no member named 'send_clients_connected1'
I'm obviously doing SOMETHING wrong here xD
Thanks again for your help, you two.
I don't say it often, But I seriously appreciate the help you're giving me.
Without you two, I'd be COMPLETELY lost.
-Fysez
#40
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 01:04 PM
11 recv (current_client, (char *)&SD1,
sizeof(send_data1), 0);
If you are creating a function it needs to be outside of main. The structure name has changed.
bool my_receive(SOCKET &sock, struct send_data1 &data)
{
}
int main()
{
...
}
#41
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 10:20 PM
So I'm going to try to sum up what I said:
I'm stressing out over these problems, It's taking me SO long and it's SO hard to understand this simple problem..
But I'm still getting errors.
I tried this:
bool recv_data1(SOCKET &sock, struct send_data1 &RD1) {
int recv_clients_connected1;
int recv_player_x1;
int recv_player_y1;
}
int main(int argc, char* argv[]) {
...
while (gamerunning) {
fd_set fdread;
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, reinterpret_cast<char *>(&RD1), sizeof(&recv_data1), 0);
}
}
}
}
and got the error:
`RD1' undeclared (first use this function)
So then I tried adding:
int main() {
...
struct RD1;
before all of that, And got the error:
expected primary-expression before ')' token
Again, I'm stressing out over this, I just want to rip my hair out lol.
Sorry if I seemed to ignore any parts of your reply,
I'm over-complicating this for myself.
I feel like I've tried everything and it seems to fail.
Thanks, Again, for the help. Sorry i'm so difficult.
-Fysez
#42
Re: SDL and Winsock - Flashing images?
Posted 26 June 2013 - 10:59 PM
C:
struct send_data1 RD1;
C++:
send_data1 RD1;
I recommend this tutorial to try to catch yourself up: http://www.cplusplus...ial/structures/
I already see that your recv_data1 is being seen by the compiler as a function, but you seem to be writing it almost like a structure.
#43
Re: SDL and Winsock - Flashing images?
Posted 27 June 2013 - 09:44 AM
Skydiver, on 26 June 2013 - 10:59 PM, said:
C:
struct send_data1 RD1;
C++:
send_data1 RD1;
I recommend this tutorial to try to catch yourself up: http://www.cplusplus...ial/structures/
I already see that your recv_data1 is being seen by the compiler as a function, but you seem to be writing it almost like a structure.
Really? Because once I try
send_data1 RD1
I get the error:
`send_data1 RD1' has incomplete type and cannot be defined
Structures are easy, I can do them fine.
RECEIVING THEM on the other hand, Is not the same.
Everything I look up, Do what you guys tell me to do, Ect. gives me errors.
Doesn't seem to work.
-Fysez
This post has been edited by fysez: 27 June 2013 - 09:45 AM
#44
Re: SDL and Winsock - Flashing images?
Posted 27 June 2013 - 10:29 AM
bool recv_data1(SOCKET &sock, struct send_data1 &RD1) {
int recv_clients_connected1;
int recv_player_x1;
int recv_player_y1;
}
#45
Re: SDL and Winsock - Flashing images?
Posted 27 June 2013 - 02:50 PM
#define, on 27 June 2013 - 10:29 AM, said:
bool recv_data1(SOCKET &sock, struct send_data1 &RD1) {
int recv_clients_connected1;
int recv_player_x1;
int recv_player_y1;
}
So should I do this?:
bool my_receives(SOCKET &sock, struct send_data1 &RD1) {
struct recv_data1 {
int recv_clients_connected1;
int recv_player_x1;
int recv_player_y1;
};
}

New Topic/Question
Reply



MultiQuote


|