1
System / passing SocketTCP to thread problem
« on: December 24, 2011, 12:51:31 pm »
Thanks ;p iam so stupid i forgot this
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
void Client_Listen_Thread(void* UserData)
{
sf::SocketTCP Server = static_cast<sf::SocketTCP>(UserData);
[...] blabla
sf::Thread ListenThread(&Client_Listen_Thread,&Server);
class klient
{
public:
sf::SocketTCP sock;
sf::IPAddress adres;
string nick;
int ID;
klient(sf::SocketTCP soc, sf::IPAddress adr,string nck, int id)
{
sock = soc;
adres = adr;
nick = nck;
ID = id;
}
};
klient *klienci[10]; //array of clients
int klientnum = 0; //number of clients
[...]
here is that selector thing from tutorial, Socket==Listener etc etc
[...]
sf::IPAddress Address;
sf::SocketTCP Client;
Listener.Accept(Client, &Address);
std::cout << "Client connected ! (" << Address << ")" << std::endl;
klienci[klientnum+1] = new klient(Client,Address,"default",klientnum+1); // add client data to clients array
klientnum+=1; //increse number of clients
Selector.Add(Client); //add client to selector
[...] its still in the selectors loop
{
sf::Packet Packet;
if (Socket.Receive(Packet) == sf::Socket::Done)
{
std::string Message;
Packet >> Message;
std::cout << "A client says : \"" << Message << "\"" << std::endl;
Socket.Send(Packet) == sf::Socket::Done;
//---------------------------here we go-----------------------------------
for(int i=1;i<klientnum;i++) //loop trough array of clients
{
if(klienci[i]->sock!=Socket) //if socket is not the same socket
{klienci[i]->sock.Send(Packet) == sf::Socket::Done;} //send data
}
}
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(640, 480, 32), "SFML Window");
sf::Event Event;
while(App.IsOpened())
{
while(App.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
{
App.Close();
}
}
App.Clear();
App.Display();
}
return EXIT_SUCCESS;
}