1

うまく機能しているマルチスレッドサーバーを開発しました! しかし、クライアントが文字列を接続してから、文字列を受信して​​ファイルを送信する同じ通常のプロセスを続行することを初めて送信したいと思います! ここにサーバー用の私のコードがあります

import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.Scanner;

class TCPServer {
    public static void main(String argv[]) throws Exception {

        System.out.println("Welcome to the stream Server");
System.out.println("listening to Clients");
        ServerSocket welcomeSocket = new ServerSocket(3248);
        while (true) {
            Socket connectionSocket = welcomeSocket.accept();
            if (connectionSocket != null) {
                Client client = new Client(connectionSocket);
                client.start();

            }
        }
    }
}

class Client extends Thread {
    private Socket connectionSocket;


    public Client(Socket c) throws IOException {
        connectionSocket = c;
    }

    public void run() {

        String path = "C:/pao/new2/";

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();


        try {

            String fileToSendStr = readFile();
            File fileToSend = null;
            for (File f : listOfFiles)

            {

                if (f.getName().equals(fileToSendStr)) {
                    fileToSend = f;
                    break;
                }
            }
            System.out.println("Connecting to Client to recieve the part " +fileToSendStr);
            if (fileToSend == null) {

            }
            System.out.println("Sending the chunk to Client");
            long length = fileToSend.length();
            byte [] longBytes = new byte[8];
            ByteBuffer bbuffer = ByteBuffer.wrap(longBytes);
            bbuffer.putLong(length);
            connectionSocket.getOutputStream().write(longBytes);

            BufferedOutputStream bout = new BufferedOutputStream(connectionSocket.getOutputStream());
            BufferedInputStream bain = new BufferedInputStream(new FileInputStream(fileToSend));

            byte buffer [] = new byte [1024];
            int i = 0;
            while((i = bain.read(buffer, 0, 1024)) >= 0){
                bout.write(buffer, 0, i);

            }
            System.out.println("chunk sended");
            bout.close();
            bain.close();


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String readFile() throws IOException {

        BufferedReader r = new BufferedReader(new InputStreamReader(
                connectionSocket.getInputStream()));
        return r.readLine();

    }
}

接続を確立したいクライアントの部分は、その接続を確立する部分とは異なります。初めて文字列を取得したいだけで、その部分にはもうアクセスできません!

4

2 に答える 2

0

よく理解しているかどうかわかりませんが、現在、サーバーは正常に動作しており、クライアントにサービスを提供できるとのことです。あなたが知りたいのは、クライアントがすでに接続されているかどうかを知ることですか? サーバーとクライアント間の通信が複数の接続にまたがっていますか?

これには、概念的な思考が必要です。HTTP の世界にいた場合、これはある種のセッションおよび/または Cookie の概念を意味します。Cookie を使用すると、サーバーはクライアント側に情報を保存できます (クライアントが最後に接続したとき、ある種の状態)。セッション ID を使用すると、クライアントとの通信中にどのような状態にあるかを追跡できます。

あなたの場合、非常に単純なアプリケーション レベルのプロトコル (ISO/OSI ネットワーク スタックの最上位) を開発する必要があるかもしれません。したがって、たとえば、接続が確立された後、クライアントはサーバーにバイトを送信して、情報交換のステージを伝え、サーバーはそれに応じて反応します。通信の最後に、この「状態」パラメーターが更新され、修正されます。

それは役に立ちますか?

于 2013-04-17T19:08:24.370 に答える