Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
套接字无法接收数据?客户端是使用Boost.asyio库编写的。服务器是用python编写的_Python_C++_Sockets_Boost - Fatal编程技术网

套接字无法接收数据?客户端是使用Boost.asyio库编写的。服务器是用python编写的

套接字无法接收数据?客户端是使用Boost.asyio库编写的。服务器是用python编写的,python,c++,sockets,boost,Python,C++,Sockets,Boost,客户端代码 客户端可以向服务器发送ok,但无法从服务器接收123 如果使用python编写的客户机,服务器和客户机可以很好地通信 有人能告诉我这个错误的原因吗?我也发现了类似的问题 使用read\u until可以解决这个问题。使用tcp::socket::read\u some方法。此方法将阻塞,直到套接字接收到某些内容,然后读取数据,而读取。。。函数读取整个文件/套接字直到EOF,只有在连接断开时才会发生EOF 例如: read函数一直读取到EOL,这仅在TCP连接关闭时发生。如果终止服务器

客户端代码

客户端可以向服务器发送ok,但无法从服务器接收123

如果使用python编写的客户机,服务器和客户机可以很好地通信


有人能告诉我这个错误的原因吗?

我也发现了类似的问题

使用read\u until可以解决这个问题。

使用tcp::socket::read\u some方法。此方法将阻塞,直到套接字接收到某些内容,然后读取数据,而读取。。。函数读取整个文件/套接字直到EOF,只有在连接断开时才会发生EOF

例如:


read函数一直读取到EOL,这仅在TCP连接关闭时发生。如果终止服务器,您会注意到客户端将立即读取数据并关闭套接字。@R.Joiny您知道如何限制最大缓冲区大小吗?我希望每次都读取1024k数据。使用tcp套接字方法read_some,您可以通过限制boost::asio::buffer来设置缓冲区大小,就像使用ok:bufferok,2一样
#include <cstdlib>
#include <iostream>
#include <memory>
#include <utility>
#include <boost/asio.hpp>
#include <string>
#include <boost/array.hpp>

using namespace std;
using namespace boost::asio;

int main(int argc, char* argv[])
{

    io_service service;
    ip::tcp::endpoint ep(ip::address::from_string("127.0.0.1"), 2001);

    ip::tcp::socket sock(service);
    sock.connect(ep);

    boost::asio::streambuf sb;
    boost::system::error_code ec;
    size_t len;
    while (true)
    {
        //write data
        sock.write_some(buffer("ok", 2));
        //read data
        read(sock, sb, transfer_all(),ec);
        //handle error
        if (ec && ec != error::eof) {
            std::cout << "receive failed: " << ec.message() << std::endl;
        }
        else {
            const char* data = buffer_cast<const char*>(sb.data());
            std::cout << data << std::endl;
        }
    }
    return 0;
}
import socket
import threading

s = socket.socket()
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind(("127.0.0.1",2001))

s.listen(5)
cons = []

print "begin....."
while True:
    con,addr = s.accept()
    #cons.append(con)
    print con
    while True:
        data = con.recv(1024)
        if not data:
            con.close()
            break;
        print data
        con.send("123")
s.close()
std::string recbuf;
while (true)
{
    //write data
    sock.write_some(buffer("ok", 2));
    //read data
    sock.read_some(buffer(recbuf, 1024), ec);
    //handle error
    if (ec && ec != error::eof) {
        std::cout << "receive failed: " << ec.message() << std::endl;
    }
    else {
        std::cout << recbuf << std::endl;
    }
}