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

  • Home
  • SEARCH
  • 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 8681161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:26:07+00:00 2026-06-12T21:26:07+00:00

I have a client and a server. The client binds a socket on a

  • 0

I have a client and a server. The client binds a socket on a specific port, the server sends back a new port to the client and the client should bind a new socket on the new port number.

From the main server thread, I start a thread that sends a message to the client once the server is ready and is listening to the new port, so that the client can attempt to connect to the new port. The pipe from the child thread is not sending the message to the client.
So both client and server just freeze, it seems like a deadlock, but im not sure. This line of code in the client: System.out.println(“FROM SERVER: ” + inMsg_rport); is not executing.

Server Code:

class server
            {
                public static void main(String argv[]) throws Exception
                {
                    String newPort;
                    ServerSocket serverSocket = null;
                    Socket clientSocket = null;
                    try
                    {
                        serverSocket = new ServerSocket(5555);
                        clientSocket = serverSocket.accept();

                        DataOutputStream serverOut =
                        new DataOutputStream(clientSocket.getOutputStream());
                        int r_port = 5556;
                        Thread appThread = new Thread(new serverApp(serverOut, r_port));
                        appThread.start();
                    }
                    catch(IOException e)
                    {
                        System.out.println(e);
                    }
                }

                static class serverApp implements Runnable
                {
                    DataOutputStream serverOut;
                    int nPort;
                    public serverApp(DataOutputStream servO, int r_port)
                    {
                        this.serverOut = servO;
                        this.nPort = r_port;
                    }
                    @Override
                    public void run()
                    {
                        ServerSocket serverSocket = null;
                        Socket clientSocket = null;

                        try
                        {
                            serverSocket = new ServerSocket(nPort);
                            serverOut.writeBytes(sr_port);
                            clientSocket = serverSocket.accept();
                        }
                        catch(IOException e)
                        {
                            System.out.println(e);
                        }
                    }
                }
            }

Client code:

            class client {
                public static void main(String argv[]) throws Exception
                {
                    String serverIp = argv[0]; 
                    String msg = argv[2];
                    int port = Integer.parseInt(argv[1]);

                    Socket clientSocket = new Socket(InetAddress.getByName(serverIp), port);

                    BufferedReader clientIn =
                    new BufferedReader(new
                    InputStreamReader(clientSocket.getInputStream()));

                    String inMsg_rport = clientIn.readLine();
                    System.out.println("FROM SERVER: " + inMsg_rport);
                    int r_port = Integer.parseInt(inMsg_rport);
                    clientSocket.close();
                    System.out.println("Closed connection");

                   Socket new_clientSocket = new Socket(InetAddress.getByName(serverIp), r_port);

                }
            }
  • 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-12T21:26:08+00:00Added an answer on June 12, 2026 at 9:26 pm

    readLine() in your client is a blocking call, waiting for an end-of-line character

    http://docs.oracle.com/javase/6/docs/api/java/io/BufferedReader.html#readLine()

    You aren’t sending an end of line character. You’re using a DataOutputStream in your server and sending raw bytes.

    Don’t use a DataOutputStream in your server; I don’t think that’s really what you’re looking for. Just send the port number as text with an end of line character and be done with it.

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

Sidebar

Related Questions

I have a socket server that listens and accepts connections from client, which works
I have a small program that sends and receives data from server and client
I have a client server situation, where the client sends the data (a movie
I have a UDP server which binds to a well-known port, adds itself to
I have a server and a client app, my server listens on port 10015
For a TCP server with following structure: main(){ socket(); bind(); listen(); while(1){ accept(); fork();
I have a single client that is trying to connect to my main server
I have client/server applications and a very simple protocol for communication. More precisely, it's
We have client server based app which saves user related data into a zip
I have Client-Server environment and developed a project for Client-Server. I need to share

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.