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

Author Topic: Can someone help me with this simple client/server test?  (Read 3500 times)

0 Members and 2 Guests are viewing this topic.

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Can someone help me with this simple client/server test?
« on: May 10, 2011, 10:23:51 pm »
alright, I've been working on this for about 5 hours today alone, and I still can't get anything working. Can someone tell me what I'm doing wrong? I'm getting no output from the server at all, nor the client.

Client:
Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <sstream>
using namespace std;

int main()
{
    // Create the main rendering window
    sf::IPAddress addy("76.121.76.188");


    sf::SocketUDP Socket;

    // Create bytes to send
    char Buffer[] = "Player joining.";

    // Send data to "192.168.0.2" on port 4444
    if (Socket.Send(Buffer, sizeof(Buffer), addy, 4444) != sf::Socket::Done)
    {
        cout << "Sending failed." << endl;
    }

    while (true)
    {
        // Process events
        if (addy.IsValid()){
            //recieve enemy posititon
            sf::SocketUDP receiverSocket;

            // Bind it (listen) to the port 4444
            if (!receiverSocket.Bind(4444))
            {
                cout << "binding failed" << endl;
            }

            //get data
            char buffer[128];
            std::size_t Received;
            sf::IPAddress Sender;
            unsigned short Port;

            if (receiverSocket.Receive(buffer, sizeof(buffer), Received, Sender, Port) != sf::Socket::Done)
            {
                cout << "error receiveing packet" << endl;
            }

            else{
                cout<<buffer<<endl;
            }

            //send player position
            sf::SocketUDP Socket;
            ostringstream sin;
            sin << "Goodbye.";
            sin.str().c_str();

            // Send data to "192.168.0.2" on port 4444
            if (Socket.Send(sin.str().c_str(), sizeof(sin.str().c_str()), addy, 4444) != sf::Socket::Done)
            {
                cout << "Sending failed" << endl;
            }

        }
    }

    return EXIT_SUCCESS;
}


Server:
Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>
#include <sstream>
#include <vector>

#include "player.h"
#include "playerthread.h"

using namespace std;

int main()
{
    bool done = false;
    int numConnected = 0;
    sf::IPAddress playerIP[2];
    Playerthread *pthread[2];
    // Start game loop
    while (!done)
{
        sf::SocketUDP receiverSocket;

        // Bind it (listen) to the port 4444
        if (!receiverSocket.Bind(4444))
        {
            cout << "binding failed" << endl;
        }

        //get data
        char buffer[128];
        std::size_t Received;
        sf::IPAddress Sender;
        unsigned short Port;

        if (receiverSocket.Receive(buffer, sizeof(buffer), Received, Sender, Port) != sf::Socket::Done)
        {
            cout << "error receiveing packet" << endl;
        }

        else{
            playerIP[numConnected] = Sender;
            pthread[numConnected] = new Playerthread(playerIP[numConnected]);
            pthread[numConnected]->startThread();
            numConnected++;
        }

        // Show the address of the sender
        std::cout << Sender << std::endl;

        // Show the message
        std::cout << buffer << std::endl; // "Hi guys !"

        //close socket
        receiverSocket.Close();
    }

    return EXIT_SUCCESS;
}



Server Thread:
Code: [Select]

#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

class Playerthread : private sf::Thread{
public:
int get_RetLevel();

sf::IPAddress playerIP;

Playerthread(sf::IPAddress &nplayerIP){
        playerIP = nplayerIP;
}
void startThread(){
        Launch();
    }
private:
virtual void Run(){
while(true){
if (playerIP.IsValid()){
                    //send enemy position

                        sf::SocketUDP Socket;
                        ostringstream sin;

                        sin << "enemy local";
                        sin.str().c_str();

                        // Send data to "192.168.0.2" on port 4444
                        if (Socket.Send(sin.str().c_str(), sizeof(sin.str().c_str()), playerIP, 4444) != sf::Socket::Done)
                        {
                            cout << "Sending failed" << endl;
                        }
                    }

                    //recieve player posititon
                    sf::SocketUDP receiverSocket;

                    // Bind it (listen) to the port 4444
                    if (!receiverSocket.Bind(4444))
                    {
                        cout << "binding failed" << endl;
                    }

                    //get data
                    char buffer[128];
                    std::size_t Received;
                    sf::IPAddress Sender;
                    unsigned short Port;

                    if (receiverSocket.Receive(buffer, sizeof(buffer), Received, Sender, Port) != sf::Socket::Done)
                    {
                        cout << "error receiveing packet" << endl;
                    }

                    else{
                        if(Sender.ToString().compare(playerIP.ToString())){
                            cout << "thread" << buffer << endl;
                        }
                    }

}
}
};


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Can someone help me with this simple client/server test?
« Reply #1 on: May 10, 2011, 11:34:15 pm »
Hi

First, start simple when nothing works. I see players, enemies, threads, ... first write a simple application with two sockets communicating with each other, and when it works build on top of it. You can use the Sockets sample from the SFML SDK as well.

Then, test on your local network (or even on the same machine), going throuh the public internet will probably fail if your router is not properly configured for port forwarding.
Laurent Gomila - SFML developer

CyanPrime

  • Newbie
  • *
  • Posts: 18
    • View Profile
Can someone help me with this simple client/server test?
« Reply #2 on: May 11, 2011, 02:26:25 am »
okay, this one might be a bit smaller and more simple. the problem is it only connects to one client instead of two. Can anyone help me figure out why?

Server:
Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>

void sendInfo(void *UserData)
{
    sf::IPAddress* ip = static_cast<sf::IPAddress*>(UserData);
    // Print something...
    while(true){
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Create bytes to send
        char Buffer[] = "sending info.";

        // Send data to "192.168.0.2" on port 4567
        if (Socket.Send(Buffer, sizeof(Buffer), *ip, 4444) != sf::Socket::Done)
        {
            // Error...
        }
    }
}

void receiveInfo(void *userData)
{
    // Print something...
    while(true){
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Bind it (listen) to the port 4567
        if (!Socket.Bind(4444))
        {
            // Error...
        }

        char Buffer[128];
        std::size_t Received;
        sf::IPAddress Sender;
        unsigned short Port;
        if (Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port) != sf::Socket::Done)
        {
            // Error...
        }

        // Show the address / port of the sender
        std::cout << Buffer << std::endl;

        Socket.Close();

    }
}

int main()
{
    sf::IPAddress client[2];
    int connected = 0;
    while(connected < 2){
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Bind it (listen) to the port 4567
        if (!Socket.Bind(4444))
        {
            // Error...
        }

        char Buffer[128];
        std::size_t Received;
        sf::IPAddress Sender;
        unsigned short Port;
        if (Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port) != sf::Socket::Done)
        {
            // Error...
        }

        // Show the address / port of the sender
        client[connected] = Sender;

        Socket.Close();

        sf::Thread* send = new sf::Thread(&sendInfo, &client[connected]);
        sf::Thread* receive = new sf::Thread(&receiveInfo, &client[connected]);
        // Start it !
        send->Launch();
        receive->Launch();
        connected++;
    }

    while(true){

    }

    return EXIT_SUCCESS;
}



client:

Code: [Select]

#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <iostream>

void sendInfo(void *UserData)
{
    // Print something...
    while(true){
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Create bytes to send
        char Buffer[] = "client sending info.";

        // Send data to "192.168.0.2" on port 4567
        if (Socket.Send(Buffer, sizeof(Buffer),  "127.0.0.1", 4444) != sf::Socket::Done)
        {
            // Error...
        }
    }
}

void receiveInfo(void *userData)
{
    // Print something...
    while(true){
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Bind it (listen) to the port 4567
        if (!Socket.Bind(4444))
        {
            // Error...
        }

        char Buffer[128];
        std::size_t Received;
        sf::IPAddress Sender;
        unsigned short Port;
        if (Socket.Receive(Buffer, sizeof(Buffer), Received, Sender, Port) != sf::Socket::Done)
        {
            // Error...
        }

        // Show the address / port of the sender
        std::cout << Buffer << std::endl;

        Socket.Close();

    }
}

int main()
{
    // Create the UDP socket
    sf::SocketUDP Socket;

    // Create bytes to send
    char Buffer[] = "Client Joined.";

    // Send data to "192.168.0.2" on port 4567
    if (Socket.Send(Buffer, sizeof(Buffer), "127.0.0.1", 4444) != sf::Socket::Done)
    {
        // Error...
    }

    sf::Thread* send = new sf::Thread(&sendInfo);
    sf::Thread* receive = new sf::Thread(&receiveInfo);
    // Start it !
    send->Launch();
    receive->Launch();


    while(true){

    }

    return EXIT_SUCCESS;
}



GatorQue

  • Newbie
  • *
  • Posts: 36
    • View Profile
Problems with server code
« Reply #3 on: May 11, 2011, 04:36:13 am »
I believe your problem is in the way your handing off your clients.  Usually, the server listens on port 1234 and your clients get spun off onto their own receiver ports.  In your code below you have the following:
Code: [Select]

int main()
...
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Bind it (listen) to the port 4567
        if (!Socket.Bind(4444))
        {
            // Error...
        }

...

        Socket.Close();
...

Notice how your Opening your socket on port 4444 and closing it after your first client connects.  Then you spawn a thread that does the following:
Code: [Select]

void receiveInfo(void *userData)
{
    // Print something...
    while(true){
        // Create the UDP socket
        sf::SocketUDP Socket;

        // Bind it (listen) to the port 4567
        if (!Socket.Bind(4444))
        {
            // Error...
        }
...

Notice how it binds to the SAME port as the server, this means that all future clients will be writing to the "receiver" of the first client.  It also means that in your main loop when it gets back to the "bind" step this will fail to bind to the same port.  You can only bind one UDP receiver to the same port on the same network adapter.

In addition to the SFML tutorials (which are very good) you might find a few of these articles helpful in describing how to work with sockets in general.
http://gafferongames.com/networking-for-game-programmers/sending-and-receiving-packets/

I hope to have a few examples using networking by the end of the summer as well.