SDLNet - Advice on Sending and Receiving Packets

Started by
7 comments, last by Rasterman 11 years, 10 months ago
I've been working on a server for a game I'm working on. I'm having some trouble with sending and receiving packets...

Here's my sending code...

SDLNet_TCP_Send(sock, "EX_DC", 6);
/* *** this is placed at the end of int main(), to determine a disconnect. (EX_DC = Exiled(prefix), Disconnect(action). *** */


...and here's my receiving code.

for(int x = 0; x < connectedPlayers; x++) {
int ready = SDLNet_CheckSockets(player_sockSet, 0);
if(ready != -1) {
result = SDLNet_TCP_Recv(players[x].sock, msg, 256);
consoleOut("Received data!");
}
}

//This is placed inside of a for loop that loops through an array of clients.


When I send the data, the server doesn't print anything to the console. What am I doing wrong?
Advertisement
Uhh, Ive never used SDL but where is the call to consoleOut? there is no other methods/calls in the sending code you have provided?
SDLNet_TCP_Send(sock, "EX_DC", 6);
/* *** this is placed at the end of int main(), to determine a disconnect. (EX_DC = Exiled(prefix), Disconnect(action). *** */
I think the receive call waits until 256 bytes are complete.
The ready variable should contain the amount of bytes that are ready for receiving.
If working with streams always be aware that you can receive parts of a message the other ends sends to you.
So you may receive 3 Bytes in the first try another 3 bytes in second.
You check if player_socket has data available, but then you try to receive from player[x].sock. I believe you should change the check to player[x].sock

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


You check if player_socket has data available, but then you try to receive from player[x].sock. I believe you should change the check to player[x].sock


I check if player_sockSet has data available. A sock set is exactly what it states: a socket set. I have a socket set of all the player sockets... I check it every loop of the for loop.

Uhh, Ive never used SDL but where is the call to consoleOut? there is no other methods/calls in the sending code you have provided?
SDLNet_TCP_Send(sock, "EX_DC", 6);
/* *** this is placed at the end of int main(), to determine a disconnect. (EX_DC = Exiled(prefix), Disconnect(action). *** */


consoleOut(); is just a function I made for outputting stuff to a console I made with graphics rather than a console window. I'm actually using Allegro 5 for graphics and SDL_net for networking.

[quote name='BeerNutts' timestamp='1338908351' post='4946461']
You check if player_socket has data available, but then you try to receive from player[x].sock. I believe you should change the check to player[x].sock


I check if player_sockSet has data available. A sock set is exactly what it states: a socket set. I have a socket set of all the player sockets... I check it every loop of the for loop.
[/quote]

My bad, I read it as "player_socket" not "player_sockSet"

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)


[quote name='Exiled' timestamp='1338922235' post='4946545']
[quote name='BeerNutts' timestamp='1338908351' post='4946461']
You check if player_socket has data available, but then you try to receive from player[x].sock. I believe you should change the check to player[x].sock


I check if player_sockSet has data available. A sock set is exactly what it states: a socket set. I have a socket set of all the player sockets... I check it every loop of the for loop.
[/quote]

My bad, I read it as "player_socket" not "player_sockSet"
[/quote]

Ah, it's all good.
try sending a lot more data, or using some kind of flush calls, maybe its waiting for a buffer to be filled. use wireshark on the client and server to see if anything is actually being sent or received.

This topic is closed to new replies.

Advertisement