Both Recv and recvfrom are used to accept data from the network. Let's take a look at their prototype:
Int Recv (
Socket{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> S,
Char far*{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Buf,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Len,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Flags
);
Int recvfrom (
Socket{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> S,
Char far *{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Buf,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Len,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Flags,
Struct sockaddr far*{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> From,
Int far*{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Fromlen
);
This is defined in windows. The definition in Linux only changes socket to int, so the prototype in Linux is as follows:
Int Recv (
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> S,
Char far*{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Buf,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Len,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Flags
);
Int recvfrom (
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> S,
Char far *{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Buf,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Len,
Int{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Flags,
Struct sockaddr far*{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> From,
Int far*{
Function onclick ()
{
Function onclick ()
{
Showtip (this)
}
}
} "> Fromlen
);
In fact, if we look at the socket definitions in windows, we will know that they are almost identical. Why is it almost? The difference is that the int type is used in Linux and the unsigned int type is used in windows. The socket definition in Windows is as follows:
Typedef uint_ptr socket;
Typedef unsigned int uint_ptr, * puint_ptr;
In Linux, int s represents the file descriptor. All the devices in Linux, such as disks, optical drives, USB flash drives, and even the networks we discuss here are also considered as files.
Let's take a look at the similarities and differences between the two functions:
Commonalities:
1. It is used to accept data from the network.
2. Both are acceptableConnection-oriented streaming socketAnd acceptConnectionless datagram socketData
3. after data is successfully accepted, the returned values are the actual number of bytes accepted. When the socket is disabled, 0 is returned. When an error is accepted, socket_error is returned in windows, and-1 is returned in Linux, in fact, if you are interested, you can view the socket_error definition, and its value is-1;
Note that when two functions are used in a streaming socket and a datagram socket, the meaning of the socket is different. The former indicates the client socket, the latter indicates its own socket.
4. if the socket is blocked and there is no data in the system buffer, it will be blocked. If the socket is not blocked and there is no data in the system buffer, will be returned immediately, the returned value is-1 in Linux, errno is set to ewouldblock, socket_error is returned in windows, and wsaewouldblock is returned through wsagetlasterror.
5.For streaming socketsThen, the 2's operation is: copy the data in the kernel buffer to the application's own buffer, and the maximum copy length is the length of the buffer introduced when the function is called, note that the length here is not necessarily equal to the actual buffer length. It can be smaller than the buffer length, but it cannot be greater than. Why cannot it be greater than it? Maybe you know better than me. For example, the following code:
Char szrecvbuf [1024] = {0 };
Recv (sockserver, szrecvbuf, 256, 0 );
Although the defined buffer length is 1024, only 256 of the buffer length is used. if the kernel buffer had 10 bytes at the time, the call immediately returned. szrecvbuf was filled with 10 bytes and the returned value was 10. If the kernel buffer contains 1500 bytes, szrecvbuf will be filled with 256 bytes, and the returned value is 256.
If it is a datagram socket, the data in the kernel buffer is smaller than the required length (256 here), and the result is the same as that of the streaming socket. However, if the value is greater than the value, it is different. First, it is filled with 256 to szrecvbuf, and a wasemsgsize error is generated, and the remaining part is discarded. If the data in the kernel buffer is 1000 bytes, the first 256 is filled in szrecvbuf, And the next 1000-256 is discarded.
The execution result of recvfrom is the same.
The conclusion above is based on msdn and actual tests.
The actual effect of recvfrom is the same.
Differences:
1. Different Function Parameters
To be continued...