I haven't tested client -> server -> client yet, just client -> server for now.
Unfortunately, I'm only able to send one message per connection.
Clientint main()
{
sf::SocketTCP Client;
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(480, 320, 32), "Window");
const sf::Input& Input = App.GetInput();
cout << "Type LEFT to connect, RIGHT to send a message, UP to disconnect" << endl;
// Start game loop
while (App.IsOpened())
{
// Process events
sf::Event Event;
while (App.GetEvent(Event))
{
if(Input.IsKeyDown(sf::Key::Left)){
if (Client.Connect(4567, "127.0.0.1") != sf::Socket::Done)
{
// Error...
cout << "error" << endl;
}
else cout << "connected" << endl;
}
else if(Input.IsKeyDown(sf::Key::Right)){
cout << "right pressed" << endl;
char Buffer[] = "Hi guys !";
if (Client.Send(Buffer, sizeof(Buffer)) != sf::Socket::Done)
{
// Error...
cout << "error sending message" << endl;
}
}
else if(Input.IsKeyDown(sf::Key::Up)){
Client.Close();
cout << "disconnected" << endl;
}
// Close window : exit
if (Event.Type == sf::Event::Closed)
App.Close();
}
// Clear the screen (fill it with black color)
App.Clear(sf::Color(255, 255, 255));
// Display window contents on screen
App.Display();
}
return EXIT_SUCCESS;
}
Left button connects, right button sends a message: "Hi guys !", up button disconnects/closes the socket.
Now on my server...
Servervoid ThreadFunction(void* UserData)
{
Server *server = (Server*)UserData;
char Buffer[128];
std::size_t Received;
while(true){
sf::IPAddress ClientAddress;
if (server->Listener.Accept(server->Client, &ClientAddress) != sf::Socket::Done)
{
// Error...
//cout << "Error listening..." << endl;
}
else{
cout << "Got a connection!" << endl;
}
if (server->Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
{
// Error...
continue;
}
else{
cout << "Message: " << Buffer << endl;
}
}
}
I can connect, and send a message, but just
one message.
If I disconnect and connect again, I can send a message, but only one.
Not sure what's going on. I should be able to send multiple ones.