2011-01-03 18 views
1

私のコードに何が問題なのですか? 私の悪い英語Javaソケットプログラミングの問題


package sockettest; 

import java.io.*; 
import java.net.*; 

class sevr implements Runnable{ 
    public void run() { 
     ServerSocket sSkt = null; 
     Socket skt = null; 
     BufferedReader br = null; 
     BufferedWriter bw = null; 

     try{ 
      System.out.println("Server: is about to create socket"); 
      sSkt = new ServerSocket(6666); 
      System.out.println("Server: socket created"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: socket creation failure"); 
     } 
     try{ 
      System.out.println("Server: is listening"); 
      skt = sSkt.accept(); 
      System.out.println("Server: Connection Established"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: listening failed"); 
     } 
     try{ 
      System.out.println("Server: creating streams"); 
      br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); 
      System.out.println("Server: stream done"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: stream failed"); 
     } 
     System.out.println("Server: reading the request"); 
     try{ 
      String line = null; 
      while((line =br.readLine()) != null){ 
      System.out.println("Server: client said-> "+ line); 
      } 
     } 
     catch(IOException e){ 
      System.out.println("Server: reading failed"); 
     } 
     System.out.println("Server: reading fished"); 

     System.out.println("Server: responding"); 
     try{ 
      bw.write("Hi! I am server!"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: responding failed"); 
     } 
     System.out.println("Server: responding finished"); 

     System.out.println("Server: is finishing"); 
     try { 
      br.close(); 
      bw.close(); 
      skt.close(); 
      sSkt.close(); 
     } catch (IOException e) { 
      System.out.println("Server: finishing failed"); 
     } 
     System.out.println("Server: done"); 
    } 
} 

class clnt implements Runnable{ 
    public void run() { 
     Socket skt = null; 
     BufferedReader br = null; 
     BufferedWriter bw = null; 

     try{ 
      System.out.println("Client: about to create socket"); 
      skt = new Socket(InetAddress.getLocalHost(),6666); 
      System.out.println("Client: socket created"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: socket creation failure"); 
     } 

     try{ 
      System.out.println("Client: creating streams"); 
      br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); 
      System.out.println("Client: stream done"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: stream failed"); 
     } 
     System.out.println("Client: requesting"); 
     try{ 
      bw.write("Hi! I am Client!"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: requesting failed"); 
     } 
     System.out.println("Client: requesting finished"); 
     System.out.println("Client: reading the respond"); 
     try{ 
      String line = null; 
      while((line =br.readLine()) != null){ 
      System.out.println("Client: server said-> "+ line); 
      } 
     } 
     catch(IOException e){ 
      System.out.println("Client: reading failed"); 
     } 
     System.out.println("Client: reading fished"); 



     System.out.println("Clientrver: is finishing"); 
     try { 
      br.close(); 
      bw.close(); 
      skt.close(); 
     } catch (IOException e) { 
      System.out.println("Client: finishing failed"); 
     } 
     System.out.println("Client: done"); 
    } 
} 


public class Main { 


    public static void main(String[] args) { 
     System.out.println("Main started"); 
     Thread sThread = new Thread(new sevr()); 
     Thread cThread = new Thread(new clnt()); 
     sThread.start(); 
     cThread.start(); 
     try { 
      sThread.join(); 
      cThread.join(); 
     } catch (InterruptedException ex) { 
      System.out.println("joining failed"); 
     } 
     System.out.println("Main done"); 

    } 

} 

出力について申し訳ありません:

 
Main started 
Server: is about to create socket 
Client: about to create socket 
Client: socket created 
Client: creating streams 
Server: socket created 
Server: is listening 
Server: Connection Established 
Server: creating streams 
Server: stream done 
Server: reading the request 
Client: stream done 
Client: requesting 
Client: requesting finished 
Client: reading the respond 

、それは永遠にここで待ちます!

答えて

3

にハングアップ今それが動作

bw.write("I am the Client!!\n"); 
bw.flush(); 
+0

あなたはそうです、私はあまりにも私がそれを見ていないautoflushでPrintWriterに慣れています。 – Ronnis

+0

tanxたくさん!私はそれを得るために読んでいるセクションから "while"を削除する必要があります –

2

両方のスレッドは、プログラムがBufferedWriterのを使用しているが、これはをフラッシュし(それがフラッシュされるまで、バッファが満杯または閉じ、バッファに座るであろう)と送られない改行がないないwhile((line =br.readLine()) != null)

+0

はい、私は「あれば」、あるいは印刷無条件を行うには「ながら」私が変更した場合は変化がない、ということを知っています。 –

-1

を試してみてください(あなたが改行を送信するまでのreadLineは待機します)


package sockettest; 

import java.io.*; 
import java.net.*; 

class sevr implements Runnable{ 
    public void run() { 
     ServerSocket sSkt = null; 
     Socket skt = null; 
     BufferedReader br = null; 
     BufferedWriter bw = null; 

     try{ 
      System.out.println("Server: is about to create socket"); 
      sSkt = new ServerSocket(6666); 
      System.out.println("Server: socket created"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: socket creation failure"); 
     } 
     try{ 
      System.out.println("Server: is listening"); 
      skt = sSkt.accept(); 
      System.out.println("Server: Connection Established"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: listening failed"); 
     } 
     try{ 
      System.out.println("Server: creating streams"); 
      br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); 
      System.out.println("Server: stream done"); 
     } 
     catch(IOException e){ 
      System.out.println("Server: stream failed"); 
     } 
     System.out.println("Server: reading the request"); 
     try{ 
      String line = null; 
      line = br.readLine(); 
      System.out.println("Server: client said-> "+ line); 
     } 
     catch(IOException e){ 
      System.out.println("Server: reading failed"); 
     } 
     System.out.println("Server: reading fished"); 

     System.out.println("Server: responding"); 
     try{ 
      bw.write("Hi! I am server!\n"); 
      bw.flush(); 
     } 
     catch(IOException e){ 
      System.out.println("Server: responding failed"); 
     } 
     System.out.println("Server: responding finished"); 

     System.out.println("Server: is finishing"); 
     try { 
      br.close(); 
      bw.close(); 
      skt.close(); 
      sSkt.close(); 
     } catch (IOException e) { 
      System.out.println("Server: finishing failed"); 
     } 
     System.out.println("Server: done"); 
    } 
} 

class clnt implements Runnable{ 
    public void run() { 
     Socket skt = null; 
     BufferedReader br = null; 
     BufferedWriter bw = null; 

     try{ 
      System.out.println("Client: about to create socket"); 
      skt = new Socket(InetAddress.getLocalHost(),6666); 
      System.out.println("Client: socket created"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: socket creation failure"); 
     } 

     try{ 
      System.out.println("Client: creating streams"); 
      br = new BufferedReader(new InputStreamReader(skt.getInputStream())); 
      bw = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); 
      System.out.println("Client: stream done"); 
     } 
     catch(IOException e){ 
      System.out.println("Client: stream failed"); 
     } 
     System.out.println("Client: requesting"); 
     try{ 
      bw.write("Hi! I am Client!\n"); 
      bw.flush(); 
     } 
     catch(IOException e){ 
      System.out.println("Client: requesting failed"); 
     } 
     System.out.println("Client: requesting finished"); 
     System.out.println("Client: reading the respond"); 
     try{ 
      String line = null; 
      line =br.readLine(); 
      System.out.println("Client: server said-> "+ line); 
     } 
     catch(IOException e){ 
      System.out.println("Client: reading failed"); 
     } 
     System.out.println("Client: reading fished"); 



     System.out.println("Client: is finishing"); 
     try { 
      br.close(); 
      bw.close(); 
      skt.close(); 
     } catch (IOException e) { 
      System.out.println("Client: finishing failed"); 
     } 
     System.out.println("Client: done"); 
    } 
} 


public class Main { 


    public static void main(String[] args) { 
     System.out.println("Main started"); 
     Thread sThread = new Thread(new sevr()); 
     Thread cThread = new Thread(new clnt()); 
     sThread.start(); 
     cThread.start(); 
     try { 
      sThread.join(); 
      cThread.join(); 
     } catch (InterruptedException ex) { 
      System.out.println("joining failed"); 
     } 
     System.out.println("Main done"); 

    } 

} 

出力:


Main started 
Server: is about to create socket 
Client: about to create socket 
Client: socket created 
Client: creating streams 
Server: socket created 
Server: is listening 
Server: Connection Established 
Server: creating streams 
Client: stream done 
Server: stream done 
Server: reading the request 
Client: requesting 
Client: requesting finished 
Client: reading the respond 
Server: client said-> Hi! I am Client! 
Server: reading fished 
Server: responding 
Server: responding finished 
Server: is finishing 
Client: server said-> Hi! I am server! 
Client: reading fished 
Client: is finishing 
Client: done 
Server: done 
Main done 
+0

答えとしてあなた自身の質問にあなたのソリューションを投稿しないでください。質問を編集し、最も役立った回答を選んでください。今すぐ閉じます。 – Chochos