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 7964549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:54:59+00:00 2026-06-04T05:54:59+00:00

Is it possible to receive data with recvfrom on 2 ports? I have a

  • 0

Is it possible to receive data with recvfrom on 2 ports? I have a port which I use for user requests and another port for chat messages. Is it possible to bind two sockets, with different ports and receive data with one recvfrom()?

Regards

EDIT
Does this code work?

int socket_fd,socket_fd2;
struct sockaddr_in addr, chat_addr;

addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");

chat_addr.sin_family = AF_INET;
chat_addr.sin_port = htons(4321);
chat_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

bind(socket_fd2, (struct sockaddr *) &chat_addr, sizeof(struct sockaddr_in));
bind(socket_fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in));

So I want to receive data on two different ports.

  • 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-06-04T05:55:00+00:00Added an answer on June 4, 2026 at 5:55 am

    No, it’s not possible to read from both sockets using a single call to recvfrom, since that function only takes a single socket file descriptor parameter.

    You either need a thread per port, or a mechanism (e.g. select, poll) to tell which sockets have data waiting to be read.

    In the latter case, once you know which sockets have pending data you can then call recvfrom on that specific socket to get the data you need, e.g.:

    // set up select parameters
    fd_set socks;
    FD_ZERO(&socks);
    FD_SET(socket_fd, &socks);
    FD_SET(socket_fd2, &socks);
    
    // find out which sockets are read - NB: it might be both!
    int nsocks = max(socket_fd, socket_fd2) + 1;
    if (select(nsocks, &socks, (fd_set *)0, (fd_set *)0, 0) >= 0) {
         if (FD_ISSET(socket_fd, &socks)) {
              // handle socket 1
              recvfrom(socket_fd, ...);
         }
         if (FD_ISSET(socket_fd2, &socks)) {
              // handle socket 2
              recvfrom(socket_fd2, ...);
         }
    }
    

    NB: this is just a rough and ready sample – real code would have error checking, etc.

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

Sidebar

Related Questions

Is it possible for a receive statement to have multiple timeout clauses, and if
I need to validate as fast as possible and receive the next xml-data on
Is it possible that BroadcastReceiver in an activity which have gone to background receiving
Is it possible to receive event/notification in my app when user uninstall my application
I have multiple app processes that each connect to servers and receive data from
I am developing c# application in which to send and get requests we use
Possible Duplicate: Calling ActionScript 3 function from C# i have a project which is
In MPI, is it possible to immediately throw out received data without allocating a
It's possible to get the source of the location data (for example GPS receiver,
I know its possible to receive a dayClick event on Fullcalendar. But I would

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.