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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:25:42+00:00 2026-05-26T05:25:42+00:00

I am trying to write a small program, that opens a server, creates a

  • 0

I am trying to write a small program, that opens a server, creates a client that connects to this server and receives a message from it.

This is the Code so far

public static void main(String[] args) {
final ServerSocket serverSocket;
try {
    serverSocket = new ServerSocket(12345);
    Thread t = new Thread(){
        public void run(){
                try {
                    Socket server = serverSocket.accept();
                    PrintWriter writer = new PrintWriter(server.getOutputStream(), true);
                    writer.write("Hello World");
                    writer.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };
        t.start();
        Socket client = new Socket("localhost", 12345);
        BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String message = reader.readLine();

        System.out.println("Received " + message);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

}

If i run program it keeps waiting in readLine() – so obviously the client does not receive the message from the server.
Has anyone got an idea why this isn’ working?

  • 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-26T05:25:43+00:00Added an answer on May 26, 2026 at 5:25 am

    Your reading thread is waiting for a newline in the data stream. Just change the server to use:

    writer.write("Hello World\r\n");
    

    and you’ll get the result you were expecting. Alternatively, you can just close the server socket, and then readLine will return when it reaches the end of the data stream.

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

Sidebar

Related Questions

Hey there. I'm trying to write a small program that will read the four
I'm trying to write a program that cleans data, using Matlab. This program takes
I'm trying to write a small program that logs in to the Verizon website
Continuing to learn WCF, I'm trying to write a small program that would with
I'm trying to write a small program that prints out distinct numbers in an
I am trying to write a small java program that will accept a file
I'm trying to write a small piece of code that passes a small formula
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)
I'm trying to write a small app that monitors how much power is left
I'm trying to write a small application that needs to use the clipboard for

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.