recvfrom只读15个字节

时间:2014-01-19 18:43:32

标签: c sockets udp

我无法使用recvfrom读取超过15个字节,我试图更改SO_RCVBUF选项并增加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);
}

2 个答案:

答案 0 :(得分:4)

UDP是基于消息的,与基于流的TCP不同。 recv/from()无法接收部分UDP数据,读取是全有或全无。如果传入2048字节的缓冲区并且recv/from()报告读取了15个字节,则UDP消息有效负载的大小确实为15个字节。您可以使用数据包嗅探器(如Wireshark)来验证。

答案 1 :(得分:1)

我的错误我不知道为什么我把15放在sendto函数中,所以只发出了15个字节的数据...... XD

    sendto (sd, buffer , 15, 0 ,(struct sockaddr *)&addr, sizeof(addr));

解决了增加15到2048

的问题