Home > Back-end >  In Linux to udp socket send far larger than the size of the buffer data, why can receive?
In Linux to udp socket send far larger than the size of the buffer data, why can receive?

Time:09-29

Bosses, I want to ask about, Linux udp datagram socket receives the receive buffer size size beyond the socket, why can still receive?

Linux manual udp said: when the received data, reporting for not receive buffer into the socket, the datagram is truncated,

I used to receive the setsockopt udp socket set up smaller after receiving buffer (set to end with getsockopt check, no problem), with the local loopback another process sent it several times greater than the buffer data, but can still receive,

The receiving process:
 
Char buf [40000].
Int confd=socket (AF_INET, SOCK_DGRAM, 0);
Struct sockaddr_in addr.
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=htonl (INADDR_ANY);
Addr. Sin_port=htons (9122);
Bind (confd, & amp; Addr, sizeof (addr));
Int val=3000;
Int res=setsockopt (confd, SOL_SOCKET, SO_RCVBUF, & amp; Val, sizeof (int));
Int len=sizeof (int);
Res=getsockopt (confd, SOL_SOCKET, SO_RCVBUF, & amp; Val, & amp; Len);
Printf (" val res: % d: % d ", res, val);
Sleep (60);
Int n=recvfrom (confd, buf, sizeof (buf), 0, NULL, NULL);
Printf (" % s \ n ", buf);
First: printf (" % d \ n ", n);


Sending process:
 
Char buf [40000].
Int sendfd=socket (AF_INET, SOCK_DGRAM, 0);
Struct sockaddr_in addr.
Addr. Sin_family=AF_INET;
Addr. Sin_addr. S_addr=inet_addr (" 127.0.0.1 ");
Addr. Sin_port=htons (9122);
Memset (buf, 1, sizeof (buf));
Int n=sendto (sendfd, buf, sizeof (buf), 0, & amp; Addr, sizeof (addr));
First: printf (" % d \ n ", n);
  • Related