从SFML套接字接收数据时的随机输出

时间:2016-12-06 02:26:47

标签: c++ sockets networking sfml

运行此代码时,我在输入C' C时会收到完全随机的输出。进入' S'

时没有任何事情发生

非常感谢任何帮助!

#include <iostream>
#include <string>
#include <SFML/Network.hpp>

int main()
{
sf::IpAddress ip = sf::IpAddress::getLocalAddress();
sf::TcpSocket socket;
char connectionType, buffer[2000];
std::size_t received;

std::cout << "C for client, S for server" << std::endl;
std::cin >> connectionType;

std::string packetText = "test";

if (connectionType == 'S')
{
    sf::TcpListener listener;
    listener.listen(2000);
    listener.accept(socket);
}
else if (connectionType == 'C')
{
    socket.connect(ip, 2000);
}

socket.send(packetText.c_str(), packetText.length() + 1);
socket.receive(buffer, sizeof(buffer), received);
std::cout << buffer << std::endl;
system("pause");
}

random output when entering C

1 个答案:

答案 0 :(得分:0)

您可能希望将其视为错误。发生了什么 add_action( 'wp_ajax_ccs_fetch_list', array( __CLASS__, 'fetch_list' ) ); add_action( 'wp_ajax_nopriv_ccs_fetch_list', array( __CLASS__, 'fetch_list' ) ); class Person { public String name; public int count; public boolean favorite; // getters / setters ... } connect()都失败了,但您没有检查返回值。此外,您可能希望将send()清零,并在使用缓冲区打印之前检查是否确实收到了某些内容。                      - 肖恩克莱恩