An SO User
An SO User

Reputation: 25018

Creating a client-server application to echo what the user sends

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?

Upvotes: 2

Views: 2054

Answers (1)

taxeeta
taxeeta

Reputation: 1198

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 :).

Upvotes: 1

Related Questions