title: server make a txt file and client write it
body: <p>I have a project that has a server which creates an empty text file.</p>
  
  <p>Once my client stops writing to this file, the server should read and display the results.</p>
  
  <p>My problem is that the client is connecting to the server, but whatever text the client is sending is not being written on the server side. In addition, when I exit, the server doesn't quit.</p>
  
  <p>This is what I have so far:</p>
  
  <p>The Server</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.DataInputStream;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.PrintStream;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.Scanner;
  
  public class Server {
  
      public static void main(String args[]) throws IOException {
           BufferedWriter out;// = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));
  
           ServerSocket echoServer = null;
              String line;
              DataInputStream is;
              PrintStream os;
              Socket clientSocket = null;
  
      // Try to open a server socket on port 9999
             try {
                 echoServer = new ServerSocket(55);
              }
              catch (IOException e) {
                 System.out.println(e);
              }   
      // Create a socket object from the ServerSocket to listen and accept 
      // connections.
      // Open input and output streams
  
      try {
                 clientSocket = echoServer.accept();
                 is = new DataInputStream(clientSocket.getInputStream());
                 os = new PrintStream(clientSocket.getOutputStream());
                 out = new BufferedWriter(new FileWriter("C://Users//Vagos//Desktop//file.txt"));
      // As long as we receive data, echo that data back to the client.
  
                 while (true) {
                   line = is.readUTF();
                   os.println(line); 
                   os.flush();
                   out.write(line);
                   out.flush();
                 }
              }   
      catch (IOException e) {
                 System.out.println(e);
              }
          }
      }
  </code></pre>
  
  <p>Here is my client:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.io.*;
  import java.net.InetAddress;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Scanner;
  
  public class client {
  
      public static void main(String[] args) {
          Socket smtpSocket = null;  
          DataOutputStream os = null;
          DataInputStream is = null;
          String strout;
          Scanner in = new Scanner(System.in);
          try {
              smtpSocket = new Socket("localhost", 55);
              os = new DataOutputStream(smtpSocket.getOutputStream());
              is = new DataInputStream(smtpSocket.getInputStream());
          } catch (UnknownHostException e) {
              System.err.println("Don't know about host: hostname");
          } catch (IOException e) {
              System.err.println("Couldn't get I/O for the connection to: hostname");
          }
  
      if (smtpSocket != null &amp;&amp; os != null &amp;&amp; is != null) {
              try {
                  do{
                      System.out.print("Write what the client will send: ");
  
                  strout = in.nextLine();
                  os.writeBytes(strout);}
                  while(!strout.equals("exit"));
                  os.close();
                  is.close();
                  smtpSocket.close();   
              } catch (UnknownHostException e) {
                  System.err.println("Trying to connect to unknown host: " + e);
              } catch (IOException e) {
                  System.err.println("IOException:  " + e);
              }
          }
      }           
  };
  </code></pre>