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

Author Topic: Server send to any client and client reconnect.  (Read 1917 times)

0 Members and 3 Guests are viewing this topic.

TobiasSmollett

  • Newbie
  • *
  • Posts: 3
    • View Profile
Server send to any client and client reconnect.
« on: July 31, 2012, 08:46:42 pm »
Hello
I have a working server at last:

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
sf::SelectorTCP Selector;
sf::IPAddress Address;
sf::SocketTCP Listener;
sf::SocketTCP Socket;
unsigned int NbSockets;

void Listen(void*){
 Selector.Add(Listener);

    while (true){

    NbSockets = Selector.Wait();

    for (unsigned int i = 0; i < NbSockets; ++i)
    {
    // Get the current socket
    Socket = Selector.GetSocketReady(i);;

    if (Socket == Listener)
    {
        // If the listening socket is ready, it means that we can accept a new connection

        sf::SocketTCP Client;
        Listener.Accept(Client, &Address);
        std::cout << "Client connected ! (" << Address << ")" << std::endl;

        // Add it to the selector
        Selector.Add(Client);
    }else{
        // Else, it is a client socket so we can read the data he sent
        char Message[128]; //128
        std::size_t Received;
            Socket = Selector.GetSocketReady(0);;
            if (Socket.Receive(Message, sizeof(Message), Received) == sf::Socket::Done){
            // Show the message
            std::cout << "Message received from the client : \"" << Message << "\"" << std::endl;


        }
    }
    }
    }
    }



void Send(void*){
char ToSend[128];
    for(;;){
        std::cin>>ToSend;
        if (Socket.Send(ToSend, sizeof(ToSend)) != sf::Socket::Done)
        {
        std::cout << "Message sent to the client : \"" << ToSend << "\"" << "to" << &Socket << std::endl;
        }
        }
    }
    // Choose a random port for opening sockets (ports < 1024 are reserved)


int main()
{


    if (!Listener.Listen(4567)){
    // Error...
    }
    sf::Thread Thread1(&Listen);
    sf::Thread Thread2(&Send);
    Thread1.Launch();
    Thread2.Launch();
    return 0;
}



 

I do however have two problems.
1) How can i select which client  to send the string to?
2)How can i allow a client to reconnect after it has disconnected?

Thanks in advance.
TobiasSmollett


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Server send to any client and client reconnect.
« Reply #1 on: July 31, 2012, 09:49:25 pm »
Quote
1) How can i select which client  to send the string to?
You must keep a list of all the connected clients (sockets). If you just add them to the selector, you'll only be able to receive from them.

Quote
2)How can i allow a client to reconnect after it has disconnected?
Why would 2nd, 3rd, etc. connections from a client be different from the first one? What's the problem?
Laurent Gomila - SFML developer

 

anything