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

Author Topic: Anyone here have a project made using SFML for networking?  (Read 15399 times)

0 Members and 1 Guest are viewing this topic.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: Anyone here have a project made using SFML for networking?
« Reply #15 on: April 08, 2012, 08:46:19 am »
Sigh..... this has to be a bug....

I am getting not ready when I try to receive anything when I test the returned status on non-blocking sockets....

This is SFML2.0 and MSVC++ 2010...

switch(socket.receive(packetReceive))
      {
         case sf::Socket::Done:
            std::cout <<  "done\n";
            break;
         case sf::Socket::Disconnected:
            std::cout << "disconnected\n";
            break;
         case sf::Socket::Error:
            std::cout << "error\n";
            break;
         case sf::Socket::NotReady:
            std::cout << "not ready\n";
            break;
      }

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: Anyone here have a project made using SFML for networking?
« Reply #16 on: April 08, 2012, 08:54:34 pm »
Can someone please post his code as a sticky for others to see? This code works for a very very simple client/server chat back and forth example I came up with, to show others after beating my head against the wall, I figured I would show others to help them with their first examples....

Code: [Select]
#include "stdafx.h"

const unsigned short PORT = 5000;
const std::string IPADDRESS("192.168.0.100");//change to suit your needs

std::string msgSend;

sf::TcpSocket socket;
sf::Mutex globalMutex;
bool quit = false;

void DoStuff(void)
{
static std::string oldMsg;
while(!quit)
{
sf::Packet packetSend;
globalMutex.lock();
packetSend << msgSend;
globalMutex.unlock();

socket.send(packetSend);

std::string msg;
sf::Packet packetReceive;

socket.receive(packetReceive);
if(packetReceive >> msg)
{
if(oldMsg != msg)
if(!msg.empty())
{
std::cout << msg << std::endl;
oldMsg = msg;
}
}
}
}
void Server(void)
{
sf::TcpListener listener;
listener.listen(PORT);
listener.accept(socket);
std::cout << "New client connected: " << socket.getRemoteAddress() << std::endl;
}
bool Client(void)
{
if(socket.connect(IPADDRESS, PORT) == sf::Socket::Done)
{
std::cout << "Connected\n";
return true;
}
return false;
}
void GetInput(void)
{
std::string s;
std::cout << "\nEnter \"exit\" to quit or message to send: ";
std::cin >> s;
if(s == "exit")
quit = true;
globalMutex.lock();
msgSend = s;
globalMutex.unlock();
}
int main(int argc, char* argv[])
{
sf::Thread* thread = 0;

char who;
    std::cout << "Do you want to be a server (s) or a client (c) ? ";
    std::cin  >> who;

    if(who == 's')
Server();
else
Client();

thread = new sf::Thread(&DoStuff);
thread->launch();

while(!quit)
{
GetInput();
}
if(thread)
{
thread->wait();
delete thread;
}
return 0;
}

GatorQue

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Anyone here have a project made using SFML for networking?
« Reply #17 on: July 04, 2012, 06:35:39 am »
Mars,

I have been reviewing your code and hope that this reply will help you out. In my reading and experience working with networking code there are basically two techniques to implement the code.
- Client/Server
- Peer-To-Peer

In the first model you create a server who has one thread dedicated to "accepting" new client connections. These connections all come in on the "listening" port, but typically, the client provides some random port (ephemeral). Once accepted, the server will typically spawn a "client thread" to handle communicating to the client.  The client code is usually pretty simple, a single "connect" call to the server IP address and port and then uses that "socket" to send and receive data from the server.
Another way to implement a client/server model is to have the server poll the incoming "socket" for client activity. If activity is found, then it processes the activity quickly and then returns to polling again. To do this, you must enable "non-blocking" sockets on the listening socket you create for the server.  The client code is the same as in the previous implementation, but you can also cause the client to poll for incoming data from the server in a non-blocking way by enabling "non-blocking" sockets for the client too.

In the Peer-To-Peer model you create multiple socket connections to all of your "peer" friends, there is no centralized server from which everyone tries to communicate.  Another way to implement the Peer-To-Peer model is to use "broadcast" packets which get blasted out to anyone on the _same_ network (in other words, this is what many LAN games use, but doesn't work over the internet). This model is less centralized.

Now, getting back to your code.  In your Server() method you "listen" and "accept" a connection, and your socket will block until you "accept" your first client.  At which point it will exit and run the "DoStuff" in a separate thread.  The problem is that you stop "accepting" any new clients. Therefore you can only have 1 client and 1 server at a time. It would be better if you made the "server" method into a thread and while loop so it could "accept" as many clients as you wanted.  Each client that was "accepted" would spawn another thread to process sending data to that client.

Thread 1: Run while loop as server to listen and accept client connections
Thread 2: Run while loop accepting local input from the keyboard and posting this input to the client threads below.
Thread 3..n: Run while loop receiving data from the socket provided by the Accept call. This is the thread that will be spawned by Thread 1 above. Monitor a shared "broadcast" string to be sent to the client when Thread 2 posts to this shared "broadcast" string.  Print any "string" messages received from the client socket to which this thread is responsible for.

I hope these explanations help, please write back soon with additional questions.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: Anyone here have a project made using SFML for networking?
« Reply #18 on: July 04, 2012, 09:12:52 am »
Thanks for the info! I will take a look at it more later on.

 

anything