2017-04-15 79 views
1

我是新來的Java和我做了一個聊天應用程序,客戶端和服務器可以通過它發送和接收消息,現在我試圖發送文件從客戶端到服務器,但後文件由服務器,客戶端和服務器都收到不能發送郵件,下面是代碼:客戶端/服務器聊天應用程序與文件傳輸JAVA插座

客戶端:

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 ->" + 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 -> " + "Sending a file"; 
      jT.setText(st); 
      String str1 = "Client Sending a file,Press 'REC File' "; 
      String st1 = "\n" + " Client ->" + 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)) > 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 ->" + str; 
       jT.setText(out); 
      } 
     } catch (Exception ex) { 
     } 
    } 
} 

服務器端:

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 ->" + 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)) > 0) { 
       out.write(buffer, 0, count); 
      } 
      String o = jT.getText() + "\n" + " Client ->" + " 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 ->" + str; 
       jT.setText(out); 
      } 
     } catch (Exception ex) { 
     } 
    } 
} 
+0

@丹尼亞爾Javaid添加了的initComponents()的代碼梅託德和jTextArea1請 – DontPanic

+0

但只適用於GUI相關變量的聲明,主要問題出在插座I/Ostreams –

回答

1

公關功能是在s_r_m()功能。在while循環第一條語句是String str = dis.readUTF();因此,這裏ClientServer將首先等待來自另一方的回覆,最終將以Deadlock結束。所以他們中的任何一個都不能發送任何數據,直到從另一端收到。

因此,您需要相應地更改代碼。我已經實現了一個代碼來解決這個問題,它需要從Key-Board(STDIN)輸入。

DataInputStream dis=new DataInputStream(sckt.getInputStream()); 
DataInputStream dis1=new DataInputStream(System.in); 
DataOutputStream dos=new DataOutputStream(sckt.getOutputStream()); 

String str=""; 
while(true) 
{ 
    while(dis.available()>0) //If input is available from the other side. 
    { 
     String out = jT.getText() + "\n" + " Client ->" + dis.readUTF(); 
     jT.setText(out); 
    } 

    while(dis1.available()>0) //If input is available from STDIN to send it to the other side. 
    { 
     str=sc.nextLine(); 
     dos.writeUTF(str); 
     dos.flush(); 
    } 
} 

在這裏,你可以改變的同時,從輸入到STDINText-Field在應用程序中有代碼。所以每當用戶按下Send按鈕時,它會將消息發送到另一側。

+0

試過,但沒有工作,我想文件發送和接收應該使用線程來完成,對嗎? –

+0

是的有更好的應用程序! –