2017-04-15 81 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

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