The code in its current form works, (with the server that ONLY receives data, and the client only SENDING data) but if you uncomment the currently commented blocks (server receives data and sends it back, and the client the same), it doesn't send or receive anything anymore...
#include <iostream>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <winsock2.h>
using std::cout;
using std::cin;
class Player
{
public:
Player();
int x, y;
sf::RectangleShape shape;
};
Player::Player()
{
x = y = 0;
shape.setFillColor(sf::Color::White);
shape.setSize(sf::Vector2f(50, 100));
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
window.setVerticalSyncEnabled(true);
bool isServer = false;
cout << "Server/Client ? 1/0\n";
cin >> isServer;
if (!isServer)
{
sf::Int32 datax;
sf::Int32 datay;
Player player1, player2;
player2.shape.setFillColor(sf::Color::Red);
sf::TcpSocket socket;
sf::Socket::Status status = socket.connect("localhost", 53000);
if (status != sf::Socket::Done)
{
// error...
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::W)
{
player1.y--;
}
if (event.key.code == sf::Keyboard::S)
{
player1.y++;
}
if (event.key.code == sf::Keyboard::A)
{
player1.x--;
}
if (event.key.code == sf::Keyboard::D)
{
player1.x++;
}
}
}
sf::Packet packet2;
// if (socket.receive(packet2) != sf::Socket::Done)
// {
// cout << "Couldn't recieve data from server.\n";
// }
// else
// {
// cout << "Recieved data from server.\n";
// }
sf::Int32 enemydatax, enemydatay;
packet2 >> enemydatax, enemydatay;
player2.x = enemydatax;
player2.y = enemydatay;
player2.shape.setPosition(player2.x, player2.y);
player1.shape.setPosition(player1.x, player1.y);
window.clear(sf::Color::Black);
window.draw(player2.shape);
window.draw(player1.shape);
window.display();
//Send our clients position to the server
sf::Packet packet1;
datax = player1.x;
datay = player1.y;
packet1 << datax << datay;
// TCP socket:
if (socket.send(packet1) != sf::Socket::Done)
{
cout << "Couldn't send data to server.\n";
}
else
{
cout << "Sent data to server.\n";
}
}
}
else
{
sf::TcpListener listener;
sf::Int32 datax;
sf::Int32 datay;
Player player1, player2;
player2.shape.setFillColor(sf::Color::Red);
// bind the listener to a port
if (listener.listen(53000) != sf::Socket::Done)
{
cout << "Error: No connections.\n";
}
// accept a new connection
sf::TcpSocket client;
if (listener.accept(client) != sf::Socket::Done)
{
cout << "error client\n";
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::W)
{
player1.y--;
}
if (event.key.code == sf::Keyboard::S)
{
player1.y++;
}
if (event.key.code == sf::Keyboard::A)
{
player1.x--;
}
if (event.key.code == sf::Keyboard::D)
{
player1.x++;
}
}
}
sf::Packet packet2;
if (client.receive(packet2) != sf::Socket::Done)
{
cout << "Couldn't recieve data from client.\n";
}
else
{
cout << "Recieved data from client.\n";
}
sf::Int32 enemydatax, enemydatay;
packet2 >> enemydatax;
packet2 >> enemydatay;
cout << "enemy x: " << enemydatax << std::endl;
cout << "enemy y: " << enemydatay << std::endl;
player2.x = enemydatax;
player2.y = enemydatay;
player2.shape.setPosition(player2.x, player2.y);
player1.shape.setPosition(player1.x, player1.y);
window.clear(sf::Color::Black);
window.draw(player2.shape);
window.draw(player1.shape);
window.display();
//Send our clients position to the server
sf::Packet packet1;
datax = player1.x;
datay = player1.y;
packet1 << datax << datay;
// if (client.send(packet1) != sf::Socket::Done)
// {
// cout << "Couldn't send data to client.\n";
// }
// else
// {
// cout << "Data sent to client.\n";
// }
}
// use "client" to communicate with the connected client,
// and continue to accept new connections with the listener
}
}