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

标签 java sockets file-transfer

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

客户端

   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

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

解决方案是除了在服务器端接受连接的代码之外,还需要在那里编写接收数据的代码。

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

关于java文件传输套接字写入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17876824/

相关文章:

python - IRC 机器人无法连接到服务器 - Python

java - Java 中的客户端-服务器应用程序 - 单例问题

java - java中文件传输的问题

Java套接字编程头

java - 在其 for 循环之外访问整数

java - 当第三方客户端调用我的网络服务时调用 Controller

java.lang.runtimeException 无法启动 Activity componentinfo java.lang.nullpointerException

java - 如何使用 RestTemplate 向相对 URL 发送 POST 请求?

sockets - 带有 Golang 的蓝牙 RFCOMM 套接字

c++ - 通过 Google Protobuf 发送二进制文件数据