简体   繁体   English

如果我用一个监听套接字监听 2 个端口,我怎么知道哪个端口收到了数据包?

[英]if I am listening on 2 ports with one Listening Socket how can I tell which port received the packet?

I have an application that is listening on 2 ports, it appears that when I call my function WS_SetUpListener (see code below) with 2 different ports, I receive the same ListeningSocket for both, so when a packet arrives at either of the 2 ports, how can I tell what port it was sent to?我有一个正在侦听 2 个端口的应用程序,似乎当我用 2 个不同的端口调用我的 function WS_SetUpListener(见下面的代码)时,我收到了相同的 ListeningSocket,所以当数据包到达两个端口中的任何一个时,我怎么知道它被发送到哪个端口?

I call the function as follows:我将 function 称为如下:

ListeningSocket = WS_SetUpListener(port);

the code for it is:它的代码是:

SOCKET WS_SetUpListener(int port)
{
char port_buf[20] = {0};
struct addrinfo *result = NULL, hints;

SOCKET ListenSocket = INVALID_SOCKET;

//char recvbuf[DEFAULT_BUFLEN];
int iResult;
//int iSendResult;
int recvbuflen = DEFAULT_BUFLEN;

sprintf_s(port_buf, sizeof(port_buf), "%d", port);


ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

// Resolve the local address and port to be used by the server
iResult = getaddrinfo(NULL, port_buf, &hints, &result);
if (iResult != 0) 
{
    printf("getaddrinfo failed: %d\n", iResult);
    return INVALID_SOCKET;
}

// Create a SOCKET for the server to listen for client connections
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET)
{
    printf("***ERROR*** at socket(): %ld\n", WSAGetLastError());
    freeaddrinfo(result);
    return INVALID_SOCKET;
}

// Set up the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
    printf("bind failed: %d\n", WSAGetLastError());
    freeaddrinfo(result);
    closesocket(ListenSocket);
    return INVALID_SOCKET;
}

freeaddrinfo(result);

iResult = listen(ListenSocket, SOMAXCONN);
if ( iResult == SOCKET_ERROR ) 
{
    printf( "Error at bind(): %ld\n", WSAGetLastError() );
    closesocket(ListenSocket);
    return INVALID_SOCKET;
}

return ListenSocket;
 }

What?什么? The port number that you are listening on will not be the same port number that your remote peer will send you packets on.您正在侦听的端口号与远程对等方将向您发送数据包的端口号不同。 You would have to remember which port number you accepted the connection with.您必须记住您接受连接的端口号。

Try using getpeername if you want to get the port number that you and your peer are using.如果您想获取您和您的对等方正在使用的端口号,请尝试使用getpeername

You would use the select() routine and the fdset and fdisset macros.您将使用 select() 例程以及 fdset 和 fdisset 宏。

Differentiating Between Sockets using Select 使用 Select 区分 Sockets

If you're using recvfrom() to read the packet data you can extract the socket info, which will include port information.如果您使用recvfrom()读取数据包数据,您可以提取套接字信息,其中将包括端口信息。 How are you reading the packets in?你是如何读取数据包的?

EDIT: actually, wait a sec.编辑:实际上,等一下。 How can you receive the same ListeningSocket for both?你怎么能收到相同的 ListeningSocket 呢? That's not possible: you can't have a single socket bound to 2 ports.这是不可能的:您不能将单个套接字绑定到 2 个端口。

It is not possible for a single socket to listen on multiple ports at the same time.单个套接字不可能同时侦听多个端口。 Your WS_SetUpListener() function allocates a new listening socket every time it is called.您的WS_SetUpListener() function 每次调用时都会分配一个新的侦听套接字。 You should NOT be getting the same SOCKET handle multiple times, unless you are closing a previous listening socket before creating a new listening socket.您不应该多次获得相同的 SOCKET 句柄,除非您在创建新的侦听套接字之前关闭先前的侦听套接字。 That would allow the socket API to reuse the previous handle if it wants to.这将允许套接字 API 重用以前的句柄,如果它愿意的话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM