1

クライアントとサーバーのファイル転送プロジェクトに取り組んでいます。ローカルホストでのテストはほぼ完了しましたが、今日は以下のエラーが発生しました。クライアントとサーバーのソースコードは次のとおりです。

クライアント側

   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 ファイルは作成されますが、ストリームは取得されません)。

編集:言及するのを忘れていました。現在、私はワイヤレスインターネット接続を使用しており、ワイヤレスネットワークのテストが信頼できないと言及しているソケットプログラミングについて少し読んだことがあります

4

1 に答える 1

0

サーバー側は、データが受信されるのを待ちません。接続を受け入れ、次のループ サイクルをすぐに続行します。これにより、最初のサーバー ソケットとクライアントへのソケットがガベージ コレクションされ、閉じられます。

この動作は、telnet を使用して確認できます。これは、サーバー全般をチェックする場合に非常に便利です。サーバー マシンでコマンド プロンプト (cmd またはコンソール) を開き、次のコマンドを実行してサーバーに接続します。

telnet localhost 3249

自分のクライアント アプリケーションと同じように、接続してからすぐに切断されることがわかります。

解決策は、サーバー側で接続を受け入れるためのコードに加えて、データを受信するためのコードをそこに記述する必要があるということです。

さらに、ループ内ではなく、ループの前にサーバー ソケットの作成を配置する必要があります。サーバーソケットを一度だけ作成する必要があり、それを介して任意の数の接続を受け入れることができます。サーバー ソケットを 2 回以上開くと、ポートがまだ占有されているため、失敗します (一部のオペレーティング システムでは、前のサーバー ソケットが閉じられている場合でも、ポートを解放するのに数秒かかることがよくあります)。

于 2013-07-26T08:53:28.477 に答える