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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:59:13+00:00 2026-06-10T12:59:13+00:00

I’m working on a project which expects a TCP client and Server, where server

  • 0

I’m working on a project which expects a TCP client and Server, where server echoes the message back to client. Following is from the assignment:

The server application shall:

  1. Listen for TCP connections on a well known IP address and port
  2. Accept connections initiated on that port
  3. Receive messages from the client and echo them back
  4. Continue to do this until the client drops the connection.

The client application shall:

  1. Establish a connection with the server at its well known IP address and port
  2. Send messages in an asynchronous manner to the server. The format of the message is
    of your choice; however, it must contain enough information in order for it to be
    recognized on its return from the server.

I have completed the coding for Server, and this is what i’ve come up with for the client.

My questions:

  • What does it mean that Server listens for TCP connections on a well known IP and Port In my implementation, i’ve used ServerSocket which accepts the port server listens on. Did i interpret it correctly?

  • In my current implementation of TCPClient, client sends messages to Server, but the println() seems to be a blocking call, which makes it Synchronous. What can i do to make my client asynchronous?

For brevity, I havent added the code of TCPServer, let me know if it is needed

UPDATE**
Based on the feedback, i have modified by TCPClient class. After receiving client request, i spawn two threads ReceiveMessage and SendMessage. Doing that gives me following exception:

[Client] Message sent: Message from Client 97
[Client] Message sent: Message from Client 98
[Client] Message sent: Message from Client 99
[Client] Done Sending all the messages
java.net.SocketException: Socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(SocketInputStream.java:129)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at org.chanders.client.ReceiveMessage.run(ReceiveMessage.java:18)
    at java.lang.Thread.run(Thread.java:680)

Following is the new Client Code:

public class TCPClient {
    Socket clientSocket = null;
    OutputStream out = null;
    BufferedReader in = null;
    String message = "Hello from Client";
    int messagecount = 100;

    // server credentials
    private static final String SERVER_ADDRESS = "localhost";
    private static final int SERVER_PORT = 50001;

    protected void execute() {
        try {
            clientSocket = new Socket(SERVER_ADDRESS, SERVER_PORT);
            Thread send = new Thread(new SendMessage(clientSocket.getOutputStream()));
            Thread receive = new Thread(new ReceiveMessage(clientSocket.getInputStream()));

            send.start();
            receive.start();

            //For server to wait until send and receive threads finish
            send.join();
            receive.join();

        } catch (UnknownHostException uhe) {
            System.err.println("Couldnt find host:  "  + SERVER_ADDRESS);
            uhe.printStackTrace();
            System.exit(-1);

        }catch(IOException ioe) {
            System.err.println("Couldnt get I/O:  "  + SERVER_ADDRESS);
            ioe.printStackTrace();
            System.exit(-1);

        }catch(InterruptedException ie) {
            System.err.println("Thread.join failed:  ");
            ie.printStackTrace();
            System.exit(-1);
        }
        finally {
            //cleanup();
        }
    }

    private void cleanup() {
        try {
            clientSocket.close();
        }catch(Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }

    public static void main(String[] args) {
        TCPClient client = new TCPClient();
        client.execute();
    }

public class SendMessage implements Runnable {
    OutputStream out = null;
    String message = "Message from Client";
    int messageCount = 100;

    public SendMessage(OutputStream out) {
        this.out = out;
    }

    public void run() {
        PrintWriter writer = new PrintWriter(out);
        try {

            for (int i = 0; i < messageCount; i++) {
                String m = message + " " + i;
                writer.println(m);
                System.out.println("[Client] Message sent: " + m);
            }
            System.out.println("[Client] Done Sending all the messages");

        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        } finally {
            cleanup();
        }

    }
    private void cleanup() {
        try {
            out.close();
        }catch(Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}

public class ReceiveMessage implements Runnable {
    InputStream in = null;
    String message;

    public ReceiveMessage(InputStream in) {
        this.in = in;
    }

    public void run() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        try {

            while ((message = reader.readLine()) != null) {

                System.out.println("[Client] Received message from Server: "
                        + message);
            }

            System.out.println("[Client] Done Receiving messages from Server");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cleanup();
        }

    }
    private void cleanup() {
        try {
            in.close();
        }catch(Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }
    }
}
  • 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-10T12:59:15+00:00Added an answer on June 10, 2026 at 12:59 pm

    Use a separate thread for each client. When you write something, in the server end , there must be a method which accepts the string. Otherwise it will be blocking. Paste your server code.

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

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.