Java聊天程序也发送文件

时间:2016-04-08 13:48:53

标签: java file sockets chat transfer

我已经制作了一个基本的聊天程序,它也发送了,不完整。一切运行正常,除非我发送文件,我的程序收到它没有任何问题,但它给出一个套接字关闭错误。

客户端

import java.net.*;
import java.io.*;

public class chatClnt {

    public static int PORT = 6666;
    public static String IP_ADDR = "192.168.15.1";
    public static String F = "FILE MODE";

    public static void main(String[] ar) {

        try {
            InetAddress ipAddress = InetAddress.getByName(IP_ADDR);

            System.out.println("\nconnecting......");
            Socket socket = new Socket(ipAddress, PORT);
            System.out.println("\nconnected");

            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();

            DataInputStream in = new DataInputStream(sin);
            DataOutputStream out = new DataOutputStream(sout);

            BufferedReader keyboard = new BufferedReader(new         InputStreamReader(System.in));

            String line = null;
            String fname = null;

            while (true) {
                System.out.println("\nYou(Plain Text):>");
                line = keyboard.readLine();
                out.writeUTF(line);
                out.flush();
                if (line.equals(F)) {
                    //Sending File
                    OutputStream fout = socket.getOutputStream();
                    FileInputStream fis = null;
                    BufferedInputStream bis = null;
                    try {
                        System.out.println("Enter the path: ");
                        fname = keyboard.readLine();
                        File myFile = new File(fname);
                        byte[] mybytearray = new byte[(int)myFile.length()];
                        fis = new FileInputStream(myFile);
                        bis = new BufferedInputStream(fis);
                        bis.read(mybytearray,0,mybytearray.length);

                        fout = socket.getOutputStream();
                        System.out.println("Sending " + fname + "(" + mybytearray.length + " bytes)");
                        fout.write(mybytearray,0,mybytearray.length);
                        fout.flush();
                        System.out.println("Done.");
                    } finally {
                        if (bis != null) bis.close();
                        if (fout != null) fout.close();
                    }


                    line = in.readUTF(); // wait for the server to send a line of text.
                    System.out.println("\nSender(Plain Text):> " + line);
                    System.out.println();
                }
            }
        } catch(Exception x) {
            x.printStackTrace();
        }
    }
}

SERVER

import java.net.*;
import java.io.*;


public class chatSrv {

    public static int PORT = 6666;
    public static String F = "FILE MODE";
    public final static String FILE_TO_RECEIVE = "received.txt";
    public final static int FILE_SIZE = 999999999;

    public static void main(String[] ar) {

        try { 
            ServerSocket ss = new ServerSocket(PORT);
            System.out.println("\nconnecting...");
            Socket socket = ss.accept();

            System.out.println("\nconnected");
            System.out.println();

            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();

            DataInputStream in = new DataInputStream(sin);
            DataOutputStream out = new DataOutputStream(sout);

            String line = null;
            int bytesRead;
            int current = 0;

            while (true) {
                line = in.readUTF();

                //Receiving File
                if (line.equals(F)) {
                    FileOutputStream fos = null;
                    BufferedOutputStream bos = null;
                    try {
                        byte [] mybytearray = new byte [FILE_SIZE];
                        fos = new FileOutputStream(FILE_TO_RECEIVE);
                        bos = new BufferedOutputStream(fos);
                        bytesRead = sin.read(mybytearray,0,mybytearray.length);
                        current = bytesRead;

                        do {
                            bytesRead = sin.read(mybytearray, current, (mybytearray.length-current));
                            if (bytesRead >= 0) current += bytesRead;
                        } while (bytesRead > -1);

                        bos.write(mybytearray, 0 , current);
                        bos.flush();
                        System.out.println("File " + FILE_TO_RECEIVE+ " downloaded (" + current + " bytes read)");
                    } finally {
                        if (fos != null) fos.close();
                        if (bos != null) bos.close();
                    }

                    System.out.println("\nSender(Plain Text):> " + line);
                    BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

                    System.out.println("\nYou(Plain Text):>");
                    line = keyboard.readLine();
                    out.writeUTF(line); 
                    out.flush(); 
                    System.out.println();
                }
            }
        } catch(Exception x) {
            x.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

关闭套接字的输出流时,也会关闭套接字。 documentation说:

  

关闭返回的OutputStream将关闭相关的套接字。

所以你无法关闭流。但如果你不关闭它,你将永远不会在接收方获得-1

基本上,您不能使用从磁盘读取文件时使用的相同读取过程。您需要设计一个协议,允许您在保持流打开的同时读取文件。可能的策略:

  • 为此打开第二个插槽。例如,在可用端口上打开一个套接字,并向服务器发送一条消息,告知它应该使用此端口号连接到另一个套接字。然后,您可以通过其他套接字发送整个文件并关闭它。或者要求服务器打开第二个套接字并告诉您要连接的端口号。这是ftp协议使用的策略。
  • 提前以字节为单位发送文件的长度,在接收端,读取多个字节。这是HTTP使用的策略,例如,当它发送Content-length:标题时。

请记住,文件可能会很长,因此如果您决定发送文件,请使用long而不是int