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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:19:06+00:00 2026-06-12T19:19:06+00:00

if separately Client —send message to—> Server : works fine! if separately Server —send

  • 0

if separately

Client —send message to—> Server : works fine!

if separately

Server —send message to—> Client : works fine!

But when both together:

Client —send message to—> Server
Server —send message to—>
Client

Nothing work!

Here is the ServerSide:

import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyServer
{  
    private final static int port = 8000; 
    private static String hostname = "";
    private static String hostIP ="";
    public static void main(String[] args )
    {  
        ServerSocket serverSocket=null;
        try {
            // get host information
            hostname = InetAddress.getLocalHost().getHostName();
            hostIP = InetAddress.getLocalHost().getHostAddress();
            // display server information
            System.out.println("MyServer started on "+hostname+" with IP: "+hostIP + " on the port number: " + port);
            serverSocket = new ServerSocket(port);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        while(true)
        {   
            ClientWorker w;
            try
            {   
                w = new ClientWorker(serverSocket.accept());
                Thread t = new Thread(w);
                t.start();
            }
            catch(IOException e)
            {
                e.printStackTrace();
                break;
            }   
        }
    }
}   
class ClientWorker implements Runnable
{
    Socket incoming;
    public ClientWorker(Socket incoming)
    {
        this.incoming = incoming;
    }
    public void run()
    {
        String request = null;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            request = in.readLine();
            System.out.println("request =" + request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(incoming.getOutputStream(),true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println("The command is not readable");

    }

}

Here is ClientSide:

import java.io.*;
import java.net.*;
public class MyClient2
{
    public static void main(String[] args)
    {
        String answer = null;
        if (args.length != 1){
            System.out.println("Usage: MyClient serverHostname");
            System.exit(0);
        }
        try
        {
            Socket socket = new Socket (args[0], 8000);
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
            out.print("TIME");
            System.out.println("Request has been send");
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            answer = in.readLine();         
            System.out.println("The answer is:  "+answer);
            in.close();
            out.close();
            socket.close();
        }
        catch (Exception e)
        {
            System.out.println("Make sure the server is running and try again");
        }
    }
}

Please have a look and I appreciate your comments.
Cheers

  • 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-12T19:19:07+00:00Added an answer on June 12, 2026 at 7:19 pm

    You are using the method readLine which blocks until a newline character is read or the stream is closed. You neither send a newline character or close the stream (until after you expect to have received a response).

    Try changing out.print("TIME"); to out.println("TIME");.

    Also, avoid using PrintWriter as it hides any IOExceptions that occur and you have to manually check instead. Your exception handling isn’t great. When an IOException occurs you should probably be terminating the thread (rather than just blindly continuing) as there isn’t much you can do to recover from the error.

    In addition your ClientWorker does not clean up after itself — ie. close the streams and socket.

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

Sidebar

Related Questions

i have a java chat server & client, that works fine. I made a
I'm writing both client and server code using WCF, where I need to know
I implemented a client and server programms using RMI. I wrote this both server
I want to create a multi-cllient client-server program in which the server has separate
The following query hangs: (although subqueries perfomed separately are fine) I don't know how
I am creating a servlet(proxy server) which handles multiple request from the client.I do
I am using the Tweetstream gem to retrieve statuses from Twitter. This works: Tweetstream::Client.new('user',
I am having problem in java to pass cookie from client to server. In
I) My proxy server passes the Accept-Encoding header that it received from the client
My team has decided to work with Redis via the ServiceStack.net Redis Client as

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.