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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:25:07+00:00 2026-05-30T18:25:07+00:00

I got here client and server side programs. Client talks to server by sending

  • 0

I got here client and server side programs. Client talks to server by sending a string, the server then converts the string into capital letters and sends back. The problem is that the client does not receive any string from the server. Only the server prints 2 passed in strings, then the server throws IOException. I guess that because client closed connection. But why does client does not receive any message from server? How to overcome this issue?
Thanks

Client:
package solutions;

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

class SocketExampleClient {

    public static void main(String [] args) throws Exception {

    String host = "localhost"; // hostname of server
    int port = 5678;           // port of server
    Socket s = new Socket(host, port);
    DataOutputStream dos = new DataOutputStream(s.getOutputStream());
    DataInputStream dis = new DataInputStream(s.getInputStream());

    dos.writeUTF("Hello World!");
    System.out.println(dis.readUTF());

    dos.writeUTF("Happy new year!");
    System.out.println(dis.readUTF());

    dos.writeUTF("What's the problem?!");
    System.out.println(dis.readUTF());

    }
}

Server:

package solutions;

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

class SocketExampleServer {

    public static void main(String [] args) throws Exception {

    int port = 5678;
    ServerSocket ss = new ServerSocket(port);
    System.out.println("Waiting incoming connection...");

    Socket s = ss.accept();
    DataInputStream dis = new DataInputStream(s.getInputStream());
    DataOutputStream dos = new DataOutputStream(s.getOutputStream());

    String x = null;

    try {
        while ((x = dis.readUTF()) != null) {

        System.out.println(x);

        dos.writeUTF(x.toUpperCase());
        }
    }
    catch(IOException e) {
        System.err.println("Client closed its connection.");
    }
    }
}

Output:

Waiting incoming connection...
Hello World!
Happy new year!
What's the problem?!
Client closed its connection.
  • 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-30T18:25:09+00:00Added an answer on May 30, 2026 at 6:25 pm

    Your main program is exiting before it has a chance to read the response from the server. If you add the following code it will work fine. 🙂 UPDATE- I just realised your code is working fine on my computer – and it does output the string as expected. DataInputStream.readUTF() is blocking correctly and receiving the response. Are you still having the problem?

    Thread t = new Thread(){
    public void run()
    {
        for(;;)
        {
            String s = null;
        try 
            {
            s = dis.readUTF();
        } 
            catch (IOException e) 
            {
            e.printStackTrace();
            }
            while(s!=null)
            {
                System.out.println("Output: " + s);
            try 
            {
            s = dis.readUTF();
        } 
            catch (IOException e) 
            {
            e.printStackTrace();
        }
        }}}};
       t.start();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the quick background: I've got a client and a server program that are
Ok here's the deal I got one table with a bunch of client information.
Here is the situation. There are server and client in network. They communicate like
I tried to populate google chart datatable in server side using PHP.I got JSON
I have got a Client/Server Application that using Asynchronous Socket.My problem is i cant
I've got a simple Flash client connecting to a Flash Media Server, and I'd
on the server side, I've got these: ASPX: <form id=form1 runat=server enctype=multipart/form-data> <input type=file
I've got a huge problem here. I've managed to add javascript to my server
Here's my scenario: I've got a VisualSVN server on my main dev box @
Yea i know i'm way behind times but what i've got here is a

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.