This is the server sc:
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
using namespace sf;
class Server
{
private:
SocketTCP InSocket;
SocketTCP OutSocket;
unsigned short ClientPort;
IPAddress ClientAddress;
char RecievedData[10];
size_t RecievedDataSize;
public:
Server();
~Server();
void RecieveData()
{
InSocket.Receive( RecievedData, sizeof(RecievedData), RecievedDataSize);
}
string WaitConnections(int port);
bool StartServer();
};
bool run = true;
Server *server= NULL;
Server::Server()
{
InSocket.SetBlocking(true);
OutSocket.SetBlocking(true);
}
Server::~Server()
{
}
string Server::WaitConnections(int port)
{
if(!InSocket.Listen(port))
return "ERROR";
cout << "Waiting for connections on port " << port << endl;
InSocket.Accept(OutSocket, &ClientAddress);
cout << "Client connected: " << ClientAddress << endl;
cout << "Connection established!" << endl;
}
bool Server::StartServer()
{
RecieveData();
cout << "Data from client: " << RecievedData << endl;
cout << "ALL IS GOOD!" << endl;
}
int main()
{
server= new Server();
server->WaitConnections(1025);
server->StartServer();
system("pause");
}
And this is the client sc:
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
using namespace sf;
class Client
{
private:
SocketTCP ClientSocket;
unsigned short ServerPort;
IPAddress ServerAddress;
char ToSendData[10];
public:
Client();
~Client();
void InitClient(string address, unsigned short port);
bool StartClient();
};
bool run = true;
string server_address;
Client *client= NULL;
Client::Client()
{
}
Client::~Client()
{
}
void Client::InitClient(string address, unsigned short port)
{
ServerAddress= address;
ServerPort= port;
}
bool Client::StartClient()
{
cout << "Connecting on server at address " << ServerAddress << endl;
do
{
ClientSocket.Connect(ServerPort, ServerAddress);
}
while (ClientSocket.Connect(ServerPort, ServerAddress)== Socket::Disconnected );
cout << "You have been connected to the server!" << endl;
cout << "Enter the data to send to the server: ";
cin >> ToSendData;
ClientSocket.Send( ToSendData, sizeof(ToSendData) );
}
int main()
{
client= new Client();
client->InitClient("127.0.0.1", 1025);
client->StartClient();
system("pause");
}
If you have any question tell me.