我试图关注this教程,我做到了这一点。
#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;
}
我编译了它,并得到了这些错误:
我还尝试将sfml-network-2.dll
和sfml-network-d-2.dll
添加到我的项目文件夹中,但它不起作用。
我也很确定我正确设置了所有内容 这是我的设置:
如果你不介意的话,我把我的名字盖了起来。谢谢!更新
我重新安装了SFML,并更新了我的代码和错误。
答案 0 :(得分:1)
我解决了我的问题!
我将每个库改为名称,而不是它的位置
示例:Desktop/SFML-2.1/lib/libsfml-network-s-d.a
- &gt; sfml-network
答案 1 :(得分:0)
如果您确定已正确设置了所有内容,是否确定已将SFML路径添加到编译器包含路径和SFML DLL到链接器路径?
当您使用Code :: Blocks时,您是否完全按照此处所述设置项目“在线游戏”?
http://www.sfml-dev.org/tutorials/2.1/start-cb.php
<强>更新强>
看看这张照片: http://www.sfml-dev.org/tutorials/2.1/images/start-cb-link-libs.png
您确定,那里的图书馆列表中有sfml-network
吗?这可能是问题所在。
将dll添加到项目文件夹无济于事,因为它是编译器/链接器错误而不是运行时库错误。
答案 2 :(得分:0)
您还需要链接到sfml-system
。
作为旁注,您可以找到有关套接字here的官方示例。