title: problems with sending String via socket in java
body: <p>hi everyone i have problems with sending 2 strings via socket in java.
  i have UI that get Username and password from client and send it to server . my problem make 2 part :1-i can not get the string username in my server 2- when the client send the username the socket is close before send the password here is my code please help me .
  the main purpose is getting 2 string username and password from UI (client)and send them by socket to server.</p>
  
  <p>Server:</p>
  
  <pre><code>package finalproject;
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  public class Server 
  {
  ServerSocket listener;
  Socket socket;
  OutputStream out ;
  InputStream in;;
  InputStreamReader reader;
  OutputStreamWriter writer;
  String massage;
  
  public void connectserver() throws IOException
  {
      listener = new ServerSocket(9097);
      System.out.println("Server is running on port 9097 ...");
  }
  public void waitforclient() throws IOException
  {
      socket = listener.accept();
  System.out.println("A new client connected to the server");
  }
  public void startstreamsserver() throws IOException
  {
      in = socket.getInputStream();
      out = socket.getOutputStream();
  writer = new OutputStreamWriter(out);
      reader = new InputStreamReader(in);
      System.out.println("Server streams are ready");
  }
  public void closestreamsserver() throws IOException
  {
      writer.close();
      reader.close();
  }
  public void getinfoserver() throws IOException
  {
      try  
      {
          reader.read(); 
           System.out.println(reader);
           System.out.println("input is : " + reader.toString());      
      }
      catch(IOException IOE)
      {
              IOE.printStackTrace();//if there is an error, print it out
      }      
  }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>package finalproject;
  
  import java.awt.BorderLayout;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.io.EOFException;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.swing.BoxLayout;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  
  public class Client
  {
  Socket socket;
  OutputStream out1;
  InputStream in1;
  OutputStreamWriter writer;
  InputStreamReader reader;
  String massage;
  JFrame frame;
  BorderLayout blayout;
  JButton center;
  JButton south;
  FlowLayout fLoyout;
  JLabel jb1;
  JTextField name;
  JLabel jb2;
  JTextField pass;
  JLabel jb7;
  JPanel cpanel;
  JPanel spanel;
  
  public void connectclient() throws IOException
  {
      socket = new Socket("localhost", 9097);
      System.out.println("connect to server on port 9097");
  }
  public void startstreamsclient() throws IOException
  {
      in1 = socket.getInputStream();
      out1 = socket.getOutputStream();
  writer = new OutputStreamWriter(out1);
      reader = new InputStreamReader(in1);
      System.out.println("Client streams are ready");
  }     
  public void closestreamsclient() throws IOException
  {
      writer.close();
      reader.close();
  }
  public void loginformclient()
  {
    frame = new JFrame();
    frame.setVisible(true);
    frame.setSize(500, 600);
    blayout = new BorderLayout();
    center = new JButton();
    south = new JButton();
    frame.setLayout(blayout);
    fLoyout = new FlowLayout(FlowLayout.CENTER);
    center.setLayout(fLoyout);
    south.setLayout(fLoyout);
    jb1 = new JLabel("Username :");
    name = new JTextField(20);
    center.add(jb1);
    center.add(name);
    jb2 = new JLabel("Password :");
    pass = new JTextField(30);
    center.add(jb2);
    center.add(pass);
  
    jb7 = new JLabel("Save");
    south.add(jb7);
  
    cpanel = new JPanel();
    cpanel.add(center);
    spanel = new JPanel();
    south.addActionListener((ActionEvent ae) -&gt; {
        try 
        {
            writer.write(name.getText());
            writer.flush();
            writer.write(pass.getText());
            writer.flush();
            writer.close();
        } 
        catch (IOException ex)
        {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    });
    spanel.add(south);
    cpanel.setLayout(new BoxLayout(cpanel, BoxLayout.Y_AXIS));
    frame.add(cpanel, BorderLayout.CENTER);
    frame.add(spanel, BorderLayout.SOUTH);
  
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  
  }
  </code></pre>
  
  <p>and my main class :</p>
  
  <pre><code>package finalproject;
  
  import java.io.IOException;
  import javax.swing.JFrame;
  
  public class Finalproject 
  {
  public static void main(String[] args) throws IOException
  {
      //build Server &amp; Client
      Server server = new Server();
      Client client = new Client();
  
      //Start Server &amp; Client
      server.connectserver();
      client.connectclient();
  
      //Server wait for new connection
      server.waitforclient();
  
      //start the Streams
      client.startstreamsclient();
      server.startstreamsserver();
  
      //Client send login information to Server
      client.loginformclient();
  
      //Server get information 
      server.getinfoserver();
  
  }
  
  }
  </code></pre>
  
  <p>but the my inputs in my server are:</p>
  
  <pre><code>  java.io.InputStreamReader@171fc7e
    input is : java.io.InputStreamReader@171fc7e   
  </code></pre>