1

私は Java の初心者なので、この作業を行うには何が必要ですか? クラスを作成し、このコードを貼り付ける main メソッドを追加しましたが、これは機能しません。サーバークラスでは、finally 変数に関するいくつかのエラーが表示されます。私は何も理解していません。

クライアントクラス

    //Creating the client socket:
    Socket socket = new Socket ();

    //Binding to the local socket address:
    InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
    int localIpPort = 0;
    SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
    socket.bind (localSocketAddress);

    //Connecting to the remote socket address:
    InetAddress remoteIpAddress = InetAddress.getByName ("localhost");
    int remoteIpPort = 20000;
    SocketAddress remoteSocketAddress = new InetSocketAddress (remoteIpAddress, remoteIpPort);
    socket.connect (remoteSocketAddress);

    //Receiving and/or sending data through inbound and outbound streams:
    BufferedReader reader = new BufferedReader (new InputStreamReader (socket.getInputStream ()));
    BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (socket.getOutputStream ()));

    String request = "Hello";
    writer.write (request);
    writer.newLine ();
    // Do not forget to flush
    writer.flush ();

    // Reading the response
    String response = reader.readLine ();

    //Shutting-down the inbound and outbound streams:
    socket.shutdownInput ();
    socket.shutdownOutput ();

    //Closing the socket:
    socket.close ();
    [...]

サーバークラス

    //Creating the server socket:
    ServerSocket socket = new ServerSocket ();

    //Binding to the local socket address -- this is the one the clients should be connecting to:
    InetAddress localIpAddress = InetAddress.getByName ("0.0.0.0");
    int localIpPort = 20000;
    SocketAddress localSocketAddress = new InetSocketAddress (localIpAddress, localIpPort);
    socket.bind (localSocketAddress);

    while (true) {

            //For each connection accepting a client socket, and:
            Socket client = socket.accept ();

            // Starting a new Thread for each client
            new Thread () {

                    public void run () {
                            try {
                                    //Receiving and/or sending data;
                                    BufferedReader reader = new BufferedReader (new InputStreamReader (client.getInputStream ()));
                                    BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (client.getOutputStream ()));

                                    // Reading the request
                                    String request = reader.readLine ();
                                    // Write the response
                                    String response = "Welcome";
                                    writer.write(response);
                                    writer.newLine();
                                    // Do not forget to flush!
                                    writer.flush();

                                    client.close ();
                            } catch (Exception e) {
                                    e.printStackTrace();
                            }
                    }
            };
    }

    //Closing the server socket;
    socket.close ();
4

2 に答える 2

1

に変更Socket client = socket.accept ();final Socket client = socket.accept ();ます。

于 2013-10-01T12:31:29.703 に答える
1

匿名の内部クラスを使用しており、含まれているスコープから変数 (クライアントなど) にアクセスするには、それらを final として宣言する必要があります。

final キーワードは、後で変数を再割り当てできないことを意味しますが、それが問題になるとは思いません。

説明する:

ここ...

new Thread () {

                public void run () {
                        try {

Thread をオンザフライでサブクラス化し、runnable インターフェイスから run メソッドを実装しています。これは、別の場所で再利用できる新しいクラスを宣言していないため、匿名クラスです。これは Thread の 1 回限りのサブクラスであり、匿名であるため、クラス定義の外部からの変数のみを使用できます。

于 2013-10-01T12:32:01.027 に答える