目前我正在尝试学习SFML网络,但我遇到了客户端问题(我认为)。
#include <iostream>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
int main()
{
sf::TcpSocket sock;
sf::Packet backpack;
sf::Thread Thread;
std::cout <<"Attempting to connect to server" << std::endl;
sf::Socket::Status status = sock.connect("127.0.0.1", 25568);
if (status != sf::Socket::Done){
std::cout << "Could Not Connect to server" << std::endl;
} else {
std::cout << "Connected to server" << std::endl;
}
backpack << 9;
std::cout << "Sending data" << std::endl;
sock.send(backpack);
std::cout << "Sent the data" << std::endl;
std::cout << "Client task completed" << std::endl;
return 0;
}
这是服务器代码
#include <iostream>
#include <SFML/Network.hpp>
int main()
{
std::cout << "Server Has started" << std::endl;
sf::TcpSocket peer;
sf::TcpListener ear;
sf::Packet backpack;
if (ear.listen(25568) != sf::Socket::Done){
std::cout << "Listerner failed to be setup, another program may be using that port." << std::endl;
} else {
std::cout << "Server is ready" << std::endl;
}
if (ear.accept(peer) != sf::Socket::Done){
std::cout << "Client refused connection" << std::endl;
} else {
std::cout << "Client connection Accepted" << std::endl;
}
peer.receive(backpack);
std::cout << "Data recieved" << std::endl;
int num;
backpack >> num;
std::cout << num << std::endl;
std::cout << "hello world" << std::endl;
return 0;
}
运行服务器和客户端时(按任意顺序)客户端将在消息后输出任何内容&#34;尝试连接到服务器&#34;出现,并且服务器在消息&#34;服务器准备就绪后输出任何内容&#34;请帮助=)我一直坚持这个问题。