user3882772
user3882772

Reputation: 149

SFML 2.1 Networking

I was trying to follow this tutorial, and I got this far.

#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main(){
    sf::IpAddress ip=sf::IpAddress::getLocalAddress();
    sf::TcpSocket socket;
    char connectiontype,mode;
    char buffer[2000];
    size_t received;
    cout<<"s for server, c for client"<<endl;
    cin>>connectiontype;
    string text="connected to ";
    if(connectiontype=='s'){
        sf::TcpListener listener;
        listener.listen(3000);
        listener.accept(socket);
        text+="server";
        mode='s';
    }
    else if(connectiontype=='c'){
        socket.connect(ip,3000);
        text+="client";
        mode='r';
    }
    socket.send(text.c_str(),text.length()+1);
    socket.receive(buffer,sizeof(buffer),received);
    cout<<buffer<<endl;

    bool done=false;

    while(!done){
        if(mode=='s'){
            getline(cin,text);
            socket.send(text.c_str(),text.length()+1);
            mode='r';
        }
        else if(mode=='r'){
            socket.receive(buffer,sizeof(buffer),received);
            if(received>0){
                cout<<"received: "<<buffer<<endl;
                mode='s';
            }
        }
    }
    return 0;
}

I compiled it, and got these errors: enter image description here

I also tried adding sfml-network-2.dll and sfml-network-d-2.dll into my project folder, but it didn't work.

I'm also pretty sure that I set everything up correctly.
This is my setup: enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

I covered my name up, if you don't mind. Thanks!

Update:

I have reinstalled SFML, and I updated my code and my errors.

Upvotes: 2

Views: 1439

Answers (3)

user3882772
user3882772

Reputation: 149

I solved my problem!
I changed each library to just the name, not where it is.
Example: Desktop/SFML-2.1/lib/libsfml-network-s-d.a->sfml-network

Upvotes: 1

Hiura
Hiura

Reputation: 3530

You need to link to sfml-system too.

As a side note, you can find the official example about sockets here.

Upvotes: 0

0xpentix
0xpentix

Reputation: 742

If you are pretty sure you have set up everything correctly, are you sure you added the SFML path to the Compiler include paths AND the SFML DLL to the Linker paths?

As you use Code::Blocks, have you setup your project "online game" exactly as described here?

http://www.sfml-dev.org/tutorials/2.1/start-cb.php

Update:

Have a look at this picture: http://www.sfml-dev.org/tutorials/2.1/images/start-cb-link-libs.png

Are you sure, you've got sfml-network in the library list there? This might be the problem.

Adding the dlls to the project folder won't help, as it is a compiler/linker error and not a runtime library error.

Upvotes: 0

Related Questions