您现在的位置是:首页 > 电脑 > 

Unix Socket发送/接收长消息(Unix Socket sending/receiving long messages)

2025-07-22 20:37:53
Unix Socket发送/接收长消息(Unix Socket sending/receiving long messages) 我正在使用tcp编写一个简单的应用程序层协议,我遇到了一个问题。 我想在消息发送中进行碎片化,因为消息太长了。 但我无法同步进程,客户端在服务器写入数据之前读取空缓冲区。 消息大约是4mb。 我该如何编写这些方法? 对于客户
Unix Socket发送/接收长消息(Unix Socket sending/receiving long messages)

我正在使用tcp编写一个简单的应用程序层协议,我遇到了一个问题。 我想在消息发送中进行碎片化,因为消息太长了。 但我无法同步进程,客户端在服务器写入数据之前读取空缓冲区。 消息大约是4mb。 我该如何编写这些方法?

对于客户

void send_message(string message); string receive_message()

对于服务器

void send_message(int sock,string message) string receive_message(int sock)

我的职能如下

void send_fragment(char* buffer,int length){ int n = write(sockfd, buffer, length); if (n < 0) { perror("ERROR writing to socket"); exit(1); } } string receive_fragment(){ char buffer[FRAGMET_LEGTH]; bzero(buffer,FRAGMET_LEGTH); int n = read(sockfd, buffer, FRAGMET_LEGTH-1); if (n < 0) { perror("ERROR reading from socket"); exit(1); } return string(buffer); } void send_message(string message){ char buffer[FRAGMET_LEGTH]; bzero(buffer,FRAGMET_LEGTH); int message_length = message.length(); //computes the number of fragment int number_of_fragment = ceil((double)message_length / FRAGMET_LEGTH); sprintf(buffer,"%d",number_of_fragment); //sends the number of fragment send_fragment(buffer,strlen(buffer)); for(int i=0;i<number_of_fragment;++i){ bzero(buffer,FRAGMET_LEGTH); //fragment interval int start = i*FRAGMET_LEGTH; int end = (i+1)*FRAGMET_LEGTH; if(i==number_of_fragment-1){ end = min(end,message_length); } //creates a fragment ct char* fragment = message.substr(start,end).c_str(); sprintf(buffer,"%s",fragment); //sends the fragment send_fragment(buffer,strlen(buffer)); } } string receive_message(){ //receive and computes the number of fragment string number_of_fragment_string = receive_fragment(); int number_of_fragment = atoi(number_of_fragment__str()); string message =""; for(int i=0;i<number_of_fragment;++i){ //concatenating fragments message += receive_fragment(); } return message; }

I am writing a simple application layer protocol using tcp and I encounter a problem. I want to make fragmentation in message sending because messages are so long. But I cannot synchronize the process and the client reads empty buffer before the server writes the data. The messages are approximately 4mb. How can I write these methods?

For client

void send_message(string message); string receive_message()

For server

void send_message(int sock,string message) string receive_message(int sock)

My functi are below

void send_fragment(char* buffer,int length){ int n = write(sockfd, buffer, length); if (n < 0) { perror("ERROR writing to socket"); exit(1); } } string receive_fragment(){ char buffer[FRAGMET_LEGTH]; bzero(buffer,FRAGMET_LEGTH); int n = read(sockfd, buffer, FRAGMET_LEGTH-1); if (n < 0) { perror("ERROR reading from socket"); exit(1); } return string(buffer); } void send_message(string message){ char buffer[FRAGMET_LEGTH]; bzero(buffer,FRAGMET_LEGTH); int message_length = message.length(); //computes the number of fragment int number_of_fragment = ceil((double)message_length / FRAGMET_LEGTH); sprintf(buffer,"%d",number_of_fragment); //sends the number of fragment send_fragment(buffer,strlen(buffer)); for(int i=0;i<number_of_fragment;++i){ bzero(buffer,FRAGMET_LEGTH); //fragment interval int start = i*FRAGMET_LEGTH; int end = (i+1)*FRAGMET_LEGTH; if(i==number_of_fragment-1){ end = min(end,message_length); } //creates a fragment ct char* fragment = message.substr(start,end).c_str(); sprintf(buffer,"%s",fragment); //sends the fragment send_fragment(buffer,strlen(buffer)); } } string receive_message(){ //receive and computes the number of fragment string number_of_fragment_string = receive_fragment(); int number_of_fragment = atoi(number_of_fragment__str()); string message =""; for(int i=0;i<number_of_fragment;++i){ //concatenating fragments message += receive_fragment(); } return message; }

最满意答案

您必须在自己的代码中实现框架。 TCP是一个“流”,意味着它只发送没有任何开始/结束指示的字节。 (UDP是基于数据包的,但不适合您的大小的数据包。)

最简单的方法是将4字节长度写入套接字并让接收方读取这些字节,记住endianess是一个问题(使用htonl()和ntohl()将本地表示转换为“网络顺序”)。

然后继续读取该字节数。 完成后,您已收到消息。

如果你使用阻塞读取,它会相当简单 - 如果你得到更少,那么连接就会中断。 如果你使用非阻塞读取,你必须组装你得到的碎片(你甚至可以得到碎片的长度,尽管不太可能),每次读取调用。

还有其他方法可以构建您的数据,但这是最简单的方法。

You have to implement the framing in your own code. TCP is a "stream" meaning it just sends bytes without any sort of start/end indication. (UDP is packet-based but not suitable for packets of your size.)

The simplest method would be to write a 4-byte length to the socket and have the receiving side read those bytes, remembering that endianess is an issue (use htonl() and ntohl() to convert local representati to "network order").

Then proceed to read that number of bytes. When that is done, you've received your message.

If you use blocking reads, it'll be fairly simple -- if you get less then the connection has broken. If you use non-blocking reads, you have to assemble the pieces you get (you could even get the length in pieces, though unlikely) back with each read call.

There are other ways of framing your data but this is the simplest.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/79512.html

相关标签:无
上传时间: 2023-04-28 04:54:47
留言与评论(共有 5 条评论)
本站网友 360ddt
4分钟前 发表
strlen(buffer)); for(int i=0;i<number_of_fragment;++i){ bzero(buffer
本站网友 北京整形最好的医院
10分钟前 发表
fragment); //sends the fragment send_fragment(buffer
本站网友 西安心理
9分钟前 发表
message_length); } //creates a fragment ct char* fragment = message.substr(start
本站网友 操控
27分钟前 发表
"%s"