套接字recv功能 [英] Socket recv function

查看:71
本文介绍了套接字recv功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想知道socket是如何接收其他对等体发送的可变长度数据的。如何识别

需要接收多少数据?

Regds

SNI

Hi,

I want to know how socket receive variable length data sent by other peer. How it is identify
how much data needs to be receive?
Regds
SNI

推荐答案

TCP是一种流式协议。在其中一个端点上,您正在以块的形式写入字节,而另一端则是将它们读出来。使用recv()读取的块大小不一定与您在连接另一侧写入的块大小相同。您不能假设send()和recv()大小之间存在任何关联,即使它们在快速本地网络或环回连接上通常是相同的!理论上,例如,如果你在一个块中发送5个字节(一个发送调用),那么另一个可能必须调用recv()五次每个recv()调用只接收1个字节,即使你的接收缓冲区大小足够大接收所有5个字节。您将不知道另一方写入套接字的数据大小。因此,您必须在协议中指定块大小(在通过网络发送的数据中)。在发送方,您可以开始发送带有长度字段的消息(例如uint16或您需要的任何整数宽度),因此在另一侧接收方将知道该消息有多少字节。从理论上讲,在最坏的情况下,另一方可以接收1个字节的消息(有很多recv调用,每个调用1个字节)但是,你知道首先必须接收长度字段的所有字节(如果是一个uint16长度它的2个字节),当完全接收到长度字段时,你知道消息剩下多少字节。
TCP is a streamed protocol. On one of the endpoints you are writing bytes in chunks and on the other side you are reading them out. The size of blocks you read out with recv() aren't necessarily the same as the block sizes you have written on the other side of the connection. You mustn't assume any correlation between send() and recv() sizes even if they are often the same on a fast local network or loopback connection! In theory for example if you send 5 bytes in one block (with one send call) then the other side may have to call recv() five times each recv() call receiving only 1 byte even if your receive buffer size is large enough to receive all 5 bytes. You will have no clue about the size of the data written into the socket on the other side. For this reason you have to indicate the block sizes in your protocol (in the data sent over the network). On the sender side you could begin sending your messages with a length field (for example an uint16 or whatever integer width you need) so on the other side the receiver will know how many bytes belong to this message. Theoretically in worst case the other side can receive your messages in 1 byte pieces (with a lot of recv calls each of them reading 1 byte) but still, you know that first you have to receive all bytes of the length field (in case of an uint16 length its 2 bytes) and when the length field is fully received then you know how many bytes are left from the message.


我回到我的FRC机器人团队我写了一些愿景需要通过TCP / IP与CRIO通信的代码。以下是它会帮助你的代码。



以下是我应该提醒你的一些提示。



RECV功能会停止你的程序,直到收到一个数据包,所以你很可能想从另一个线程调用它。 (正如我所做的那样)



I while back for my FRC robotics team I wrote some vision code that needed to communicate over TCP/IP to the CRIO. Here is a look at the code it will help you out a lot.

Here are some tips that I should warn you about.

The RECV function stops your program until a packet has been received so you most likely want to call it from a different thread. (as i did)

#include <stdio.h>
#include <stdlib.h>
#include "/usr/include/curl/curl.h"
#include "/usr/include/curl/easy.h"
#include "/usr/include/opencv/cv.h"
#include "/usr/include/opencv/highgui.h"
#include "/usr/include/opencv2/imgproc/imgproc.hpp"
#include <string>
#include <cstdio>
#include "math.h"
#include <syslog.h>
#include <queue>
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <fstream>
#include <string.h>
#include <unistd.h>
#include <stdlib.h> 
#include <pthread.h>

#define FROM_FILE
#define VISUALIZE
#define TIMING

using namespace cv;
using namespace std;
//using namespace boost;

int sockfd;

pthread_mutex_t angleTCPmutex;
pthread_mutex_t distanceTCPmutex;
pthread_t thread;
pthread_t TCPthread;

double TCPangleVal;
double TCPdistanceVal;

void *TCP_thread(void *obj){

	char TCPbuffer[1024];
	int TCPbytes = 0;
	double angleVal = 0;
	double distanceVal = 0;



	while(true){

		bzero(TCPbuffer, 1024);
		TCPbytes = recv(sockfd, TCPbuffer, 1023, 0);

		pthread_mutex_lock(&angleTCPmutex);
		angleVal = TCPangleVal;
		pthread_mutex_unlock(&angleTCPmutex);

		pthread_mutex_lock(&distanceTCPmutex);
		distanceVal = TCPdistanceVal;
		pthread_mutex_unlock(&distanceTCPmutex);

		std::ostringstream sstream;
		sstream << angleVal << ", " << distanceVal << "\n";
		std::string data_string = sstream.str();
			write(sockfd, data_string.data(), data_string.length());

	}

}





希望这会有所帮助:)



Hope this helps :)


这篇关于套接字recv功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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