0

私はJavaアプリケーションの開発の初心者です。私はアンドロイドでチャットアプリケーションを作っています。入ってくるクライアントにサービスを提供するためにスレッドを使用していますが、クライアントがサーバーに接続している場合、ソケットに含まれているデータを取得できませんが、クライアント接続が失われると、データを表示できます。ReadLineメソッドを使用してソケットからデータを読み取ります。これはサーバー側のプログラムコードです。

package server;
import java.net.*;
import java.io.*;
import java.util.Vector;  

import com.sun.org.apache.bcel.internal.generic.NEW;  

public class Server {       

public static void main(String[] args)throws IOException, InstantiationException,
IllegalAccessException {            

    ServerSocket servsocket = null;  
    Socket sock = null;  
    byte[] bytebuffer = new byte[512];  

    try {
        System.out.println("SERVER IS RUNNING...");
        servsocket = new ServerSocket(28000);
        while(true){
        sock = servsocket.accept();
        System.out.println(servsocket.isBound());
        System.out.println("Port "+servsocket+" Ready!!!");
        System.out.println("Accept connection requests from " + sock);
        System.out.println("From CLIENT "+sock.getInetAddress()+ " and PORT " +  
            sock.getPort());
        ChatThread thread = new ChatThread(sock);  
        System.out.println("Thread is running");
        thread.run();       


        }
    } catch (IOException ioe) {
        System.out.println(ioe);
    } 
    finally{
            try {
                servsocket.close();
                } catch (IOException ioe) {
                    System.out.println(ioe);
                }
            }       
    }
    } 

class ChatThread extends Thread{
    static Vector<ChatThread> chatthread = new Vector<ChatThread>(10);
    private Socket sock;
    private BufferedReader in ;
    private PrintWriter out;

    public ChatThread (Socket socket) throws IOException {
        this.sock = socket;
        in = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
        out = new PrintWriter(
                new OutputStreamWriter(socket.getOutputStream()));

        byte[] bytebuffer = new byte[512];
        int receivemssg;

    }

    public void run(){
        int recvMsgSize;
        byte[] bytebuffer = new byte[512];
        String readsocket;
        try {
            readsocket = in.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
            }
                }   

プログラム起動時のサーバー側ディスプレイの下。クライアント側から「Hello....」という言葉を送ってみました。スレッドが実行されていないことがわかります。

サーバーは実行中です...truePort ServerSocket [addr = 0.0.0.0 / 0.0.0.0、port = 0、localport=28000]準備完了!!! Socket [addr =/172.17.231.254,port=3567,localport=28000]からの接続要求を受け入れます。CLIENT/172.17.231.254およびPORT3567からスレッドが実行されています。

スレッドのreadlineメソッドをgetInputStreamに置き換えると、クライアントからスレッドを実行してメッセージを表示できます。これは、以前使用したreadlineメソッドを置き換えるためにスレッドに入力するコードです。

public ChatThread (Socket socket) throws IOException {
        this.sock = socket;
        in  = sock.getInputStream();
        out = sock.getOutputStream();
        byte[] bytebuffer = new byte[512];
        int receivemssg;

    }

    public void run(){
        int recvMsgSize;
        byte[] bytebuffer = new byte[512];

        System.out.println("Thread is Running...");
        String masuk = new String(bytebuffer);

        System.out.println(bytebuffer);
        System.out.println(in.toString());
        System.out.println("thread successfully executed !!!");

        synchronized (chatthread) {
            chatthread.addElement(this);
        }

         try {
            while ((recvMsgSize = in.read(bytebuffer)) != -1) {
                    out.write(bytebuffer, 0, recvMsgSize);
                    System.out.println("The length of a character is received and returned "+bytebuffer.length);
                 }
        } catch (IOException e) {

            System.out.println(e);
        }
                    }
                    }

しかし、次の問題は、次のように表示される文字列/テキストでソケットの内容を表示できないことです。

ポートServerSocket[addr= 0.0.0.0 / 0.0.0.0、port = 0、localport = 28000] Siap !!! Socket [addr =/172.17.231.254,port=3577,localport=28000]からの接続要求を受け入れます。CLIENT/172.17.231.254およびPORT3577からスレッドが実行されています...[B@7c6768java.net.SocketInputStream@1690726スレッドが正常に実行されました! !! 文字の長さを送受信して512

助けてください、ありがとう:)GBUみんな...

4

1 に答える 1

1

開発者向けドキュメントを参照してください

public final String readLine () 以降: API レベル 1

このストリームから取得できるテキストの次の行を含む文字列を返します。行は 0 個以上の文字で構成され、その後に '\n'、'\r'、"\r\n"、またはストリームの終わりが続きます。文字列には改行シーケンスは含まれません。

readLine() はブロックされ、改行文字などの行末条件が検出されるか、ストリームの終わりに到達するまで返されません。これは、おそらく接続が失われたときに発生することです。

readLine() を使用する場合は、"Hello....\n" を送信するか、readLine() が確認できるように終了文字を追加する必要があります。

于 2012-05-04T15:02:25.457 に答える