Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to obtain the local source port number of socket communication in linux

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "socket communication in linux how to get the local source port number", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn how to obtain the local source port number of socket communication in linux "this article.

There is a lot of information about TCP IP network communication, and TCP IP communicates end-to-end through IP packet mode. A typical TCP packet is as follows

You can see that the packet contains the source port number and the destination port number. When the client socket initiates a connection to the server, the system will randomly assign a source port number to the socket. We can use getsocketname to obtain the original port information of the successfully connected socket.

Function prototype

# include int getsockname (int sockfd, struct sockaddr * addr, socklen_t * addrlen)

Parameters:

Handle to the sockfd socket connection

Addr network address pointer, which is used to store local socket address information

Space size of addrlen addr

Returns the result, returns 0 if the call is successful, stores the local network address information in addr, returns-1 for failure, and responds to the error message through errno.

Source_port.cpp

# include # include void safe_close (int & sock); int main (int argc, char * argv []) {int sockfd = 0, n = 0; socklen_t len = 0; char host [1024] = {0}; struct hostent * server; struct sockaddr_in serv_addr, loc_addr; if (argc)

< 2) { printf("Please input host name\n"); exit(-1); } strncpy(host, argv[1], sizeof(host)); server = gethostbyname(host);// 判断输入的域名是否正确 if (NULL == server) { printf("find host: %s failed.\n", host); exit(-1); } if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))) {// 创建socket memset(buf, 0, sizeof(buf)); snprintf(buf, sizeof(buf), "new socket failed. errno: %d, error: %s", errno, strerror(errno)); perror(buf); exit(-1); } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(80);// http标准端口号 memcpy(&serv_addr.sin_addr.s_addr, server->

H_addr, server- > h_length); if (- 1 = = inet_pton (AF_INET, host, & serv_addr.sin_addr)) {memset (buf, 0, sizeof (buf)); snprintf (buf, sizeof (buf), "inet_pton failed. Errno:% d, error:% s ", errno, strerror (errno); perror (buf); exit (- 1);} if (- 1 = = connect (sockfd, (struct sockaddr *) & serv_addr, sizeof (serv_addr)) {/ / connect socket memset (buf, 0, sizeof (buf)); snprintf (buf, sizeof (buf)," connect socket failed. Errno:% d, error:% s ", errno, strerror (errno); perror (buf); exit (- 1);} printf (" connect to% s success.\ n ", host); len = sizeof (sizeof (loc_addr)); memset (& loc_addr, 0, len) If (- 1 = = getsockname (sockfd, (struct sockaddr *) & loc_addr, & len)) {/ / get the local address information bound by socket memset (buf, 0, sizeof (buf)); snprintf (buf, sizeof (buf), "get socket name failed. Errno:% d, error:% s ", errno, strerror (errno); perror (buf); safe_close (sockfd); exit (- 1);} if (loc_addr.sin_family = = AF_INET) {/ / print message printf (" local port:% u\ n ", ntohs (loc_addr.sin_port));} safe_close (sockfd); return 0 } void safe_close (int & sock) {if (- 1! = sock) {shutdown (sock, SHUT_RDWR); sock =-1;}}

This program will first start a socket to connect to an ordinary http server (baidu,qq,163,csdn). When socket is connected, it will obtain the local address of the connection binding through getsocketname, and obtain the source port number through this address.

Terminal 1: compile and run

$Gmail + source_port.cpp$. / a.out www.baidu.comconnect to www.baidu.com success.local port: 39702

Terminal 2: pass tcpdump packet capture verification

$sudo tcpdump host www.baidu.com-vtcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes18:38:32.381448 IP (tos 0x0, ttl 64, id 35033, offset 0, flags [DF], proto TCP (6), length 60) icentos.39702 > 220.181.111.188.http: Flags [S], cksum 0x8cd2 (incorrect-> 0x596a), seq 2381397554, win 29200, options [mss 1460 maestro sackook TS val 3513497323 ecr 0jnopjour WScale 7], length 018Ranger 3832.425904 IP (tos 0x0, ttl 55) Id 35033, offset 0, flags [DF], proto TCP (6), length 60) 220.181.111.188.http > icentos.39702: Flags [S.], cksum 0xc315 (correct), seq 3561856904, ack 2381397555, win 8192, options [mss 1424 IP [DF], proto TCP (6), length 40)

Comparing terminal one and terminal two shows that the source port address obtained is correct.

The above is all the content of the article "how to get the local source port number of socket communication in linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report