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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:20:33+00:00 2026-06-06T09:20:33+00:00

I want to write a server which listens on the given port for connections

  • 0

I want to write a server which listens on the given port for connections and puts sockets into BlockingLinkedQueue from which the consumer thread will read messages. I accept incoming connections in this way:

   ServerSocket serverSocket = new ServerSocket(port);
   while (true)
   {
        Socket socket = null;
        socket = serverSocket.accept();
        queue.put(socket);
   }

When I try to connect in parallel from two separate hosts it occurs that responses to the first one are sent to the second one after establishing the second connection. When I change my code to this listed below the second connection is merely refused:

   while (true)
   {
        ServerSocket serverSocket = new ServerSocket(port);
        Socket socket = serverSocket.accept();
        queue.put(socket);
   }

My questions are:

  1. What’s the difference between this two situations? Why in the first situation messages are sent to the second host?
  2. How should I refactor my code in order to create separate connections between my server and both hosts and handle them in parallel?
  • 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-06T09:20:34+00:00Added an answer on June 6, 2026 at 9:20 am

    What’s the difference between this two situations?

    In the first case, you are using the same port for multiple connections. In the second case, you are discarding the server port so any waiting connections for be refused.

    Why in the first situation messages are sent to the second host?

    Due to a bug in code, not shown here.

    How should I refactor my code in order to create separate connections between my server and both hosts and handle them in parallel?

    Create a thread for each connection.

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

Sidebar

Related Questions

I need to write a server which accepts connections from multiple client machines, maintains
I have a socket server that listens and accepts connections from client, which works
I get a bytearray from the server which I want to write to my
I wrote a (not-so-)simple websocket server in python, which listens in a port, performs
I want to write a web server which stores its state in a State
In ubuntu 12.04, I want to write a concurrent server, which can receive http
I want to write a SQL Server 2005 stored procedure which will select and
I have written a java socket server program which listens to a port continuously.
I want to write a User-Defined function in SQL SERVER which will take a
I want the logs from the Selenium Server Console write to file. I know

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.