Hey!
A freind of me is trying out some of the networking functions of SFML, this example works perfectly on his Windows computers and he thought i might give it a go and compile it:
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
const bool DEBUG = true;
const unsigned int PORTTCP = 4567;
const unsigned int PORTUDP = 4568;
struct Client
{
sf::SocketTCP socket;
sf::IPAddress address;
};
bool running = true;
string errMess;
sf::Mutex globalMutex;
vector<Client> connClients;
vector<sf::SocketTCP> connSendClients;
void handleConn(void *UserData)
{
if(DEBUG)
{
globalMutex.Lock();
cout << "\tDEBUG: Entering thread: \"handleConn()\"." << endl;
globalMutex.Unlock();
}
sf::SocketTCP listener;
if(!listener.Listen(PORTTCP))
{
running = false;
errMess = "Failed listening to port!";
return;
}
cout << "Listening to port " << PORTTCP << ". Waiting for TCP connections..." << endl;
sf::SelectorTCP selector;
selector.Add(listener);
while(running)
{
unsigned int NbSockets = selector.Wait(5.f);
for(unsigned int i = 0; i < NbSockets; i++)
{
sf::SocketTCP socket = selector.GetSocketReady(i);
//New connection.
if(socket == listener)
{
sf::IPAddress address;
sf::SocketTCP client;
listener.Accept(client, &address);
globalMutex.Lock();
cout << "Client connected ! (" << address << ")" << endl;
globalMutex.Unlock();
selector.Add(client);
Client tmpClient;
tmpClient.socket = client;
tmpClient.address = address;
connClients.push_back(tmpClient);
sf::SocketTCP tmpSocket;
if(!tmpSocket.Connect(PORTTCP, address))
{
globalMutex.Lock();
cout << "Unable to connect to client! (" << connClients[i].address.ToString() << ")" << endl;
globalMutex.Unlock();
}
connSendClients.push_back(tmpSocket);
}
else
{
sf::Packet packet;
if(socket.Receive(packet) == sf::Socket::Done)
{
sf::Int8 isMess;
string clientName;
string mess;
packet >> isMess >> clientName >> mess;
if(isMess == 0)
{
if(mess == "quit")
{
sf::IPAddress droppedIP;
int q;
for(unsigned int i = 0; i < connClients.size(); i++)
{
if(socket == connClients[i].socket)
{
droppedIP = connClients[i].address;
q = i;
}
}
globalMutex.Lock();
cout << "Client dropped! (" << droppedIP.ToString() << ")" << endl;
globalMutex.Unlock();
selector.Remove(socket);
globalMutex.Lock();
connClients.erase(connClients.begin()+q);
globalMutex.Unlock();
}
}
if(isMess == 1)
{
if(DEBUG)
{
globalMutex.Lock();
cout << "Message recieved!" << endl;
globalMutex.Unlock();
}
globalMutex.Lock();
cout << clientName << ": " << mess << endl;
globalMutex.Unlock();
if(DEBUG)
{
globalMutex.Lock();
cout << "\tDEBUG: connSendClients.size(): " << connSendClients.size() << endl;
globalMutex.Unlock();
}
for(unsigned int i = 0; i < connSendClients.size(); i++)
{
if(connSendClients[i].Send(packet) != sf::Socket::Done)
{
globalMutex.Lock();
cout << "Unable to send packet to client! (" << connClients[i].address.ToString() << ")" << endl;
globalMutex.Unlock();
}
}
}
}
}
}
globalMutex.Lock();
cout << "...waiting!" << endl;
globalMutex.Unlock();
}
for(unsigned int i = 0; i < connSendClients.size(); i++)
{
connSendClients[i].Close();
}
}
int main()
{
sf::RenderWindow app(sf::VideoMode(800, 600, 32), "Julles chatt server.");
sf::Thread connThread(&handleConn);
connThread.Launch();
while(app.IsOpened())
{
sf::Event Event;
while (app.GetEvent(Event))
{
// Close window : exit
if(Event.Type == sf::Event::Closed)
app.Close();
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
app.Close();
}
app.Display();
}
running = false;
connThread.Wait();
return 0;
}
It compiles in OSX, but at runtime i get this:
...waiting!
Client connected ! (83.227.232.222)
...waiting!
SFML2(1725,0xb0147000) malloc: *** error for object 0xa02056d8: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
SFML2(1725,0xb0147000) malloc: *** error for object 0xa02056d8: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
Message recieved!
blabla: tjo din keffade grek
DEBUG: connSendClients.size(): 1
Unable to send packet to client! (83.227.232.222)
...waiting!
SFML2(1725,0xb0147000) malloc: *** error for object 0xa02056d8: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
SFML2(1725,0xb0147000) malloc: *** error for object 0xa02056d8: Non-aligned pointer being freed
*** set a breakpoint in malloc_error_break to debug
Message recieved!
blabla: jkhdfjkdh
DEBUG: connSendClients.size(): 1
Unable to send packet to client! (83.227.232.222)
...waiting!
Program received signal: “SIGPIPE”.
Xcode: Introspection dylib not loaded because thread 3 has function: malloc_printf on stack
(gdb)
As said, this does not occur on his Windows machine. Is this code related or SFML related to the Mac?
I am compiling with the lastest version from the SVN.