title: Client/Server Chat app with file transfer JAVA Sockets
body: <p>I am new to java and I made a chat application through which client and server can send and receive messages, now I was trying to send a file from client to server but after the file is received by server , client and server both cant send messages , Here is the code : </p>
  
  <p>Client Side : </p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import javax.swing.JFileChooser;
  
  public class ClientFrame extends javax.swing.JFrame {
  
      static Socket s;
      static DataOutputStream dos1;
      static DataInputStream dis;
      static javax.swing.JTextArea jT;
      static JFileChooser fc = new JFileChooser();
      File file;
  
      public ClientFrame() {
          initComponents();
          jT = jTextArea1;
      }
  
  
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  //send message
          try {
              String str1 = jTextField1.getText();
              String o = jT.getText() + "\n" + " Client -&gt;" + str1;
              jT.setText(o);
              dos1.writeUTF(str1);
          } catch (Exception ex) {
          }
      }                                        
  
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  //open file chooser
          fc.showOpenDialog(this);
          file = fc.getSelectedFile();
          jTextField3.setText(file.getAbsolutePath());
      }                                        
  
      private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  //send file         
          try {
              String st = jT.getText() + "\n" + " Client -&gt; " + "Sending a file";
              jT.setText(st);
              String str1 = "Client Sending a file,Press 'REC File' ";
              String st1 = "\n" + " Client -&gt;" + str1;
              dos1.writeUTF(st1);
          } catch (Exception e) {
          }
          long length = file.length();
          byte[] bytes = new byte[65536];//65536 is max, i think
          InputStream in;
          try {
              in = new FileInputStream(file);
              OutputStream out = s.getOutputStream();
              int count;
              while ((count = in.read(bytes)) &gt; 0) {
                  out.write(bytes, 0, count);
              }
              out.close();
              in.close();
              s_r_m();
          } catch (Exception ex) {
          }
      }                                        
  
      public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new ClientFrame().setVisible(true);
              }
          });
          try {
              s = new Socket("localhost", 3000);
          } catch (Exception e) {
          }
          s_r_m();
      }
  
      public static void s_r_m() {
          System.out.println("call srm");
          try {
              dis = new DataInputStream(s.getInputStream());
              dos1 = new DataOutputStream(s.getOutputStream());
              while (true) {
                  String str = (String) dis.readUTF();
                  String out = jT.getText() + "\n" + " Server -&gt;" + str;
                  jT.setText(out);
              }
          } catch (Exception ex) {
          }
      }
  }
  </code></pre>
  
  <p>Server Side : </p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import javax.swing.JFileChooser;
  
  public class ServerFrame extends javax.swing.JFrame {
  
      static ServerSocket ss;
      static Socket s;
      static DataInputStream dis;
      static DataOutputStream dos1;
      static javax.swing.JTextArea jT;
      static JFileChooser fc = new JFileChooser();
      File file;
  
      public ServerFrame() {
          initComponents();
          jT = jTextArea1;
      }
  
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  //send message
          try {
              String str1 = jTextField1.getText();
              String out = jT.getText() + "\n" + " Server -&gt;" + str1;
              jT.setText(out);
              dos1.writeUTF(str1);
          } catch (Exception ex) {
          }
      }                                        
  
      private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
          //open file chooser
          fc.showOpenDialog(this);
          file = fc.getSelectedFile();
          jTextField3.setText(file.getAbsolutePath());
      }                                        
  
  
      private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
  //rec file 
          InputStream in = null;
          OutputStream out = null;
          try {
              in = s.getInputStream();
              out = new FileOutputStream("F:\\yoMama.exe");
              int count;
              byte[] buffer = new byte[65536];
              while ((count = in.read(buffer)) &gt; 0) {
                  out.write(buffer, 0, count);
              }
              String o = jT.getText() + "\n" + " Client -&gt;" + " File received";
              jT.setText(o);
              out.close();
              in.close();
              s_r_m();
          } catch (Exception ex) {
          }
      }                                        
      public static void main(String args[]) {
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new ServerFrame().setVisible(true);
              }
          });
          try {
              ss = new ServerSocket(3000);
              s = ss.accept();//will accept connection from client,waiting state untill client connects
              s_r_m();
  
          } catch (Exception e) {
          }
      }
  
      public static void s_r_m() {
          System.out.println("call srm");
          try {
              dis = new DataInputStream(s.getInputStream());
              dos1 = new DataOutputStream(s.getOutputStream());
  
              while (true) {
                  String str = dis.readUTF();
                  String out = jT.getText() + "\n" + " Client -&gt;" + str;
                  jT.setText(out);
              }
          } catch (Exception ex) {
          }
      }
  }
  </code></pre>