title: Java TCP Server read file name
body: <p>I want to create small client-server TCP file transfer program. And I have problem with one thing. When I send file from Client to Server, for example a txt file: omg.txt, I want the Server to read the incoming file name. </p>
  
  <p>So - Client send omg.txt, Server says "New file recived: omg.txt". I tried to use <code>BufferedReader</code> (Server) and <code>DataOutputStream</code> (Client, because you have to write name of the file to send it) but it didnt work. </p>
  
  <p>EDIT:</p>
  
  <p>Client:</p>
  
  <pre><code>Socket sock = new Socket("localhost",13267);
  System.out.println("Wait...");
  Scanner input = new Scanner(System.in);
  
  while(true){
  // wysylanie
  System.out.println("Filename");
  String p = input.nextLine();
  File myFile = new File (p);
  boolean exists = (new File(p)).exists();
  if (exists) {
          byte [] mybytearray  = new byte [(int)myFile.length()];
          FileInputStream fis = new FileInputStream(myFile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          BufferedOutputStream out = new BufferedOutputStream(sock.getOutputStream());
          bis.read(mybytearray,0,mybytearray.length);
          OutputStream os = sock.getOutputStream();
          System.out.println("Wait...");
          os.write(mybytearray,0,mybytearray.length);
          os.flush();
          System.out.println("Done");
          sock.close();
      } 
  </code></pre>
  
  <p>Server:</p>
  
  <pre><code>int filesize=6022386;
  int bytesRead;
  int current = 0;
  
  ServerSocket servsock = new ServerSocket(13267);
  Scanner input = new Scanner(System.in);
  
  
  while (true) {
  System.out.println("Wait...");
  Socket sock = servsock.accept();
  System.out.println("OK : " + sock);
  
  // odbior pliku
  byte [] mybytearray  = new byte [filesize];
  InputStream is = sock.getInputStream();
  bytesRead = is.read(mybytearray,0,mybytearray.length);
  current = bytesRead;
  do {
     bytesRead =
        is.read(mybytearray, current, (mybytearray.length-current));
     if(bytesRead &gt;= 0) current += bytesRead;
  } while(bytesRead &gt; -1);
  
  System.out.println("New filename");
  String p = input.nextLine();
  File plik = new File(p);
  FileOutputStream fos = new FileOutputStream(plik);
  BufferedOutputStream bos = new BufferedOutputStream(fos);
  bos.write(mybytearray, 0 , current);
  bos.flush();
  
  System.out.println("File saved");
  bos.close();
  sock.close();
  </code></pre>
  
  <p>And I tried to do something like that:</p>
  
  <p>Client addon:</p>
  
  <pre><code>DataOutputStream outToServer = new DataOutputStream(sock.getOutputStream());
  
  sentence = input.nextLine();
  outToServer.writeBytes(sentence);
  </code></pre>
  
  <p>Server addon:</p>
  
  <pre><code>BufferedReader inFromClient = new BufferedReader(new InputStreamReader(sock.getInputStream()));
  
  clientSentence = inFromClient.readLine();
  System.out.println(clientSentence);
  </code></pre>
  
  <p>...but unfortunetly I have "Wait.." all the time for Client</p>