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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:01:41+00:00 2026-06-01T15:01:41+00:00

Here is the server code package echoserver; import java.net.*; import java.io.*; public class EchoServer

  • 0

Here is the server code

package echoserver;

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

public class EchoServer {

    public static void main(String[] args) {
        try {

            //establish server socket
            ServerSocket s = new ServerSocket(1981);

            //Thread client connectionsincoming
            while (true) {
                //wait for incoming connection
                Socket incoming = s.accept();
                Runnable r = new ThreadedEchoHandler(incoming);
                Thread t = new Thread(r);
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


package echoserver;

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

class ThreadedEchoHandler implements Runnable {

    public ThreadedEchoHandler(Socket i) {
        //initializing socket
        incoming = i;
    }

    public void run() {
        try {
            try {
                //recieve input stream from socket
                InputStream inStream = incoming.getInputStream();

                //recieve output stream from socket
                OutputStream outStream = incoming.getOutputStream();

                //Create a scanner from input stream
                Scanner scan = new Scanner(inStream);

                //Create printer writer from output stream and enabled auto flushing
                PrintWriter out = new PrintWriter(outStream, true);

                //prompt users on how to exit program soon as a long in into the server
                out.println("Enter BYE to exit");

                boolean done = false;

                //while done is not true and scanner has next line loop
                while (!done && scan.hasNextLine()) {

                    //reading text that came in from the socket
                    String line = scan.nextLine();

                    //On the server print the ip address of where the text is coming from and the text they typed
                    System.out.println("Recieved from " + incoming.getInetAddress().getHostAddress() + ": " + line);

                    //Echo back the text the client typed to the client
                    out.println("Echo: " + line);

                    //if they type BYE in caps terminate there connection and I also trimmed whitespaces
                    if (line.trim().equals("BYE")) {
                        done = true;
                    }
                }
            } //finally close the socket connection
            finally {
                incoming.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private Socket incoming;
}

and here is the code for client

package client;

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

public class Client {

    public static void main(String[] args) throws IOException {
        PrintWriter out = null;
        try {
            Socket s = new Socket(InetAddress.getLocalHost(), 1981);
            System.out.println("Connected to server on port 1981");
            out = new PrintWriter(s.getOutputStream());

            out.println("Hello");
            s.close();

        } catch (Exception ex) {
            System.err.println(ex.getMessage());
        }
    }
}

Socktes are getting created successfully but when control goes to t.start() method call it is not calling run() method of ThreadedEchoHandler class.

Why is this happening? any idea?

  • 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-01T15:01:43+00:00Added an answer on June 1, 2026 at 3:01 pm

    The client writes "Hello" to the PrintWriter. So far, so good.

    You may expect that the PrintWriter sends this text directly to the socket, but it doesn’t. The documentation from the PrintWriter(OutputStream) constructor says that it creates a PrintWriter without automatic line flushing. This means that you have to call out.flush() whenever you want something to be actually sent.

    Until you call out.flush() the text only exists in some internal buffer, and the server will not be able to see it.

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

Sidebar

Related Questions

I make a java socket server and c socket client, here are the code
//Here is the code to request the server send the public key to client
enter code here I have a table on SQL server 2005 with bigint primary
I have some troubles with receiving data on server side. Here goes the code:
Here's some code: OdbcConnection connection = new OdbcConnection(@DRIVER={MySQL ODBC 5.1 Driver};SERVER= + ip +
I'm creating an install package using InnoSetup and installing SQL Server 2005 Express. Here's
I am creating my first, very simple RMI client-server application. Here is the code:
I have server and client code that works perfectly as a Java application. The
When I am running this code inside of a module that inherits from Net::Server
Let say we got an android client and a java api server and code

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.