add tk_socket_get_port

This commit is contained in:
lixianjing 2024-07-09 19:06:42 +08:00
parent 812189e184
commit f9e06fd701
3 changed files with 23 additions and 0 deletions

View File

@ -1,5 +1,8 @@
# 最新动态
2024/07/09
* 增加函数 tk\_socket\_get\_port (感谢智明提供补丁)
2024/07/05
* 修改assets\_manager\_unref没有释放资源列表中的数据问题和增加注释(感谢智明提供补丁)

View File

@ -110,6 +110,16 @@ ret_t tk_socket_bind(int sock, int port) {
return tk_socket_bind_ex(sock, NULL, port);
}
int tk_socket_get_port(int sock) {
struct sockaddr_in serv_addr;
socklen_t serv_addr_len = sizeof(serv_addr);
if (getsockname(sock, (struct sockaddr *)&serv_addr, &serv_addr_len) >= 0) {
return ntohs(serv_addr.sin_port);
}
return -1;
}
int tk_tcp_listen(int port) {
int sock;
int on = 1;

View File

@ -260,6 +260,16 @@ const char* tk_socket_get_self_ip_str(int sockfd, char* ip, int len);
*/
int tk_tcp_listen(int port);
/**
* @method tk_socket_get_port
* @annotation ["static"]
* socket
* @param {int} port
*
* @return {int} -1
*/
int tk_socket_get_port(int sock);
/**
* @method tk_tcp_accept
* @annotation ["static"]