繁体   English   中英

Java文件传输套接字写入错误

[英]java file transfer socket write error

我正在做一个客户端-服务器文件传输项目,我几乎在localhost上完成了测试,但是今天下面出现错误,这是客户端和服务器的源代码:

客户端

   public class Client {

    public static void main(String args[]) {
        String receiverIP = null;
        int serverPort = 0;
        receiverIP = args[0];
        serverPort = Integer.parseInt(args[1]);
        String fileToSend = args[2]; 
        byte[] aByte = new byte[1];
        int bytesR;
        Socket clientSocket = null;
        BufferedOutputStream bos = null;
        InputStream is = null;

        try {
            clientSocket = new Socket(receiverIP, serverPort);
            bos = new BufferedOutputStream(clientSocket.getOutputStream());           
            is = clientSocket.getInputStream();
        } catch (IOException ex) {
            ex.printStackTrace();
        }    

        if (is != null) {           
            FileOutputStream fos = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {

                    File myFile = new File( fileToSend );
                    System.out.println("The file chosen is being sent...");
                    byte[] mybytearray = new byte[(int) myFile.length()];
                    FileInputStream fis = null;

                    try {
                        fis = new FileInputStream( myFile );
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    try {
                        BufferedInputStream bis = new BufferedInputStream(fis);
                        bis.read(mybytearray, 0, mybytearray.length);
                        bos.write(mybytearray, 0, mybytearray.length);
                        bis.close();

                        return;
                    }catch (IOException ex) {
                        ex.printStackTrace();
                    }

                File file = new File("C:\\copy.jpg");
                fos = new FileOutputStream( file );
                bos = new BufferedOutputStream(fos);
                bytesR = is.read(aByte, 0, aByte.length);
                do {
                        baos.write(aByte);
                        bytesR = is.read(aByte);
                } while (bytesR != -1);
                System.out.println("File transfer successful");

                bos.write(baos.toByteArray());
                bos.flush();
                bos.close();
                clientSocket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

服务器端

    public class Server {

    public static void main(String args[]) {

        while (true) {
            ServerSocket welcomeSocket = null;
            BufferedOutputStream ToClient = null;

            try {
                welcomeSocket = new ServerSocket(3249);
                System.out.println("The port " + welcomeSocket.getLocalPort() + " is opened and ready for use.");
                Socket connectionSocket = welcomeSocket.accept();

                ToClient = new BufferedOutputStream(connectionSocket.getOutputStream());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

这是我得到的错误

    The file chosen is being sent...
java.net.SocketException: Connection reset by peer: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(Unknown Source)
    at java.net.SocketOutputStream.write(Unknown Source)
    at java.io.BufferedOutputStream.write(Unknown Source)
    at Client.main(Client.java:44)
java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at Client.main(Client.java:55)

我几乎可以肯定,该错误不是与所有数据都被传输之前关闭服务器套接字有关,以及与字节数组上的读写过程有关,但是我所有的修复尝试都是徒劳的,也许我放错了流,所以它们没有按预期工作(创建copy.jpg文件但未获得任何流)任何帮助将不胜感激

编辑:我忘了提及,目前我正在使用无线互联网连接,并且我已经阅读了一些有关套接字格式的文章,其中提到无线网络不可靠地进行测试

您的服务器端不等待接收数据。 它接受连接,然后立即继续下一个循环。 这会导致您的初始服务器套接字和客户端套接字被垃圾收集,从而被关闭。

您可以通过使用telnet来验证此行为,这在通常检查服务器时非常方便。 在服务器计算机(cmd或控制台)上打开命令提示符,然后运行以下命令以连接到服务器:

telnet localhost 3249

您将看到它连接,然后几乎立即断开连接,就像您自己的客户端应用程序一样。

解决方案是,除了用于在服务器端接受连接的代码外,您还需要在此处编写用于接收数据的代码。

此外,您应该将服务器套接字的创建放在循环的前面而不是循环的内部。 您只需要创建一次服务器套接字,然后就可以通过它接受任意多个连接。 多次打开服务器套接字将失败,因为该端口仍被占用(在某些操作系统中,即使关闭了先前的服务器套接字,释放端口通常也要花费几秒钟)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM