2017-04-17 1 views
0

サーバーを実行して2つのクライアントを受け入れる簡単なプログラムを作成しようとしました。次に、そのうちの1人が別のクライアントに文字列を送信しようとします。 しかし、私のコードは動作しませんし、なぜ私は知らない。なぜクライアントに行がないと言われますか?InputStream?(ソケットプログラミング)

これは私TestClientクラスである:

public class TestClient extends Thread{ 

int id; 
String Name; 
Socket client; 
boolean isAsk; 


public TestClient(int id,String clientName,boolean isAsk) throws IOException { 
    this.id=id; 
    this.Name=clientName; 
    this.isAsk=isAsk; 
} 


public void connectTheClientToTheLocalHost(ServerSocket server){ 
    try { 
     client = new Socket("localhost",1111); 
     server.accept(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void readFromTerminal(){ 
    try { 
     InputStream is=client.getInputStream(); 
     OutputStream os = client.getOutputStream(); 
     PrintWriter pw = new PrintWriter(os); 
     pw.println("sdklfsdklfk"); 
     pw.flush(); 
     pw.close(); 
    }catch (IOException e){ 
     e.printStackTrace(); 
    } 
} 
public void closeTheCientSocket(){ 
    try { 
     client.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
public void write(){ 
    try { 

     Scanner sc = new Scanner(client.getInputStream()); 
     BufferedWriter bw = new BufferedWriter(new FileWriter(new File("file1.txt"))); 

     String st =sc.nextLine(); 

     bw.write(st); 

     bw.close(); 

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

@Override 
public void run() { 

    if(isAsk){ 

     readFromTerminal(); 
    } 
    else{ 

     write(); 
    } 
} 

、これが主な機能である:

public class PCServer { 


public static void main(String[] args){ 

    try { 
     ServerSocket s = new ServerSocket(1111); 
     TestClient t1=(new TestClient(1,"reza",true)); 
     TestClient t2=(new TestClient(2,"man",false)); 
     t1.connectTheClientToTheLocalHost(s); 

     t1.start(); 
     Scanner sc = new Scanner(t1.client.getInputStream()); 
     String st=sc.nextLine(); 
     System.out.println(st); 
     t1.closeTheCientSocket(); 

     t2.connectTheClientToTheLocalHost(s); 
     PrintWriter pw = new PrintWriter(t2.client.getOutputStream()); 
     pw.println(st); 
     pw.flush(); 

     t2.start(); 
     t2.closeTheCientSocket(); 


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

}

実際にこのコードは

String st=sc.nextLine(); 

に例外を返します私n main関数を呼び出し、行が見つからないことを示します。 何が問題ですか?

+0

例外がスローされるのは何ですか? – CraigR8806

+0

このような要素の例外はありません:行が見つかりません – user109107

答えて

0

JavaのServerSocketは、通常、別の方法で使用されます。

ポイントツーポイント接続が必要な場合、1つのホストがServerSocketを作成し、接続を受け入れます。例:

まずホスト例:

try(ServerSocket serverSocket = new ServerSocket(5555); 
    Socket socket = serverSocket.accept(); 
    // it more convenient to use DataInputStream instead of Scanner I think 
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); 
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());) { 

    while (!Thread.currentThread().isInterrupted()) { 
     String msg = dataInputStream.readUTF(); 
     System.out.println("got request: " + msg); 

     dataOutputStream.writeUTF("1-response"); 
     dataOutputStream.flush(); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

第二のホスト例:サーバー(ブローカー)の上に別のホスト話をしたい場合

try(Socket socket = new Socket("127.0.0.1", 5555); 
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); 
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) { 

    while (!Thread.currentThread().isInterrupted()) { 
     dataOutputStream.writeUTF("2-request"); 
     dataOutputStream.flush(); 

     String msg = dataInputStream.readUTF(); 
     System.out.println("got response: " + msg); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

、その後、あなたが必要ホスト上のプレーンJavaソケットとブローカ上のServerSocket、およびブローカはあるホストから別のホストに受信したメッセージを送信する必要があります。例:

ブローカ

try { 
    List<Socket> sockets = new ArrayList<>(); 

    ServerSocket serverSocket = new ServerSocket(5555); 

    // accepting connections from 2 clients 
    for (int i = 0; i < 2; i++) { 
     Socket socket = serverSocket.accept(); 
     sockets.add(socket); 
    } 

    // streams for first host 
    InputStream hostOneInputStream = sockets.get(0).getInputStream(); 
    DataInputStream hostOneDataInputStream = new DataInputStream(sockets.get(0).getInputStream()); 
    DataOutputStream hostOneDataOutputStream = new DataOutputStream(sockets.get(0).getOutputStream()); 

    // streams for second host 
    InputStream hostTwoInputStream = sockets.get(1).getInputStream(); 
    DataInputStream hostTwoDataInputStream = new DataInputStream(sockets.get(1).getInputStream()); 
    DataOutputStream hostTwoDataOutputStream = new DataOutputStream(sockets.get(1).getOutputStream()); 

    while (!Thread.currentThread().isInterrupted()) { 
     if (hostOneInputStream.available() > 0) { 
      String msg = hostOneDataInputStream.readUTF(); 
      System.out.println("got message from host 1: " + msg); 

      hostTwoDataOutputStream.writeUTF(msg); 
      hostTwoDataOutputStream.flush(); 
      System.out.println("message " + msg + " sent to host two"); 
     } 

     if (hostTwoInputStream.available() > 0) { 
      String msg = hostTwoDataInputStream.readUTF(); 
      System.out.println("got message from host 2: " + msg); 

      hostOneDataOutputStream.writeUTF(msg); 
      hostOneDataOutputStream.flush(); 
      System.out.println("message " + msg + " sent to host one"); 
     } 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

まずホスト

try(Socket socket = new Socket("127.0.0.1", 5555); 
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); 
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) { 

    while (!Thread.currentThread().isInterrupted()) { 
     dataOutputStream.writeUTF("1"); 
     dataOutputStream.flush(); 

     String msg = dataInputStream.readUTF(); 
     System.out.println("got msg: " + msg); 
     TimeUnit.SECONDS.sleep(5); 
    } 
} catch (IOException | InterruptedException e) { 
    e.printStackTrace(); 
} 

第二のホスト(別個のスレッドまたはプロセスで実行)(別のスレッドまたはプロセスで実行します)別のスレッドまたはプロセスで実行)

try(Socket socket = new Socket("127.0.0.1", 5555); 
    DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); 
    DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())) { 

    while (!Thread.currentThread().isInterrupted()) { 
     String msg = dataInputStream.readUTF(); 
     System.out.println("got msg: " + msg); 
     TimeUnit.SECONDS.sleep(5); 

     dataOutputStream.writeUTF("2"); 
     dataOutputStream.flush(); 
    } 
} catch (IOException | InterruptedException e) { 
    e.printStackTrace(); 
} 
+0

ありがとうございます。しかし、実際には、サーバーを実行し、クライアントがサーバーを介して相互にやり取りできるようにするプログラムを作成したいと考えています。 – user109107

+0

@ user109107だから、ブローカーの例はあなたが探しているものです。クライアントはブローカー(サーバー) – DontPanic

+0

を介して相互にやりとりしますが、私のコードには何が問題なのですか?私のコードは、私のコードが別の方法で仕事をしていることを除いて、ブローカーの例に似ていると思います – user109107

関連する問題