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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:16:03+00:00 2026-05-11T18:16:03+00:00

I have written a simple Java dispatcher with a daemon thread to handle the

  • 0

I have written a simple Java dispatcher with a daemon thread to handle the incoming traffic, and using another thread to send out commands.

The problem comes when the server receives the first message, then the client/server system gets stuck on where the server trying to send out a response to the client. The sockets on both end just simply freeze when the server sends out data.

I have simplified my original problem into a echo server and client; I guess I must have a very very stupid mistake on the code. The code and result on my machine are reproduced below. Can anyone explain what is going wrong?

Thanks!

Here is the results, we can see the server and client stuck once the first message received.

Echo Server listening port...
Echo Server: Waiting from client connection.
Connecting to the server.
Connected to the server.
Dispatcher send: 10
Dispatcher send: 11
Dispatcher send: 12
Dispatcher send: 13
Dispatcher read...
Dispatcher read...
Dispatcher readed 10

Code:

EchoTest:

import java.io.*;
import java.net.*;

public class EchoTest {
  public static void main(String[] args) {
    EchoServer.listen();
    EchoClient client = new EchoClient();
    EchoServer server = EchoServer.accept();
  }
}

Client and Server:

class EchoClient implements Runnable {
  private static final int PORT = 13244;

  Socket socket;
  Disp disp;
  Thread client;

  public EchoClient() {
    client = new Thread(this);
    client.start();
  }

  public void run() {
    try {
      System.out.println("Connecting to the server.");
      Socket socket = new Socket("localhost", PORT);
      System.out.println("Connected to the server.");
      disp = new Disp(socket);

      disp.send(10);
      disp.send(11);
      disp.send(12);
      disp.send(13);


    } catch(IOException e) {
      System.out.println("Would not connect to local host: " + PORT);
      System.exit(-1);
    }
  }

  public void send(int m) {
    disp.send(m);
    System.out.println("Sent message " + m);

    int echo = disp.getMsg();

    if(m == echo) {
      System.out.println("Message " + m + "sent and received.");
    } else {
      System.out.println("Message " + m + "cannot be echoed correctly.");
    }
  }
}

class EchoServer implements Runnable{
  private static final int PORT = 13244;
  private static ServerSocket serverSocket;

  Disp disp;

  public EchoServer(Socket s) {
    disp = new Disp(s);
  }

  public static void listen() {
    System.out.println("Echo Server listening port...");

    try {
      serverSocket = new ServerSocket(PORT);
    } catch (IOException e) {
      System.out.println("Could not listen on port: " + PORT);
      System.exit(-1);
    }
  }

  public static EchoServer accept(){
    try {
      System.out.println("Echo Server: Waiting from client connection.");
      return new EchoServer(serverSocket.accept());
    } catch(IOException e) {
      System.out.println("Couldn't accept connection from client.");
      System.exit(-1);
    }

    return null;
  }

  public void run() {
    while(true) {
      int m = disp.getMsg();
      disp.send(m);
    }
  }
}

Disp:

class Disp implements Runnable{
  int msg = -1;
  Socket socket;
  BufferedInputStream input;
  DataInputStream dis;
  BufferedOutputStream output;
  DataOutputStream dos;
  Thread daemon;

  public Disp(Socket s) {
    this.socket = s;

    try{
      input = new BufferedInputStream(socket.getInputStream());
      dis = new DataInputStream(input);

      output = new BufferedOutputStream(socket.getOutputStream());
      dos = new DataOutputStream(output);
    }catch(IOException e) {
    }
    daemon = new Thread(this);
    daemon.start();
  }

  public void run() {
    while(true) {
      int m = get();
      setMsg(m);
    }
  }

  public void send(int m) {
    synchronized(dos) {
      try{
        System.out.println("Dispatcher send: " + m);
        dos.writeInt(m);
        dos.flush();
      } catch(IOException e) {
      }
    }
  }

  public int get() {
    System.out.println("Dispatcher read...");
    synchronized(dis) {
      try{
        int m = dis.readInt();
        System.out.println("Dispatcher readed " + m);
        return m;
      } catch(IOException e) {
      }
    }

    return -1;
  }

  synchronized public void setMsg(int m) {
    while(true) {
      if(msg == -1) {
        try {
          wait();
        } catch(InterruptedException e) {
        } 
      } else {
        msg = m;
        notifyAll();
      }
    }
  }

  synchronized public int getMsg() {
    while(true) {
      if(msg != -1) {
        try {
          wait();
        } catch(InterruptedException e) {
        } 
      } else {
        notifyAll();
        return msg;
      }
    }
  }
}
  • 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-05-11T18:16:03+00:00Added an answer on May 11, 2026 at 6:16 pm

    That’s an awful lot of code. First of all I suggest cutting it down.

    Secondly, you never seem to call EchoServer.run, but it’s difficult to see.

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

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The below example will trim off the last char of… May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer viewWillAppear: or viewDidAppear: might work for you. May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer Probably you are missing a semicolon at the end of… May 13, 2026 at 7:33 pm

Related Questions

This is probably a really basic problem but I can't seem to find any
I am very new to web service stuff so please be kind. I have
I've written a fairly simple java application that allows you to drag your mouse
I'm used to scripting languages. PHP, Javascript etc. and I've written a few relatively

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.