我有麻煩閱讀超過15個字節recvfrom,我試圖改變SO_RCVBUF選項,並增加recvfrom參數的大小,但仍只能讀取15個字節。recvfrom只讀15字節
int a = 65535;
if (setsockopt(sd, SOL_SOCKET, SO_RCVBUF, &a, sizeof(int)) == -1) {
fprintf(stderr, "Error setting socket opts: %s\n", strerror(errno));
}
...
bytes = recvfrom(sd, buffer, 2048, 0,(struct sockaddr *) &addr, &addr_len);
但recvfrom函數仍然只讀取15個字節。
clientUDP.c
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdio.h>
int main (int argc, char ** argv){
int sd, addr_len, bytes;
char buffer[2048];
struct sockaddr_in addr;
sd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
bzero (&addr, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(8787);
inet_aton("127.0.0.1", &addr.sin_addr);
printf("->:");
gets(buffer);
sendto (sd, buffer , 15, 0 ,(struct sockaddr *)&addr, sizeof(addr));
do{
addr_len = sizeof(addr);
setsockopt(sd, SOL_SOCKET, SO_RCVBUF, &a, sizeof(int));
bytes = recvfrom(sd, buffer, 2048, MSG_WAITALL,,(struct sockaddr *) &addr, &addr_len);
if(bytes>0){
printf("Mensaje de %s: %d (%d bytes)\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), bytes);
printf("el mensaje dice :");
puts(buffer);
printf("-:");
gets(buffer);
sendto (sd, buffer , 15, 0 ,(struct sockaddr *)&addr, sizeof(addr));
}
}while(1);
return 0;
}
serverUDP.c
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
int main (int argc, char ** argv){
struct sockaddr_in addr;
int sd;
int bytes, reply_len, addr_len;
char buffer[2048];
sd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
int a = 65535;
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(8787);
addr.sin_addr.s_addr = INADDR_ANY;
if(bind(sd,(struct sockaddr *) &addr, sizeof(addr)) != 0){
printf("Error en BIND\n");
}
do {
addr_len = sizeof(addr);
bytes = recvfrom(sd, buffer, 2048, MSG_WAITALL,(struct sockaddr *) &addr, &addr_len);
setsockopt(sd, SOL_SOCKET, SO_RCVBUF, &a, sizeof(int));
if(bytes>0){
printf("Mensaje de %s: %d (%d bytes)\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), bytes);
printf("el mensaje dice :");
puts(buffer);
printf("-:");
gets(buffer);
sendto (sd, buffer , 15, 0 ,(struct sockaddr *)&addr, sizeof(addr));
}
}while(1);
}
可能聽起來像一個愚蠢的問題,但你確定你實際上應該得到更多? wireshark對數據包有什麼看法? – Timbo
您是否曾嘗試在標誌中添加MSG_WAITALL? – Nemo
它獲得一個字符串,我已經添加bytes = recvfrom(sd,buffer,2048,MSG_WAITALL,(struct sockaddr *)&addr,&addr_len);但是是一樣的,它就像一個聊天應用程序 –