I just started working on a simple client/server chat program, right now I can connect using the client, no problem here, I send a message to the server and here is where I get disconnected after that, along side I cannot type a new message in on the client either, why is this happening? What am I missing? I have looked at the tutorial and sample, both those have same issue so I don't know what to do to loop it right, any suggestions would be helpful, thank you.
Client code:
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
using namespace std;
//Create the TCP socket
sf::SocketTCP Server;
sf::SocketTCP Client;
//Set the port to listen on
const unsigned short Port = 2435;
int main()
{
//Ask user for IP address of the server to connect to
sf::IPAddress ServerAdd;
do
{
cout << "Enter server IP: ";
cin >> ServerAdd;
cout << endl;
if(!ServerAdd.IsValid())
cout << "Error: " << "Invalid IP address..." << endl;
}while(!ServerAdd.IsValid());
//Connect to the server specified by IP address
if(Client.Connect(Port, ServerAdd) != sf::Socket::Done)
cout << "Error connecting to server: " << ServerAdd << endl;
cout << "Connected to server " << ServerAdd << endl;
while(true) //I know this is bad practice, I will fix later!
{
//Connect to the server specified by IP address
if(Client.Connect(Port, ServerAdd) != sf::Socket::Done)
cout << "Error connecting to server: " << ServerAdd << endl;
//Receive a message from the server
char Message[255];
std::size_t Received;
if(Client.Receive(Message, sizeof(Message), Received) != sf::Socket::Done)
cout << "Error receiving message from server: " << ServerAdd << endl;
else
cout << "User: " << Message << endl;
//Send a message to the server
char Sent[255];
cout << "You: ";
cin >> Sent;
cout << endl;
if(Client.Send(Sent, sizeof(Sent)) != sf::Socket::Done)
cout << "Error sending message to server: " << ServerAdd << endl;
}
return EXIT_SUCCESS;
}
Server code:
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
using namespace std;
//Create the TCP socket
sf::SocketTCP Server;
//Set the port to listen on
const unsigned short Port = 2435;
int main()
{
//Open a socket and listen to a specified port
if(!Server.Listen(Port))
cout << "Error listing on Port: " << Port << endl;
cout << "Server is listening to port " << Port << ", waiting for connections..." << endl;
while(true) //I know this is bad practice, I will fix later!
{
//Wait for a connection to be established by a client
sf::IPAddress ClientAdd;
sf::SocketTCP Client;
if(Server.Accept(Client, &ClientAdd) != sf::Socket::Done)
cout << "Error not ready for listening..." << endl;
else
{
cout << "Client connected: " << ClientAdd << endl;
char buffer[] = "Connected to Server";
if(Client.Send(buffer, sizeof(buffer)) != sf::Socket::Done)
cout << "Error sendind message to client: " << ClientAdd << endl;
}
char Buffer[128];
std::size_t Received;
if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
cout << "Error receiving message from client: " << ClientAdd << endl;
else
cout << ClientAdd << ": " << Buffer << endl;
}
return EXIT_SUCCESS;
}