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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:48:22+00:00 2026-06-04T11:48:22+00:00

Fail to read/write data to socket when trying to run echo server. No exception

  • 0

Fail to read/write data to socket when trying to run echo server. No exception thrown and there is no response to console input. What’s not correct in code?

public class Server {

    static ServerSocket server;

    public static void main(String[] args) throws IOException  {
        String hostname = "127.0.0.1";

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

        Socket theSocket = null;
        try {
            theSocket = new Socket(hostname, 8888);

            BufferedReader networkIn = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
            BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter out = new PrintWriter(theSocket.getOutputStream(), true);
            System.out.println("Connected to echo server");

            while (true) {
              String theLine = userIn.readLine();
              if (theLine.equals("."))
                break;
              out.println(theLine);
              out.flush();
              System.out.println("networkIn: "+networkIn.readLine());
            }
            networkIn.close();
            out.close();
            System.out.println("out.close();");
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
  • 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-04T11:48:23+00:00Added an answer on June 4, 2026 at 11:48 am

    You created two unrelated sockets, one for accepting connections, and one for connecting to (hostname,8888). You need to call accept() on the server socket to actually get clients connected. See the tutorial.

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

Sidebar

Related Questions

I try to read and write data from/to Socket with NSStream. Here is my
I am trying to read data from a HID device. I have a USB
I'm trying to load data from a .txt file with PHP, but I fail
I'm trying to post data on a server using java HttpUrlConnection class. It seems
I (apparently) manage to make a ftp connection, but fail to read anything from
What causes the FAIL - Deploy Upload Failed, Exception: null error message in tomcat?
I'm trying to write a function, that would use native openssl to do some
There're some library classes that although implement Serializable , fail to serialize correctly. I
How does TCP/IP report errors when packet delivery fails permanently? All Socket.write() APIs I've
I have a requirement to take the test data from Excel sheet and write

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.