0

別々の場合

クライアント---メッセージの送信--->サーバー:正常に動作します!

別々の場合

サーバー---メッセージの送信--->クライアント:正常に動作します!

しかし、両方が一緒の場合:

クライアント---メッセージの送信先--->サーバー
サーバー---メッセージの送信先--->クライアント

何も機能しません!

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

import java.io.*;
import java.net.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyServer
{  
    private final static int port = 8000; 
    private static String hostname = "";
    private static String hostIP ="";
    public static void main(String[] args )
    {  
        ServerSocket serverSocket=null;
        try {
            // get host information
            hostname = InetAddress.getLocalHost().getHostName();
            hostIP = InetAddress.getLocalHost().getHostAddress();
            // display server information
            System.out.println("MyServer started on "+hostname+" with IP: "+hostIP + " on the port number: " + port);
            serverSocket = new ServerSocket(port);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        while(true)
        {   
            ClientWorker w;
            try
            {   
                w = new ClientWorker(serverSocket.accept());
                Thread t = new Thread(w);
                t.start();
            }
            catch(IOException e)
            {
                e.printStackTrace();
                break;
            }   
        }
    }
}   
class ClientWorker implements Runnable
{
    Socket incoming;
    public ClientWorker(Socket incoming)
    {
        this.incoming = incoming;
    }
    public void run()
    {
        String request = null;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            request = in.readLine();
            System.out.println("request =" + request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter out = null;
        try {
            out = new PrintWriter(incoming.getOutputStream(),true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.println("The command is not readable");

    }

}

ClientSideは次のとおりです。

import java.io.*;
import java.net.*;
public class MyClient2
{
    public static void main(String[] args)
    {
        String answer = null;
        if (args.length != 1){
            System.out.println("Usage: MyClient serverHostname");
            System.exit(0);
        }
        try
        {
            Socket socket = new Socket (args[0], 8000);
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
            out.print("TIME");
            System.out.println("Request has been send");
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
            answer = in.readLine();         
            System.out.println("The answer is:  "+answer);
            in.close();
            out.close();
            socket.close();
        }
        catch (Exception e)
        {
            System.out.println("Make sure the server is running and try again");
        }
    }
}

ご覧になって、コメントをいただければ幸いです。乾杯

4

1 に答える 1

5

readLine改行文字が読み取られるか、ストリームが閉じられるまでブロックするメソッドを使用しています。改行文字を送信したり、ストリームを閉じたりすることはありません(応答を受信する予定になるまで)。

に変更out.print("TIME");してみてくださいout.println("TIME");

また、発生するIOExceptionを非表示にし、代わりに手動で確認する必要があるため、PrintWriterの使用は避けてください。あなたの例外処理は素晴らしいものではありません。IOExceptionが発生した場合、エラーから回復するためにできることはあまりないため、おそらく(単に盲目的に続行するのではなく)スレッドを終了する必要があります。

さらに、あなたClientWorkerはそれ自体の後でクリーンアップしません-すなわち。ストリームとソケットを閉じます。

于 2012-10-14T04:28:17.310 に答える