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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:15:08+00:00 2026-06-17T02:15:08+00:00

I am creating a simple client server application in which there is a GUI

  • 0

I am creating a simple client server application in which there is a GUI client where the user can enter some text and the server will send the text back along with the time stamp.
enter image description here

The problem is that whenever I click on the Echo button, I get a Connection Reset error message. I have no idea why that is happening.
Here is the code:

Server

package echo;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;

public class Server extends Thread{

    final int PORT = 444;
    ServerSocket serverSocket;
    Socket socket;
    InputStreamReader ir;
    BufferedReader b;
    PrintStream p;
    Date currentTime;
    Format fmt;

//------------------------------------------------------------------------------    
    public static void main(String[] args) {
        Server s = new Server();
        s.start();
    }
//------------------------------------------------------------------------------
    public void setupConnection(){
        try{
            serverSocket = new ServerSocket(PORT);
            socket = serverSocket.accept();

            ir = new InputStreamReader(socket.getInputStream());
            b = new BufferedReader(ir);

            p = new PrintStream(socket.getOutputStream());
            fmt = DateFormat.getDateTimeInstance();

        }catch(Exception e){
            e.printStackTrace();
        }
    }
//------------------------------------------------------------------------------

    public Server(){


    }

//------------------------------------------------------------------------------
    @Override
    public void run(){
        setupConnection();
        if(socket!=null){
            try {
                String message = b.readLine();
                if(message!=null){
                    p.println(fmt.format(new Date()) + " " + message);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

Client

package echo;
import java.net.*;
import java.io.*;

import javax.swing.*;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.*;

public class Client extends JFrame{

    final int PORT = 444;
    Socket s;

    InputStreamReader ir;
    BufferedReader b;
    PrintStream p;

    JTextArea textArea;
    JTextField field;
    JScrollPane pane;
    JButton echo;


//------------------------------------------------------------------------------
    public static void main(String[] args) {
        new Client();
    }
//------------------------------------------------------------------------------
    public Client(){
        setupConnection();
        setupGUI();
        addListeners();
    }
//------------------------------------------------------------------------------

    public void setupConnection(){
        try {
            s = new Socket("localhost",PORT);
            ir = new InputStreamReader(s.getInputStream());
            b = new BufferedReader(ir);
            p = new PrintStream(s.getOutputStream());

            p.println("User Logged In");

        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

//------------------------------------------------------------------------------    
    public void setupGUI(){
        setLayout(new GridBagLayout());
        textArea = new JTextArea(30,30);
        field = new JTextField(10);
        pane = new JScrollPane(textArea);
        echo = new JButton("Echo");

        GridBagConstraints gbc = new GridBagConstraints();
        textArea.setBorder(BorderFactory.createTitledBorder("Replies from server: "));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 5;
        gbc.gridheight = 5;
        add(pane,gbc);

        gbc.gridy = 5;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(field,gbc);

        field.setBorder(BorderFactory.createTitledBorder("Enter text here:"));
        gbc.gridy = 6;
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        add(echo,gbc);

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
//------------------------------------------------------------------------------
    public void addListeners(){
        echo.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e){
                String message = field.getText();
                field.setText("");
                p.println(message);
                try {
                    String reply = b.readLine();
                    if(reply!=null){
                        textArea.append(reply);
                    }
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                System.out.println();
            }
        });
    }
//------------------------------------------------------------------------------
}

Can you please help me solve that problem?

  • 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-17T02:15:09+00:00Added an answer on June 17, 2026 at 2:15 am

    Inside the server run () you need to have a while loop, which breaks only after your client says “close this connection”. What is happening now is that your server is waiting for the data, client receives the data and exits (readline).

    The exception is correct, if you think of it :).

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

Sidebar

Related Questions

I'm creating simple Socket server. Flex application will be client for this server. On
I am creating my first, very simple RMI client-server application. Here is the code:
I am creating a simple HTTP client/server application on my local machine but I
i am creating simple call filter application which restrict unwanted calls. i use following
Was creating a simple console application to do some prototyping and was shocked to
I'm new to socket programming and programming a Java UDP simple client-server application. I'm
When creating a client server application (serversocket), would I create 2 seperate projects or
I have a server\client application which needs to request two different remote proxies via
I'm creating a simple online multiplayer game, with which two players (clients) can play
I am creating simple midlet for Bluetooth communication with server but I can't get

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.