Update mc_tcp.cpp

This commit is contained in:
Eao3piq4e 2022-08-09 17:11:49 +08:00
parent 8cd2925f93
commit 78d7d8e54f
1 changed files with 21 additions and 0 deletions

View File

@ -81,6 +81,27 @@ void mc_tcp_set_keepalive(int fd)
mc_tcp_setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, (char*)&count, sizeof(count));
}
/*
function name: mc_tcp_get_peer_name
description: This function is used to obtain the host IP and port number of the host bound to the specific socket.
arguments: The first argument is a descriptor to a specified socket.
The second argument is used to store the host IP address bound to the socket determined by the first parameter, in dotted decimal.
The third parameter is used to store the port number bound to a specific socket, in the order of host bytes.
return value: Return 0 if the function runs successfully.
When the call to the getpeername() function fails
1Return EBADF if the socket argument is not a valid file descriptor.
2Return EINVAL if the socket has been shut down.
3Return ENOTCONN if the socket is not connected or otherwise has not had the peer pre-specified.
4Return ENOTSOCK if the socket argument does not refer to a socket.
5Return EOPNOTSUPP if the operation is not supported for the socket protocol.
6Return ENOBUFS if insufficient resources were available in the system to complete the call.
Return -2 when the host IP address belongs to IPv4 type, it fails to convert it to dotted decimal.
Return -3 when the host IP address belongs to IPv6 type, it fails to convert it to dotted decimal.
Return -4 when the error type is not any of the above.
note: Allocate a certain amount of memory space for the host and port pointers respectively in advance.
date: 2022/8/9
contact tel: 18720816902
*/
int mc_tcp_get_peer_name(int fd, char* host, int* port)
{
struct sockaddr peeraddr = {0};