获取进入UDP数据包的目标端口



我已经阅读了IP MAN页面以及套接字人页面,我所能找到的是标题中IP_PKTINFO控制消息中的目标IP地址,似乎没有从数据包的标题中拉出目标端口的方式。还是在那里?

如果没有,是否有任何方法可以从数据包中获取端口?

当您使用普通套接字时,可以在插座FD上使用RecV。recvfrom的人页:

recv(2(

名称

   recv, recvfrom, recvmsg - receive a message from a socket

摘要

        #include <sys/types.h>
        #include <sys/socket.h>
        ssize_t recv(int sockfd, void *buf, size_t len, int flags);
        ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
                         struct sockaddr *src_addr, socklen_t *addrlen);
        ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);

描述

   The  recv(),  recvfrom(),  and recvmsg() calls are used to receive messages from a socket.  They
   may be used to receive data on both connectionless and connection-oriented sockets. 

recvfrom(( recvFrom((将收到的消息放入缓冲区BUF中。呼叫者必须指定大小 Len中的缓冲区

   If src_addr is not NULL, and the underlying protocol provides the source address of the message,
   that source address is placed in the buffer pointed to by src_addr.  In this case, addrlen is  a
   value-result  argument.   Before  the  call,  it should be initialized to the size of the buffer
   associated with src_addr.  Upon return, addrlen is updated to contain the  actual  size  of  the
   source  address.  The returned address is truncated if the buffer provided is too small; in this
   case, addrlen will return a value greater than was supplied to the call.
   If the caller is not interested in the source address, src_addr and addrlen should be  specified
   as NULL.

Beej指南的一个示例:

// datagram sockets and recvfrom()
struct addrinfo hints, *res;
int sockfd;
int byte_count;
socklen_t fromlen;
struct sockaddr_storage addr;
char buf[512];
char ipstr[INET6_ADDRSTRLEN];
// get host info, make socket, bind it to port 4950
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;  // use IPv4 or IPv6, whichever
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = AI_PASSIVE;
getaddrinfo(NULL, "4950", &hints, &res);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
bind(sockfd, res->ai_addr, res->ai_addrlen);
// no need to accept(), just recvfrom():
fromlen = sizeof addr;
byte_count = recvfrom(sockfd, buf, sizeof buf, 0, &addr, &fromlen);
printf("recv()'d %d bytes of data in bufn", byte_count);
printf("from IP address %sn",
    inet_ntop(addr.ss_family,
        addr.ss_family == AF_INET?
            ((struct sockadd_in *)&addr)->sin_addr:
            ((struct sockadd_in6 *)&addr)->sin6_addr,
        ipstr, sizeof ipstr);

recvfrom的示例,recvfrom的人页

希望这有帮助

最新更新