在 C 和 python 中实现 sendall() 和 recvall() [英] implementing sendall() and recvall() in C and python

查看:29
本文介绍了在 C 和 python 中实现 sendall() 和 recvall()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试在用 C 编写的服务器中实现一个 sendall() 函数,并在用 python 编写的相应客户端上实现一个 recvall() 函数.

I'm currently trying to implement a sendall() function in a server written in C, and a recvall() function on the corresponding client written in python.

当服务器和客户端都用相同的语言(即都用 c 或都用 python)编写时,我可以让它们一起工作,但我不能让它在 c 和服务器上工作python中的客户端.目前,我想从 c 服务器向 python 客户端发送一个简单的字符串.

I can get the server and the client to work together when they're both written in the same language (i.e. both in c or both in python), but I can't get it to work with the server in c and the client in python. currently, i want to send a simple string from the c server to the python client.

C 中的服务器 sendall() 实现如下(参考 Beej 的指南):

Server sendall() implementation in C as follows (referenced from Beej's guide):

int sendall(int socket, char * buf, int *len){
    int total = 0;        // how many bytes we've sent
    int bytesleft = *len; // how many we have left to send
    int n;

    while(total < *len) {
        n = send(socket, buf + total, bytesleft, 0);
        if (n == -1) { break; }
        total += n;
        bytesleft -= n;
    }

    *len = total; // return number actually sent here

    return n==-1?-1:0; // return -1 on failure, 0 on success
}

调用函数:

char buf[10] = "test";
    int len;

    len = strlen(buf);

    if (sendall(command_socket, buf, &len) == -1) {
        perror("sendall");
        printf("We only sent %d bytes because of the error!\n", len);
    }

    printf("Bytes sent: %d\n", len);

Python 中的客户端 recvall() 实现(参考自 http://stupidpythonideas.blogspot.com/2013/05/sockets-are-byte-streams-not-message.html):

Client recvall() implementation in Python (referenced from http://stupidpythonideas.blogspot.com/2013/05/sockets-are-byte-streams-not-message.html):

def recv_one_message(sock):
    lengthbuf = recvall(sock, 4)
    length, = struct.unpack('!I', lengthbuf)
    return recvall(sock, length)

def recvall(sock, count):
    buf = ''
    while count:
        newbuf = sock.recv(count)
        print newbuf

        if not newbuf:
            return None

        buf += newbuf
        count -= len(newbuf)
    return buf

称为:

buf = recv_one_message(command_socket)
print buf

每当我从 C 服务器向 Python 客户端发送消息时,我都会得到无"的返回.我已经跟踪了客户端的响应——它正在获取我发送的消息,但最终响应始终为无",并且不会打印消息.我也试过只返回消息而不是 None 返回,这也导致没有打印任何内容.看不出我哪里错了,有什么想法吗?谢谢.

Whenever I send a message from the C-server to the Python-client, I get a return of "None." I've traced the the response coming in on the client side -- it is getting the message I sent, but the final response is always "none" and the message won't be printed. I've also tried just returning the message instead of having the None return, which also results in nothing getting printed. Can't see where I'm going wrong, any thoughts? Thanks.

推荐答案

假设你有一个非常流利的英语和一个非常流利的法语.他们将无法很好地相互沟通.谁有错?-- 谁把他们两个放在一起,希望他们能够交流,而无需先就如何交流达成一致.

Say you have a perfectly fluent English speaker and a perfectly fluent French speaker. They will not be able to communicate with each other very well. Who is at fault? -- Whoever put the two of them together expecting them to be able to communicate without first agreeing on how they would communicate.

您的发送函数和接收函数实现了完全不同的协议.发送函数要求接收者已经知道要接收的数据的长度.您的接收功能要求发送方在数据之前发送长度.所以你不能混合和匹配它们,因为它们不使用相同的线格式

Your send function and your receive function implement totally different protocols. The send function requires the receiver to already know the length of the data to receive. Your receive function requires the sender to send the length prior to the data. So you cannot mix and match them because they do not use the same wire format

以下是从数十年的经验中获得的一些宝贵建议:切勿在未首先记录您将要在 TCP 上使用的协议的情况下尝试使用 TCP 字节等级.事实上,没有办法知道哪个函数是正确的,哪个是错误的,因为没有人知道应该通过网络发送哪些字节.

Here's some valuable advice gained over decades of experience: Never attempt to use TCP without first documenting the protocol you're going to use on top of TCP at the byte level. As it is, there's no way to know which function is right and which is wrong because nobody has any idea what bytes are supposed to be sent over the wire.

TCP 是一种字节流协议,为了让两个程序在它们之间的 TCP 连接下正常工作,每个程序都必须准确地发送另一个期望接收的字节流.确保发生这种情况的最佳方法是首先记录该字节流.

TCP is a byte-stream protocol, and for two programs to work correctly with a TCP connection between them, each must send precisely the stream of bytes the other expects to receive. The best way to make sure this happens is to first document that stream of bytes.

如果您不知道从哪里开始使用此类文档,请查看与您正在尝试执行的操作类似的最简单的现有协议.查看它的可能协议包括 HTTP、SMTP、IRC 和 DNS.

If you have no idea where to start with such documentation, have a look at the simplest existing protocol that's analogous to what you're trying to do. Possible protocols to look at it include HTTP, SMTP, IRC, and DNS.

这篇关于在 C 和 python 中实现 sendall() 和 recvall()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆