2

Java でソケットを使用してクライアント サーバー システムを作成しようとしていますが、サーバーからクライアントに送信されたデータを読み取ることができないようです。

クライアントのコードは次のとおりです。

public class ClientSocket 
{
    Socket clientSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;


    // establish a connection to All Care's server application through socket 4444 (adjust localhost to reflect the IP address that the server
    // is being run from)
    public ClientSocket()
    {
        try
        {
            clientSocket = new Socket("localhost", 4445);

            out = new PrintWriter(clientSocket.getOutputStream());
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        }
        catch (IOException e)
        {
            System.out.println("Could not connect to All Care Server Application");
        }
    }

    public void closeClientSocket()
    {   
        try
        {
            clientSocket.close();
        }
        catch (IOException e)
        {
            System.out.println("Could not close connection to All Care Server Application");
        }
    }

    public String getMessageFromServer()
    {
        try
        {
            String input = in.readLine();

            return input;
        }
        catch (IOException e)
        {
            System.out.println("Could not read message from server");
        }

        return "No Data";
    }

    public void sendMessageToServer(String message)
    {
        out.write(message);
    }
}

サーバーコードは次のとおりです。

public class ArFileServer {

    public static void main(String[] args) 
    {
        ServerSocket serverSocket = null;
        boolean listening = true;

        try
        {
            serverSocket = new ServerSocket(4445);

            // infinite loop to continually listen for connection requests made by clients
            while (listening)
            {
                new ClientConnection(serverSocket.accept()).start();

                if (serverSocket != null)
                {
                    System.out.println("Connection to client established");
                }
            }

            serverSocket.close();
        }
        catch (IOException e)
        {
            System.out.println("Error could not create socket connection to port");
        }
    }
}

public class ClientConnection extends Thread
{
    private Socket socket = null;

    public ClientConnection(Socket socket) 
    {
        super("ClientConnection");
        this.socket = socket; 
    }

    // the thread that runs after a connection to the server has been accepted
    @Override
    public void run()
    {
        try
        {
            PrintWriter out = new PrintWriter(socket.getOutputStream());
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            sendMessagetoClient(out, "CONNECTION SUCCESS");

            // check login credentials sent from client to the server

            // if valid send back their encrypted password, otherwise output a login error message

            // wait for user input and then do various processes based on their requests

            in.close();
            out.close();
            socket.close();
        }
        catch (IOException e)
        {
            System.out.println("Client socket connection error");
        }
    }

    // sends a message to the client
    void sendMessagetoClient(PrintWriter out, String message)
    {
        out.write(message);
    } 

    // listens for a message from the client
    String getMessageFromClient(BufferedReader in)
    {      
        try
        {
            String input = in.readLine();
            return input;
        }
        catch (IOException e)
        {
            System.out.println("Could not read message from client");
        }

        return "No Data";
    }

データが送信されているかどうかを確認するために使用するコード行を次に示します。

System.out.println(clientSocket.getMessageFromServer());
4

3 に答える 3

2

sendMessageToClient()メソッドでは、次をフラッシュする必要があります。

void sendMessagetoClient(PrintWriter out, String message)
{
    out.write(message);
    out.flush();
} 

または、 を作成するときにPrintWriter、コンストラクターを次のように使用しautoflushます。

PrintWriter out = new PrintWriter(socket.getOutputStream(),true);

また、書くときは、out.write(message)orprintf()の代わりにprintln().

于 2012-07-19T03:50:34.753 に答える
0

ここにはいくつかの問題があります。

  1. あなたは行を読んでいますが、行を書いていません。

  2. null の readLine() の結果をチェックしていません。これは、ピアが接続を閉じたことを意味します。つまり、同様に行う必要があります。

  3. PrintWriter書き込み後にフラッシュしていません。

  4. あなたは間違った順序で物事を閉じています。ソケットに接続した出力ライター/ストリームを閉じる必要があります。それを行うと、それがフラッシュされ、入力ストリーム/リーダーとソケットが閉じられます。これを間違った順序で行うと、フラッシュが失われます。出力を閉じたら、他の 2 つのクローズは必要ありません。

  5. PrintWriter,通信中の例外とエラーについて知る必要がある場合、ネットワーク全体で例外を飲み込むを使用していますが、エラーもチェックしていません。使うBufferedWriter.

于 2012-07-19T08:07:06.313 に答える
-2

clintコードでは、サーバーソケットに接続していません。クリントソケット接続用

socket soc= new socket ("server host ip",port);
于 2012-07-19T03:44:06.213 に答える