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

Show Posts

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.


Messages - PhiLLe

Pages: 1 2 [3]
31
Network / Client managing
« on: February 20, 2010, 04:29:34 pm »
Thanks a lot again.
I tried all the things you said in a lot of different variants. I still couldn't get it to work.

I tried all combinations of:
- storing pointers in my vector
- storing a pointer to the Socket in my class (sf::SocketTCP * Socket;)
- inherit from sf::NonCopyable
- making "sf::SocketTCP * Socket;" private

I don't know what to do.

Is there any other easy way to build a Client Manager except vectors?

32
Network / Client managing
« on: February 20, 2010, 03:00:19 pm »
Thank you for the help so far. But there is still a problem.

main.cpp
Code: [Select]
#include <iostream>
#include <vector>
#include <SFML/Network.hpp>
#include "player.h"
using namespace std;

vector<Player> Clients;
sf::Mutex GlobalMutex;

void Accept(void * UserData)
{
    sf::SocketTCP Listener;

    if (!Listener.Listen(1234))
        cerr << "Clouldn't Bind to port: " << 1234 << endl;

    while (true)
    {
        sf::SocketTCP Client;
        if (Listener.Accept(Client) == sf::Socket::Done)
        {
            GlobalMutex.Lock();
            Clients.push_back(Player(Client));
            GlobalMutex.Unlock();

            cout << "New connection" << endl;
        }
    }
   
    Listener.Close();
}

int main()
{
    sf::Thread Taccept(Accept);
    Taccept.Launch();

    while (true)
    {
        sf::Packet packet;
        sf::Int8 GameChar = 'P';
        packet << GameChar;
 
        GlobalMutex.Lock();
        for (unsigned int i = 0; i < Clients.size(); i++)
        {
            sf::Socket::Status status = Clients.at(i).Socket.Send(packet);
            cout << "Socket " << i << ": ";
            if (status == sf::Socket::Error)
                cout << "Error" << endl;
            else if (status == sf::Socket::Disconnected)
                cout << "Disconnected" << endl;
            else if (status == sf::Socket::NotReady)
                cout << "NotReady" << endl;
        }
        GlobalMutex.Unlock();
        sf::Sleep(0.5);
    }
    Taccept.Terminate();
    return 0;
}


player.h
Code: [Select]
#ifndef __PLAYER__
#define __PLAYER__

#include <SFML/Network.hpp>

class Player {
    public:
       
        ~Player()
        {
            Socket.Close();
        }

        Player(sf::SocketTCP Client)
        {
            Socket = Client;
        }

        sf::SocketTCP Socket;
};

#endif


main.cpp (simple Client)
Code: [Select]
#include <iostream>
#include <SFML/Network.hpp>
using namespace std;

int main()
{
    sf::SocketTCP Server;
   
    if (Server.Connect(1234, "127.0.0.1") == sf::Socket::Done)
    {
        while(1)
        {
            sf::Packet packet;
            sf::Socket::Status status = Server.Receive(packet);
            if (status == sf::Socket::Done)
            {
                sf::Int8  GameChar;
               
                packet >> GameChar;
                cout << "Message: " << GameChar << endl;
            }
            else if (status == sf::Socket::Disconnected)
            {
                cout << "Disconnected" << endl;
            }
            else if (status == sf::Socket::Error)
            {
                cout << "Error" << endl;
            }
            else if (status == sf::Socket::NotReady)
            {
                cout << "NotReady" << endl;
            }
            sf::Sleep(0.5);
        }
    }
    else
    {
        cout << "Failed to connect" << endl;
    }

    cin.get();

    return 0;
}


Now, the client is disconnected whenever "Clients.push_back(Player(Client));" is called. I really don't know why even after 30 minutes of debugging.
What I know is that the client says it is disconnected. The Server says there is an error when trying to send the packet.
Does it matter if I use pointers or not? Should I use them?

I hope you can help me.

33
Network / Client managing
« on: February 19, 2010, 05:30:14 pm »
main.cpp
Code: [Select]
#include <iostream>
#include <SFML/Network.hpp>
#include "player.h"
using namespace std;
using namespace sf;

vector<Player> Clients;

void Accept(void * UserData)
{
    sf::SocketTCP Listener;
    Player NewClient;

    if (!Listener.Listen(1234))
        cerr << "Clouldn't Bind to port: " << PORT << endl;

    while (true)
    {
        Clients.push_back(NewClient);
        Listener.Accept(Clients.back().Socket);
   
        cout << "New connection. Address: "  << endl;
    }
   
    Listener.Close();
}

int main()
{
   
    sf::Thread Taccept(Accept);

    Taccept.Launch();

    while (true)
    {
        sf::Packet packet;
        sf::Int8 GameChar = 'P';
       
        packet << GameChar;

        for (unsigned int i = 0; i < Clients.size(); i++)
        {
            if (Clients.at(i).Socket.IsValid())
                Clients.at(i).Socket.Send(packet);
        }

        sf::Sleep(1);
    }

    Taccept.Terminate();

    cin.get();
    return 0;
}


player.h
Code: [Select]
#ifndef __PLAYER__
#define __PLAYER__

#include <SFML/Network.hpp>

class Player {
    public:
       
        ~Player()
        {
            Socket.Close();
        }

        sf::SocketTCP Socket;
        sf::IPAddress Address;

    private:

};

#endif


I know my Thread handling is bad. Just for testing purpose right now.
Once again: The first Socket that connects disconnects when "Listener.Accept(Clients.back().Socket);" is called the second time.

Btw: I am trying to make a pong(sfml sample) multiplayer mode :)

34
Network / Client managing
« on: February 19, 2010, 02:18:57 pm »
Quote from: "Laurent"
How do you know that the socket is disconnected? Do you store pointers or references to the elements of your array?


I know it is disconnected because the Client says it is when it tries to receive anything and the server confirms that when I try to send anything.

I do not store pointers. I create a Player object at the beginning and push it back each time I need a new object. After that I fill the element with the socket: "Listener.Accept(Clients.back().Socket); "

35
Network / Client managing
« on: February 18, 2010, 06:16:12 pm »
Hi!

I am writing a little Server application.
My Problem is that the Socket for my client disconnects when I am storing my Socket in a vector and I push back an other object.

Code:
Code: [Select]
vector<Player> Clients;

void Accept(void * UserData)
{
    sf::SocketTCP Listener;
    Player NewClient;

    if (!Listener.Listen(PORT))
        cerr << "Clouldn't Bind to port: " << PORT << endl;

    while (true)
    {
        Clients.push_back(NewClient);
        Listener.Accept(Clients.back().Socket);
   
        cout << "New connection. Address: "  << endl;
    }
   
    Listener.Close();
}


My player class:
Code: [Select]
class Player {
    public:
       
        ~Player()
        {
            Socket.Close();
        }

        sf::SocketTCP Socket;
        sf::IPAddress Address;

    private:

};


So the problem occurs when "Clients.push_back(NewClient);" is called the second time. I don't know why but the Socket "Clients.at(0).Socket" just disconnects.

I hope somebody can help me.

36
General / [soloved] Linking Problem - Debug Mode - Windows
« on: October 09, 2009, 04:31:23 pm »
Hello!

I am trying to compile a simple SFML application.
I am working with Visual Studio 2008 Express under Vista.

My steps:
1) Create a console Project
2) Write the code:
Code: [Select]
#include <SFML/Window.hpp>

int main()
{
    // Create the main window
    sf::Window App(sf::VideoMode(640, 480, 32), "SFML", sf::Style::Close);

    App.ShowMouseCursor(false);

    // Start main loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }

        App.Display();
    }

    return EXIT_SUCCESS;
}

3) Compile

I am linking to these libraries:
Debug:
sfml-system-s-d.lib sfml-window-s-d.lib
Release:
sfml-system-s.lib sfml-window-s.lib

Everything works in the Release mode. But there are some warnings when I compile in the Debug mode:
Quote
sfml-system-s-d.lib(Platform.obj) : warning LNK4099: PDB "vc90.pdb" wurde nicht mit "..\..\xxx\lib\vc2008\sfml-system-s-d.lib" oder an "xxx\win\Debug\vc90.pdb" gefunden; Objekt wird verknüpft, als ob keine Debuginformationen vorhanden wären.

Its kind of sad that it is in German :(

Well thats only a warning. I can run it without problems. But just one time. When I rebuild it the Program asks for a .dll:
MSVCP90D.dll

Very Strange...

Can anybody help me?
Greetings

Edit:
I solved it! I just had to build sfml by myseld and it worked fine.

Pages: 1 2 [3]
anything