title: Communication between client and server with applet
body: <p>i'm relatively new to client-server programming, i'm trying to get simple communication between the client and server on an applet gui, it is working now but i'm getting an exception when I run the client</p>
  
  <p>MODIFIED CODE:</p>
  
  <p>Client:</p>
  
  <pre><code>import java.awt.*;
  import java.net.*;
  import java.io.*;
  
  import javax.swing.*;
  
  
  public class Client extends JApplet{
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private JTextArea displayArea;
      private Socket socket = null;
      private DataInputStream input;
      private DataOutputStream output;
  
  
      public void init()
      {
  
          Container container = getContentPane();
  
          // set up JTextArea to display
          displayArea = new JTextArea();
          displayArea.setEditable(false);
          container.add(new JScrollPane(displayArea), BorderLayout.SOUTH );
  
          try 
          {
              // make connection
              socket = new Socket(getCodeBase().getHost(), 5423);
  
              // get streams
              input = new DataInputStream( socket.getInputStream() );
              output = new DataOutputStream( socket.getOutputStream() ); 
  
  
              while(true) 
              {
                  output.writeUTF("hello from client"); //send message to server
                  output.flush(); 
  
                  processMessage(input.readUTF()); //process data from server    
              }
          }
          // catch problems setting up connection and streams
          catch ( IOException ioException ) {
              ioException.printStackTrace(); 
          }
  
          try 
          {
              input.close();
              output.close();
          } 
          catch (IOException e) 
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  
  
      // process message received by client
      private void processMessage(String message)
      {
          if (message.equals("hello") ) 
          {
              displayMessage("Connected to server");
          }
      }
  
  
      private void displayMessage(final String messageToDisplay)
      {
  
          SwingUtilities.invokeLater(
                  new Runnable() {  
  
                      public void run() 
                      {
                          displayArea.append(messageToDisplay);
                          displayArea.setCaretPosition(displayArea.getText().length());
                      }
                  } 
              ); 
      }
  
  }
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>import java.awt.*;
  import java.net.*;
  import java.io.*;
  
  import javax.swing.*;
  
  
  public class Server extends JFrame{
  
      /**
       * 
       */
      private static final long serialVersionUID = 1L;
      private ServerSocket serverSocket;
      private JTextArea outputArea;
      private Socket socket = null;
      private DataInputStream input;
      private DataOutputStream output;
  
  
      public Server()
      {
  
          super("Server");
  
          try
          {
              serverSocket = new ServerSocket(5423, 1);
          }
          catch(IOException ioException)
          {
              ioException.printStackTrace();
              System.exit(1);
          }
  
  
          outputArea = new JTextArea();
          getContentPane().add(outputArea, BorderLayout.CENTER);
          outputArea.setText("Server awaiting connections\n");
  
  
          setSize(300, 300);
          setVisible(true);
      }
  
  
      private void doThis() throws IOException
      {
          try
          {
              while(true)
              {
  
                  socket = serverSocket.accept();
  
                  input = new DataInputStream(socket.getInputStream());
                  output = new DataOutputStream(socket.getOutputStream());
  
                  output.writeUTF("hello");
                  outputArea.append(input.readUTF()); //receive data from client
                  output.flush();     
              }
          }
          catch(IOException e)
          {
              e.printStackTrace();
          }
          finally
          {
              output.close();
              input.close();
              socket.close(); //close connection to client
          }
      }
  
  
      public static void main(String[] args) throws IOException 
      {
          Server server = new Server();   
          server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          server.doThis();    
      }
  }
  </code></pre>
  
  <p>Exception reads:</p>
  
  <blockquote>
    <p>java.net.SocketException: Connection reset    at
    java.net.SocketInputStream.read(Unknown Source)   at
    java.net.SocketInputStream.read(Unknown Source)   at
    java.net.SocketInputStream.read(Unknown Source)   at
    java.io.DataInputStream.readUnsignedShort(Unknown Source)     at
    java.io.DataInputStream.readUTF(Unknown Source)   at
    java.io.DataInputStream.readUTF(Unknown Source)   at
    Client.init(Client.java:45)   at sun.applet.AppletPanel.run(Unknown
    Source)   at java.lang.Thread.run(Unknown Source)</p>
  </blockquote>