Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4602482
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:59:31+00:00 2026-05-21T23:59:31+00:00

I have an application that is listening on 2 ports, it appears that when

  • 0

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;
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:59:32+00:00Added an answer on May 21, 2026 at 11:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have application that makes different queries with different results so the caching in
I have application that is up more than 3 days. I can see in
I have an Rails application server that is listening on port 9000, and is
I have an SNMP trap application in Java that aims at listening to an
I have an application that creates a thread, and it will be listening for
I have application that is connecting to the DB and if I enter incorrect
I have application that brings response via Ajax and creates 5-20 new jQuery click
I have an application that makes use of/consumes services from a number of external
I have an application that will form a packet and send the packet data
I have an application that takes too long to run, and I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.