Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Client->Server->Clients  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

mbestf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Client->Server->Clients
« on: June 02, 2014, 11:26:03 pm »
Hi, I need some help. I have server which can hold multiple clients. One client sends info to the server and server sends that info to the other clients (coordinates, name).. But the problem is that client can't see other clients on the map.. And I dont know how to do that... Can you help me a bit ?

server:
#include<SFML/Graphics.hpp>
#include<SFML/Network.hpp>
#include<string>
#include<iostream>
#include<conio.h>
#include<vector>
int main()
{
        std::cout << "Server Running" << std::endl;
        sf::TcpListener listener;
        sf::SocketSelector selector;
        std::vector<sf::TcpSocket*> clients;
        listener.listen(2000);
        selector.add(listener);

        while (1)
        {
                if (selector.wait())
                {
                        if (selector.isReady(listener))
                        {
                                sf::TcpSocket *socket = new sf::TcpSocket;
                                listener.accept(*socket);
                                sf::Packet packet;
                                std::string Name;
                                float xCoord, yCoord;

                                if (socket->receive(packet) == sf::Socket::Done) packet >> xCoord >> yCoord >> Name;
                                std::cout << Name << " has connected to the chat room (x:" << xCoord << ", y:" << yCoord << ")" << std::endl;

                                clients.push_back(socket);
                                selector.add(*socket);
                        }
                        else
                        {
                                for (int i = 0; i < clients.size(); i++)
                                {
                                        if (selector.isReady(*clients[i]))
                                        {
                                                sf::Packet packet, sendPacket;
                                                if (clients[i]->receive(packet) == sf::Socket::Done)
                                                {
                                                        float PosX, PosY;
                                                        std::string Name;
                                                        packet >> PosX >> PosY >> Name;
                                                        sendPacket << PosX << PosY << Name;

                                                        std::cout << "[" << Name << "]" << "[x:" << PosX << "]" << "[y:" << PosY << "]" << endl;

                                                        for (int j = 0; j < clients.size(); j++)
                                                        {
                                                                if (i != j)
                                                                {
                                                                        clients[j]->send(sendPacket);
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }

        for (std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); it++)
                delete *it;

        return 0;
}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Client->Server->Clients
« Reply #1 on: June 02, 2014, 11:39:38 pm »
From such a short explanation we can't really tell what you're trying to achieve, but it sounds like whenever a client connects to the server, the server should send it a message containing everyone's current positions, and send messages to all existing clients about the new client.

By the way, why use pointers to TcpSockets instead of just TcpSockets?  Memory should never be managed manually without a good reason.

mbestf

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Client->Server->Clients
« Reply #2 on: June 03, 2014, 01:01:39 am »
main.cpp:
std::string MyName, IP = "xx.xx.xx.xx";
sf::TcpSocket Client;
cout << "Tipe your name: ";
cin >> MyName;
Client.connect(IP, xxxx);
vector<PLAYER> Players;

Client.setBlocking(false);
sf::Packet Packet;
PLAYER MainPlayer;
MainPlayer.SetName(MyName);
Players.push_back(MainPlayer);
Packet << Players[0].GetPosition().x << Players[0].GetPosition().y << Players[0].GetPlayerName();
Client.send(Packet);

float xCord = Players[0].GetLastCoordiantes().x, yCord = Players[0].GetLastCoordiantes().y;

while (GameWindow.isOpen())
{
sf::Packet GetPacket;
float PosX, PosY;
string Name;
Client.receive(GetPacket);
if (GetPacket >> PosX >> PosY >> Name)
{
cout << "Player Name: " << Name << " x:" << PosX << " y:" << PosY << endl;
}

if (Players[0].GetPosition().x != xCord || Players[0].GetPosition().y != yCord)
{
sf::Packet SendPacket;
SendPacket << Players[0].GetPosition().x << Players[0].GetPosition().y << Players[0].GetPlayerName();
Client.send(SendPacket);
}
xCord = Players[0].GetLastCoordiantes().x, yCord = Players[0].GetLastCoordiantes().y;

Players[0].Draw(GameWindow);
GameWindow.display();
GameWindow.clear();

}
 

I need to draw new clients, but how I should do this ?