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 - Grimlen

Pages: 1 [2]
16
Network / writebyte() implementation
« on: October 02, 2012, 11:08:24 pm »
I'm attempting to write a: writebyte() function in which the client
specified to write a byte of data to the server, and the server will read this data as a byte

Here's my writebyte():
void writebyte(sf::UdpSocket *socket, unsigned char b){
        if (socket->send(&b, sizeof(b), "127.0.0.1", 4567) != sf::Socket::Done){
                //Error
                cout << "message was not sent" << endl;
        }
}
 

And my readbyte():
unsigned char readbyte(UdpSocket *socket){
        unsigned char byte;
        std::size_t received;
        sf::IpAddress sender;
        unsigned short port;

        int rec = socket->receive(&byte, sizeof(byte), received, sender, port);
        if(rec != sf::Socket::Done){
                //Error
        }
        if(rec == sf::Socket::Disconnected)
                cout << "D/C" << endl;
        return byte;
}
 

I've so far gotten a few functions to work:
writeint()
writefloat() and
writeshort()

The server receives the data correctly.
However for writebyte() and readbyte(), the data is not read correctly.
For example, I've sent the following data using writebyte():
                                unsigned char c = 253;
                                writebyte(&socket, c);
 

Here's what the server writes:


Any idea what's wrong?

17
Graphics / SetSubRect()?
« on: October 01, 2012, 10:07:32 pm »
I noticed in SFML 2.0 the setSubRect() function appeared to be dropped from the sf::Sprite class...?
How exactly would I set a sub-rectangle of an sf::texture in order to animate it in SFML 2.0?

You could do this in 1.6 but it appears to have disappeared.

Example:
sf::Texture tex;
sf::Sprite spr;
..
spr.setTexture(tex);
spr.setSubRect() ... //non-existing in 2.0
 

18
System / Getting my thread to stop when I close my window
« on: September 24, 2012, 10:08:26 pm »
I'm attempting right now to make it so when I close my window, the thread stops by passing in a boolean (running) variable to my thread. However...when I close my window the thread does not appear to stop until I close the console window. The main window (RenderWindow) closes as expected, but the console window does not....Not sure what to do here.
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

#include <SFML/Graphics.hpp>

void netThread(void *userData){
        bool running = *(bool*) userData;
        while(running)
            cout << running << endl;
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        bool running = true;
        sf::Thread thread(&netThread, &running);
        thread.launch();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        if (event.type == sf::Event::Closed){
                                running = false;
                window.close();
                        }
        }

        window.clear();
        window.display();
    }
        running = false;
    return 0;
}
 

19
Network / Re: How do I set up a UDP connection for my server?
« on: September 23, 2012, 07:36:28 pm »
Well what if two people from the same IP connect?

Also I'm basically asking from my code, is it set up right?
Like what would you do different?

20
Network / How do I set up a UDP connection for my server?
« on: September 23, 2012, 06:37:03 am »
Here's my current server code:
/************************************************************
SERVER
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

#include <SFML/Graphics.hpp>

bool running = true;

void netThread(){
        char buffer[128];
        std::size_t received;
        sf::IpAddress sender;
        unsigned short port;

        // Create the UDP socket
        sf::UdpSocket socket;
        socket.setBlocking(false);
        // Bind it (listen) to the port 4567
        if (!socket.bind(4567)){
                // Error...
                cout << "Unable to bind UDP socket: 4567" << endl;
        }

        while(running){
                int rec = socket.receive(buffer, sizeof(buffer), received, sender, port);
                if(rec != sf::Socket::Done){
                        //Error
                }
                if(rec == sf::Socket::Disconnected)
                        cout << "D/C" << endl;
                // Show the address / port of the sender
                std::cout << sender << ":" << port << std::endl;
                // Show the message
                std::cout << buffer << std::endl; // "Hi guys !"
        }
}

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::Thread thread(&netThread);
        thread.launch();

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        if (event.type == sf::Event::Closed){
                                running = false;
                window.close();
                        }
        }

        window.clear();
        window.display();
    }
        running = false;
    return 0;
}
 

Can anyone make any suggestions as to what I should do for a UDP connection?
I'm trying to have multiple clients for my server, so...would I use just one socket? Or multiple sockets?
I'm assuming I just use one as there's no actual connection and it's just packets coming into
my server, however how would I differentiate different players on my game? By IP?
Or would I have them, for each message they send, send a special ID that uniquely identifies
who they are on the server...?

Not sure how to set this up.

21
Network / Re: Server can only receive one message at a time?
« on: September 20, 2012, 06:56:13 pm »
There doesn't appear to be any tutorials on version 2.0 for networking (unless I saw wrong).
I've got a roster of people all ready setup for 1.6 and we'd rather not switch as we've been working
hard with 1.6. Anyways....
To be honest I'd rather want to know what's wrong with my code now.
I've gone over the tutorial on the TCP section and I'm absolutely dumbfounded why I can only
get just one message per connection.....

Could you possibly test it to determine what's wrong? I've been stuck on this for a while.

22
Network / Re: Server can only receive one message at a time?
« on: September 20, 2012, 06:39:48 am »
Well I've redone my entire server and client.
Unfortunately I'm still getting the same result as before.
I can only get one message at a time per client/connection....

CLIENT:
/************************************************************
CLIENT
************************************************************/

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
        //Server we are connecting to (via TCP)
        bool closedConnection = false; //if we closed the socket yet or not
        sf::SocketTCP Server;
        Server.SetBlocking(false); //It honestly makes no difference whether it's true/false, same problem still
        char Message1[] = "Hello!";
        char Message2[] = "Again!";
        cout << "test" << endl;
       
        if (Server.Connect(4567, "127.0.0.1") != sf::Socket::Done)
        {
                // Error...
                cout << "Unable to connect to server" << endl;
        }
        else{
                cout << "Connected to server" << endl;
        }
        if (Server.Send(Message1, sizeof(Message1)) != sf::Socket::Done)
        {
                // Error...
        }
        else{
                cout << "Sent message to server" << endl;
        }
        if (Server.Send(Message2, sizeof(Message2)) != sf::Socket::Done)
        {
                // Error...
        }
        else{
                cout << "Sent message to server" << endl;
        }
       
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(480, 320, 32), "Window");
        const sf::Input& Input = App.GetInput();
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
                        if (Event.Type == sf::Event::Closed){
                                //Player decided to close the program
                                //Check if we've closed the socket yet
                                //If we haven't, close it
                                if(!closedConnection){
                                        Server.Close();
                                        closedConnection = true;
                                }
                App.Close();
                        }
        }

        // Clear the screen (fill it with black color)
        App.Clear(sf::Color(255, 255, 255));

        // Display window contents on screen
        App.Display();
    }
        //We come to the end of the program
        //Check if we've closed the socket yet
        //If we haven't, close it
        if(!closedConnection){
                Server.Close();
                closedConnection = true;
        }
    return EXIT_SUCCESS;
}
 

SERVER:
/************************************************************
SERVER
************************************************************/

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

//Entry
int main(){
        sf::SocketTCP Client;
        sf::IPAddress ClientAddress;
        sf::SocketTCP Listener;
        Client.SetBlocking(false); //It honestly makes no difference whether it's true/false, same problem still
        Listener.SetBlocking(false); //It honestly makes no difference whether it's true/false, same problem still
        //Buffer to receive messages
        char Buffer[128];
        //How many messages we've received
        std::size_t Received;
        bool running = true;
        if (!Listener.Listen(4567))
        {
                // Error...
                cout << "Unable to listen on TCP port: 4567" << endl;
        }
        else{
                cout << "Listening on TCP port: 4567" << endl;
        }
        while(running){
                //Waits until a incoming connection arrives
                if (Listener.Accept(Client, &ClientAddress) != sf::Socket::Done)
                {
                        // Error...
                        //cout << "Unable to accept any incoming connections" << endl;
                }
                else{
                        cout << "Accepted a Client!" << endl;
                }
                if (Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
                {
                        // Error...
                        //cout << "Unable to receive any messages from client." << endl;
                }
                else{
                        cout << "Message: " << Buffer << endl;
                }
        }
}
 

23
Network / Re: Server can only receive one message at a time?
« on: September 18, 2012, 11:16:15 pm »
#include "Server.h"
#include <iostream>
using namespace std;
Server::Server(){
        started = false;
        Listener.SetBlocking(false);
}

void Server::start(){
        if(!started){
                //Check TCP Socket
                cout << "Attempting to listen on TCP port: 4567...." << endl;
                if (!Listener.Listen(4567))
                {
                        // Error...
                        cout << "Could not listen on TCP port: 4567." << endl;
                        return;
                }
                cout << "Listening on port 4567" << endl;
                started = true;
                cout << "Server started." << endl;

                selector.Add(Listener);
        }
        else{
                cout << "Server is all ready running." << endl;
        }
}

void Server::stop(){
        cout << "Stopping server..." << endl;
        started = false;
        unsigned int NbSockets = selector.Wait();
        for(unsigned int i = 0; i < NbSockets; ++i){
                selector.GetSocketReady(i).Close();
        }
}
 

24
Network / Re: Server can only receive one message at a time?
« on: September 18, 2012, 08:25:38 am »
Sorry, didn't know I could do that.
Do you know what may be the problem?

25
Network / Re: Server can only receive one message at a time?
« on: September 18, 2012, 05:29:45 am »
It's asking for commands.
Here's my entire main.cpp for my server:
/************************************************************
SERVER
************************************************************/

#include <iostream>
#include <string>
#include "Server.h"
#include <SFML/System.hpp>
using namespace std;

int command(Server *s, std::string line);
void ThreadFunction(void* UserData);

bool running;

void ThreadFunction(void* UserData)
{
        Server *server = (Server*)UserData;
        char Buffer[128];
        std::size_t Received;
        while(true){
                unsigned int NbSockets = server->selector.Wait();
                for(unsigned int i = 0; i < NbSockets; ++i){
                        sf::SocketTCP Socket = server->selector.GetSocketReady(i);
                        //Look for new connections
                        if(Socket == server->Listener){
                                sf::IPAddress Address;
                                sf::SocketTCP Client;
                                server->Listener.Accept(Client, &Address);
                                cout << "Got a client" << endl;
                                server->selector.Add(Client);
                        }
                        //Read data from clients
                        else{
                                sf::Packet Packet;
                                if(Socket.Receive(Packet) == sf::Socket::Done){
                                        //Get the message
                                        std::string Message;
                                        //Put whatever is in the Packet to the string
                                        Packet >> Message;
                                        cout << "Message: " << Message << endl;
                                }
                                else{
                                        server->selector.Remove(Socket);
                                }
                        }
                }
        }

}

//Entry
int main(){
        Server server;
        sf::Thread thread(&ThreadFunction, &server);
        running = true;
        thread.Launch();
        string line;
        cout << "=========================="    << endl;
        cout << "Welcome to the NetServer."             << endl;
        cout << "=========================="    << endl;
        cout << endl;
        cout << "===================================================="          << endl;
        cout << "Enter a command. For a list of commands type \"help\"."        << endl;
        cout << "===================================================="          << endl;
        while (running){
                int ret;
                if(std::getline(cin, line)){
                        if(ret = command(&server, line) != 0)
                                return ret;
                }
        }
}

//Loop
int command(Server *server, std::string line){
        cout << endl;
        //Process line
        if(line == "help"){
                cout    << "<LIST OF COMMANDS:>" << endl;
                cout    << "help        - Displays help"        << endl
                                << "start       - Starts the server"    << endl
                                << "stop        - Stops the server"     << endl;
        }
        else if(line == "start"){
                server->start();
        }
        else if(line == "stop"){
                server->stop();
        }
        else{
                cout << endl;
                cout << "=========================" << endl;
                cout << "Invalid. Enter a command." << endl;
                cout << "=========================" << endl;
                return 0;
        }
        cout << endl;
        cout << "================" << endl;
        cout << "Enter a command." << endl;
        cout << "================" << endl;
        return 0;
}
 

26
Network / Re: Server can only receive one message at a time?
« on: September 17, 2012, 08:26:41 pm »
I've also tried using a selector.
Only this time, I can't receive any messages, besides the one when they connect to my server:
void ThreadFunction(void* UserData)
{
        Server *server = (Server*)UserData;
        char Buffer[128];
        std::size_t Received;
        while(true){
                unsigned int NbSockets = server->Selector.Wait();
                for(unsigned int i = 0; i < NbSockets; ++i){
                        sf::SocketTCP Socket = server->Selector.GetSocketReady(i);
                        //Look for new connections
                        if(Socket == server->Listener){
                                sf::IPAddress Address;
                                sf::SocketTCP Client;
                                server->Listener.Accept(Client, &Address);
                                cout << "Got a client" << endl;
                                server->Selector.Add(Client);
                        }
                        //Read data from clients
                        else{
                                sf::Packet Packet;
                                if(Socket.Receive(Packet) == sf::Socket::Done){
                                        //Get the message
                                        std::string Message;
                                        //Put whatever is in the Packet to the string
                                        Packet >> Message;
                                        cout << "Message: " << Message << endl;
                                }
                                else{
                                        server->Selector.Remove(Socket);
                                }
                        }
                }
        }

}
 

What I'm using to send a message:
                                char Buffer[] = "Hi guys !";
                                if (Client.Send(Buffer, sizeof(Buffer)) != sf::Socket::Done)
                                {
                                        // Error...
                                        cout << "error sending message" << endl;
                                }
 

27
Network / Re: Server can only receive one message at a time?
« on: September 17, 2012, 06:32:32 pm »
Well I tried non-blocking sockets:
        Listener.SetBlocking(false);
        Client.SetBlocking(false);
 

Can still send one message per connection.

I've also tried using different threads:
void ThreadNet(void* UserData){
        Server *server = (Server*)UserData;
        char Buffer[128];
        std::size_t Received;
        while(true){
                if (server->Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
                {
                        // Error...
                        continue;
                }
                else{
                        cout << "Message: " << Buffer << endl;
                }
        }
}

void ThreadFunction(void* UserData)
{
        Server *server = (Server*)UserData;

        while(true){
                sf::IPAddress ClientAddress;
                if (server->Listener.Accept(server->Client, &ClientAddress) != sf::Socket::Done)
                {
                        // Error...
                        //cout << "Error listening..." << endl;
                }
                else{
                        cout << "Got a connection!" << endl;
                }
        }
}
 

Still just one message per connection...

28
Network / Re: Server can only receive one message at a time?
« on: September 17, 2012, 05:31:54 pm »
Not really sure how to fix the problem for the server.
Don't we need to continuously listen for new connections using a loop?

I'm  a bit clueless on where in my program these codes need to be.

29
Network / Server can only receive one message at a time?
« on: September 17, 2012, 09:16:17 am »
I haven't tested client -> server -> client yet, just client -> server for now.
Unfortunately, I'm only able to send one message per connection.
Client
int main()
{
        sf::SocketTCP Client;
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(480, 320, 32), "Window");
        const sf::Input& Input = App.GetInput();
    cout << "Type LEFT to connect, RIGHT to send a message, UP to disconnect" << endl;
    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
                        if(Input.IsKeyDown(sf::Key::Left)){
                                if (Client.Connect(4567, "127.0.0.1") != sf::Socket::Done)
                                {
                                        // Error...
                                        cout << "error" << endl;
                                }
                                else cout << "connected" << endl;
                        }
                        else if(Input.IsKeyDown(sf::Key::Right)){
                                cout << "right pressed" << endl;
                                char Buffer[] = "Hi guys !";
                                if (Client.Send(Buffer, sizeof(Buffer)) != sf::Socket::Done)
                                {
                                        // Error...
                                        cout << "error sending message" << endl;
                                }
                        }
                        else if(Input.IsKeyDown(sf::Key::Up)){
                                Client.Close();
                                cout << "disconnected" << endl;
                        }
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear the screen (fill it with black color)
        App.Clear(sf::Color(255, 255, 255));

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}
 

Left button connects, right button sends a message: "Hi guys !", up button disconnects/closes the socket.
Now on my server...
Server
void ThreadFunction(void* UserData)
{
        Server *server = (Server*)UserData;
        char Buffer[128];
        std::size_t Received;

        while(true){
                sf::IPAddress ClientAddress;
                if (server->Listener.Accept(server->Client, &ClientAddress) != sf::Socket::Done)
                {
                        // Error...
                        //cout << "Error listening..." << endl;
                }
                else{
                        cout << "Got a connection!" << endl;
                }

                if (server->Client.Receive(Buffer, sizeof(Buffer), Received) != sf::Socket::Done)
                {
                        // Error...
                        continue;
                }
                else{
                        cout << "Message: " << Buffer << endl;
                }
        }
}
 

I can connect, and send a message, but just one message.
If I disconnect and connect again, I can send a message, but only one.

Not sure what's going on. I should be able to send multiple ones.

Pages: 1 [2]