简体   繁体   中英

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?

I call the function as follows:

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.

You would use the select() routine and the fdset and fdisset macros.

Differentiating Between Sockets using Select

If you're using recvfrom() to read the packet data you can extract the socket info, which will include port information. How are you reading the packets in?

EDIT: actually, wait a sec. How can you receive the same ListeningSocket for both? That's not possible: you can't have a single socket bound to 2 ports.

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. 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. That would allow the socket API to reuse the previous handle if it wants to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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