SFML community forums
Help => Network => Topic started by: Dwarfius on January 25, 2012, 04:25:48 pm
-
I used the code on this page to make a server with sfml 2.0 : http://www.sfml-dev.org/documentation/2.0/classsf_1_1SocketSelector.php
This is where I have a problem :
// The client has sent some data, we can receive it
sf::Packet packet;
if (client.Receive(packet) == sf::Socket::Done)
{
// Extract the message and display it
std::string Message;
packet >> Message;
std::cout << "A client says : \"" << Message << "\"" << std::endl;
}
The program is stuck on the Receive method. This is the code for the client :
// Create a socket for communicating with the server
sf::TcpSocket socket;
socket.Connect("127.0.0.1", 1000);
// Send a message to the server
const char out[] = "Hi, I'm a client";
socket.Send(out, sizeof(out));
std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;
// Receive an answer from the server
char buffer[1024];
std::size_t received = 0;
socket.Receive(buffer, sizeof(buffer), received);
std::cout << "The server said: " << buffer << std::endl;
I run both programs on the same computer. The client is also stuck on the Receive method.
Thanks for any help.
-
First, check all the status codes returned by socket functions, and make sure that there's no error.
-
I put a breakpoint on the Receive line in the server, but when I step over nothing happens. The Socket is neither Done, NotReady, Disconnected or Error. The program doesn't end either. I'm not sure what is going on...
-
Can you please post a complete and minimal code that reproduces the problem?
-
Your server use packet while your client use the lower level implementation which is directly using a buffer with a size.
How the packet class work in SFML, it first send a size then the actual data.
So since you aren't using packet on the client there is somewhat a mismatch between what the client send and what the server except to receive, either use packet on both side or the lower level sockets on both side.
Note that what I said may not be totally correct, I didn't look at the source to be sure of that. Laurent might be able to answer more accurately.
-
Yes, you're right ;)