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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:12:37+00:00 2026-06-09T20:12:37+00:00

I have a server which is supposed to open a ServerSocket connection to every

  • 0

I have a server which is supposed to open a ServerSocket connection to every client. The problem is, I need the clients to check if the ConnectionPort is in use already.
That means the Clients should check port 12345 and get a result if there is a ServerSocket “waiting” or not, and if not they take the next etc.

So in the end two or three clients are connected to the Server on port 12345, 12346, 12347…

I wrote this:

serverSocket = new ServerSocket(incomingPort, 1); // backlog 1 = Minimum
incomingSocket = serverSocket.accept();
readerIn = new BufferedReader(new InputStreamReader(incomingSocket.getInputStream()));

outgoingSocket = new Socket(incomingSocket.getInetAddress().getHostAddress(), outgoingPort);
dataOutputStream = new BufferedOutputStream(outgoingSocket.getOutputStream());
pwOut = new OutputStreamWriter(outgoingSocket.getOutputStream());

I get the first client connected and open (in the last three lines) another connection “back” to the client. On the client side I wrote almost the same, the other way around:

outgoingSocket = new Socket(serverIP, inPort1);
dataOutputStream = new BufferedOutputStream(outgoingSocket.getOutputStream());
pwOut = new OutputStreamWriter(outgoingSocket.getOutputStream());

serverSocket = new ServerSocket(outPort1);
incomingSocket = serverSocket.accept();
readerIn = new BufferedReader(new InputStreamReader(incomingSocket.getInputStream()));

The next client that tries to open a connection is doing the same, starting with the same port. I was hoping to get an Exception like a ConnectException or so, but nothing. I guess it is related to the backlog “buffer”. It continues with everything and waits to become a ServerSocket but it waits forever. The server itself is already occupied with the first connection.

I read many posts but they all say that outgoingSocket.isConnected() is giving true and .isClosed() is giving false, at least until the timeout. Is there any way of determing that the server is already occupied? Or at least to “disable” the backlog so all incoming connections are refused?

  • 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-09T20:12:38+00:00Added an answer on June 9, 2026 at 8:12 pm

    I am not sure if you understand concept of client-server communication and socket interface. You don’t need to create a new socket to communicate from server to client, TCP connections are bidirectional and you can write to the same socket that you read from.

    Please refer to the official documentation for the good EXAMPLE of client-server, or follow the full TUTORIAL.

    If you still want to proceed with your approach, where the server accepts only a single connection, you need to close serverSocket in server after accepting the connection. Communication will still be possible using your incomingSocket class.

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

Sidebar

Related Questions

I have a server class which connects a client on a specific server socket
I have an server side object which I am posting to the client and
I have a server which has its own cert and a client who is
In our application we have a server which will connect to multiple clients. For
Suppose I have a event handler which makes two AJAX calls to the server:
I have a server which already host ASP.Net website. I am migrating from blogger
I have a server which executes Python scripts from a certain directory path. Incidently
Hi I have a server which has several virtual hosts set up on it.
i have a c++ server which is using boost::asio to do read/write operations -
I have a web server which I do not know what the time zone

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.