我知道TCP中没有数据包概念,因为它是一个流套接字,那么例如,如果我有2000字节的数据包,说2000'a',并且我的默认缓冲区大小是1024,那么它应该发送两次并接收两次?有关数据包和缓冲区大小的TCP套接字编程
所以在send()函数,
iResult = send(s, sendbuf, packet_size, 0);
第二个参数,我应该把什么?一个1024字节mallocated的发送缓冲区字符指针或一个2000字节的数据包字符指针,它会自动为我处理它?
和recv()阻塞函数,我应该把缓冲区字符指针指向第二个参数或数据包之一?
对于标题,我的朋友建议我添加4个字节的标题来存储包的信息,例如。数据包的序列号和大小,如何实现?谢谢
@Giorgi,谢谢!我也想问,如果我不做部分写入处理和while循环中的发送速度非常快(没有睡眠),那么服务器端会出现错误/丢失?为的recv()
SOCKET newsfd;
bind(s, (struct sockaddr *)ReceiverSocket, sizeof(struct sockaddr_in));
if (strcmp(protocol, "tcp") == 0 || strcmp(protocol, "TCP") == 0){
listen(s, 1);
newsfd = accept(s, 0, 0);
}
//*** Create Update Display Thread
std::thread th(Function_packet_transmission_display, update_interval, (char*) "recv");
//*** Receive Data//*** Set Jitter
long time_old = 0, time_new = 0, time_start = 0;
long float jitter_new = 0, jitter_old = 0;
long long temp_accubyte = 0, temp_pktnum = 0; //testing
char *recvbuf = new char[buffer_size];
long long next_seq_num = 1; int retVal;
do{
if (strcmp(protocol, "tcp") == 0 || strcmp(protocol, "TCP") == 0){
retVal = recv(newsfd, recvbuf, packet_size, 0);
if ((retVal == SOCKET_ERROR) || (retVal == 0)){
printf("\nreturn fail code:%i\n", WSAGetLastError());
closesocket(s);
WSACleanup();
lck.lock();
Ended = true;
lck.unlock();
return 0;
}
}
else if (strcmp(protocol, "udp") == 0 || strcmp(protocol, "UDP") == 0){
int fromlen = (int)sizeof(struct sockaddr_in);
retVal = recvfrom(s, recvbuf, packet_size, 0, (struct sockaddr *)ReceiverSocket, &fromlen);
}
//testing
temp_accubyte += retVal;
temp_pktnum++;//TEST
//printf("\racc: %lld %lld - ", temp_accubyte, temp_pktnum);
//if (temp_pktnum==100000) printf("\nReach 100000\n", temp_accubyte, temp_pktnum);
if (timer == NULL){
timer = new ES_FlashTimer();
}
@Giorgi,谢谢!我也想问,如果我不做部分写入处理和while循环中的发送速度非常快(没有睡眠),那么服务器端会出现错误/丢失?对于recv() –