>  기사  >  운영 및 유지보수  >  Linux에서 로컬 소스 포트 번호를 얻기 위해 소켓 통신을 구현하는 방법

Linux에서 로컬 소스 포트 번호를 얻기 위해 소켓 통신을 구현하는 방법

黄舟
黄舟원래의
2017-09-29 11:15:052796검색

이 기사에서는 로컬 소스 포트 번호를 얻기 위한 Linux 소켓 통신에 대한 관련 정보를 주로 소개합니다. 도움이 필요한 친구들은 이를 참고할 수 있습니다.

TCP IP 네트워크 통신에 대한 많은 정보가 있습니다. IP 패킷 모드를 통해. 일반적인 TCP 데이터 패킷은 다음과 같습니다

데이터 패킷에는 소스 포트 번호와 대상 포트 번호가 포함되어 있는 것을 볼 수 있습니다. 클라이언트 소켓이 서버에 대한 연결을 시작하면 시스템이 소스 포트를 무작위로 할당합니다. 성공적으로 연결된 소켓의 원래 포트 정보를 얻기 위해 getsocketname을 전달할 수 있습니다.

함수 프로토타입


#include <sys/socket.h> 
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

매개변수:

sockfd 소켓 연결 핸들

addr 네트워크 주소 포인터, 로컬 소켓 주소 정보를 저장하는 데 사용됨,

addrlen addr 공간 크기

호출이 있는 경우 결과 반환 성공하면 0을 반환하고 로컬 네트워크 주소 정보를 addr에 저장한다. 실패하면 -1을 반환하고 errno를 통해 오류 메시지를 반영한다.

source_port.cpp


#include <cstring> 
#include <cstdio> 
#include <cstdlib> 
#include <sys/socket.h> 
#include <sys/types.h> 
#include <netinet/in.h> 
#include <netinet/ip.h> 
#include <netdb.h> 
#include <errno.h> 
#include <unistd.h> 
#include <arpa/inet.h> 
void safe_close(int &sock); 
int main(int argc, char *argv[]) { 
 int sockfd = 0, n = 0; 
 socklen_t len = 0; 
 char host[512] = {0}; 
 char buf[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))) {// 连接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)) {// 获取socket绑定的本地address信息 
  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) {// 打印信息 
  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; 
 } 
}

이 프로그램은 먼저 일반 http 서버(baidu, qq, 163, csdn)에 연결하기 위해 소켓을 시작합니다. 소켓이 연결되면 연결의 로컬 주소를 얻습니다. getsocketname을 통해 바인딩하고 이 주소를 통해 소스 포트 번호를 가져옵니다.

터미널 1: 컴파일 및 실행


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

터미널 2: tcpdump 패킷 캡처를 통해 확인


$ sudo tcpdump host www.baidu.com -v
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
18: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,sackOK,TS val 3513497323 ecr 0,nop,wscale 7], length 0
18:38:32.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,sackOK,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,nop,wscale 5], length 0
18:38:32.425930 IP (tos 0x0, ttl 64, id 35034, offset 0, flags [DF], proto TCP (6), length 40)

터미널 1과 터미널 2를 비교하면 얻은 소스 포트 주소가 올바른 것을 알 수 있습니다.

요약

위 내용은 Linux에서 로컬 소스 포트 번호를 얻기 위해 소켓 통신을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.