Hello! Sorry for the delay.
mv_server.h:
#include "stdafx.h"
public class mv_server {
public:
mv_server::mv_server();
mv_server::~mv_server();
private:
sf::TcpListener listener;
void listen_loop();
sf::Thread* Thread;
std::vector<sf::TcpSocket*> clients;
sf::SocketSelector selector;
};
mv_server.cpp:
#include "stdafx.h"
mv_server::mv_server() {
//initialize connection tcp listener
std::cout << "Initializing tcp listener ..." << std::endl;
listener.Listen(55001);
//boost::thread worker_thread(boost::bind(&mv_server::listen_loop,this));
selector.Add(listener);
Thread = new sf::Thread(&mv_server::listen_loop, this);
Thread->Launch();
}
mv_server::~mv_server() {
}
void mv_server::listen_loop() {
while(1) {
if(selector.Wait()) {
if(selector.IsReady(listener)) {
sf::TcpSocket* client = new sf::TcpSocket();
if(listener.Accept(*client) == sf::Socket::Done) {
clients.push_back(client);
selector.Add(*client);
}
}
else {
for(std::vector<sf::TcpSocket*>::iterator it = clients.begin(); it != clients.end(); it++) {
sf::TcpSocket& client = **it;
if(selector.IsReady(client)) {
sf::Packet recv_packet;
sf::Packet send_packet;
if(client.Receive(recv_packet) == sf::Socket::Done) {
std::string got;
recv_packet >> got;
std::cout << "The client said: " << got << std::endl;
//packet.Clear();
send_packet << "Got yo message yo";
client.Send(send_packet);
}
}
}
}
}
}
}
mv_main.cpp:
int main(array<System::String ^> ^args)
{
sf::TcpSocket connection_dude;
bool connected_to_dude = false;
sf::RenderWindow menu_win(sf::VideoMode(400, 400, 32), "MV Menu");
//fork off a server
mv_server *listener = new mv_server();
//connect to it for a one-off test
//NOTE: I used my WAN ip here; insert your own or localhost or something
int status = connection_dude.Connect("xx.xxx.xxx.xxx", 55001);
connected_to_dude = true;
while(menu_win.IsOpened()) {
sf::Event e;
while(menu_win.PollEvent(e)) {
//close
if(e.Type == sf::Event::Closed) { menu_win.Close(); }
if(e.Type == sf::Event::KeyPressed) {
if(e.Key.Code == sf::Keyboard::A) {
//send a message to our server
if(connected_to_dude) {
sf::Packet p;
p << "We pressed A";
int status = connection_dude.Send(p);
p.Clear();
connection_dude.Receive(p);
std::string got;
p >> got;
std::cout << "Server says " << got << std::endl;
std::cout << "Sent status: " << status << std::endl;
p.Clear();
} else {
std::cout << "connect first!" << std::endl;
}
}
}
}
}
} //end main
stdafx.h:
#pragma once
#include <iostream>
#include <stdlib.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include "mv_server.h"
Some details, if they matter --
I'm using VS2008, the latest (well, as of a couple weeks ago) SFML build from github, and my OS is Windows 7 64-bit. I'm building this in Debug configuration.
Thanks!