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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:27:13+00:00 2026-06-11T03:27:13+00:00

I’m working on Client-Server simulation in Java where Clients (threads) are connecting to server

  • 0

I’m working on Client-Server simulation in Java where Clients (threads) are connecting to server to get some data. After several seconds, one of randomly chosen client (thread) needs to be killed. I close socket it used for communication to server and left him die (by exiting from run() method). Problem is when new created thread is trying to create same socket as previous one used (same addresses and same ports) to connect to server, I’m getting:

java.net.BindException: Address already in use
    at java.net.PlainSocketImpl.socketBind(Native Method)
    at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:374)
    at java.net.Socket.bind(Socket.java:627)
    at java.net.Socket.<init>(Socket.java:423)
    at java.net.Socket.<init>(Socket.java:319)

Code for creating socket:

private void createNewSocket(InetAddress sIP, int sPort, 
        InetAddress cIP, int cPort) {
    try {
        socket = new Socket(sIP, sPort, cIP, cPort);
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("Socket unsuccessfully created");
    }
    try {
        in = new BufferedReader(new InputStreamReader(
                socket.getInputStream()));

        out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
                socket.getOutputStream())), true);

    } catch (IOException e) {
        e.printStackTrace();    
        try {
            socket.close();
        } catch (IOException e2) {
            System.err.println("Socket unsuccessfully closed");
        }
    }
}

public void run() {

    createNewSocket(gprsServerIP, Util.PORT_SERVER_PORT,
            clientIP, sendPort);

    out.println(REQUEST);   
    try {
        serverPort = Integer.parseInt(in.readLine());   
        TCPClient.serverPort[clientID] = serverPort;
        System.out.println("Server port: " + serverPort + '\n' + 
                            "Send port: " + sendPort + '\n');
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            System.err.println("Socket unsuccessfully closed");
        }
    }

    while (true) {      

        if (clientID == TCPClient.selectedID) {

            TCPClient.selectedID = -1;

            createNewSocket(gprsServerIP, Util.PORT_SERVER_PORT, 
                    clientIP, sendPort);

            out.println(FREE_PORT + serverPort);
            try {
                socket.close();
            } catch (IOException e2) {
                System.err.println("Socket unsuccessfully closed");
            }
            //System.out.println(socket.isClosed());
            System.out.println("Port:" + serverPort + " is free");
            TCPClient.id[clientID] = -1;
            break;              
        }                           
    }
    clientCount--;

}
  • 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-11T03:27:15+00:00Added an answer on June 11, 2026 at 3:27 am

    Chances are that the client socket is still either in CLOSE_WAIT or TIME_WAIT status. The operating system is making sure that all data has been delivered down the socket before another application can re-use it. Otherwise, the new client might get garbage duplicate packets left over from the previous connection.

    I would suggest that your clients work with a range of ports instead of a constant. Then they can use the next port in the range and loop around when they reach the end of the range.

    However, if you don’t need to set the client port then you should not set the client port in your code at all by passing in a port of 0 to Socket. In this case the, JDK and the operating system will do the right thing and choose an appropriate free port for you.

    Quoted from wikipedia:

    CLOSE-WAIT:
    The server receives notice from the local application that it is done. The server sends its fin to the client.

    TIME-WAIT:
    Represents waiting for enough time to pass to be sure the remote peer received the acknowledgment of its connection termination request. According to RFC 793 a connection can stay in TIME-WAIT for a maximum of four minutes known as a MSL (maximum segment lifetime).

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

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.