Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有文件传输JAVA套接字的客户端/服务器聊天应用程序_Java_Sockets_Tcp - Fatal编程技术网

带有文件传输JAVA套接字的客户端/服务器聊天应用程序

带有文件传输JAVA套接字的客户端/服务器聊天应用程序,java,sockets,tcp,Java,Sockets,Tcp,我是java新手,我制作了一个聊天应用程序,客户端和服务器可以通过该程序发送和接收消息,现在我尝试将文件从客户端发送到服务器,但在服务器接收到文件后,客户端和服务器都无法发送消息,下面是代码: 客户端: import java.io.*; import java.net.*; import javax.swing.JFileChooser; public class ClientFrame extends javax.swing.JFrame { static Socket s;

我是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) {
        }
    }
}

问题出在
s\r\m()
函数中。在
while
循环中,第一条语句是
String str=dis.readUTF()
因此,在这里,
客户端
服务器
都将首先等待另一方的回复,这将导致
死锁
。所以他们中的任何一个都不能发送任何数据,直到从另一方接收

因此,您需要相应地更改代码。我已经实现了一个代码来解决这个问题,它从
键盘(STDIN)
获取输入


在这里,您可以将输入代码从应用程序中的
STDIN
更改为
文本字段。因此,每当用户按下
Send
按钮时,它都会将消息发送到另一端。

@Daniyal Javaid为initComponents()metod和jtextraea1 plsb添加您的代码,但这只是GUI相关变量的声明,主要问题在于套接字I/Ostreamstried这一点但没有起作用,我认为文件发送和接收应该使用线程,对吗?是的,有一个更好的应用程序!
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();
    }
}