2014-09-21 69 views
0

我看了很多例子,並且詢問了一些人,但是似乎沒有人能夠告訴我我的套接字上的錯誤。請幫忙。java中的套接字幫助

我正在製作一個程序,它從3個文件中讀取數據,然後在線程的套接字中發送數據。 這裏是我做了

public class Main { 

    public static void main(String[] args) throws Exception{ 
    //making all threads for A,B and C 
    Thread T_A = new Thread(new threads ("A")); 
    Thread T_B = new Thread(new threads ("B")); 
    Thread T_C = new Thread(new threads ("C")); 
    //starting the threads for A,B and C 
    T_A.start(); 
    T_B.start(); 
    T_C.start(); 
    } 
} 
import java.io.*; 
import java.net.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

public class threads implements Runnable { 
     //making temps 
     String name; 
     int listen; 
     int send; 
     String line; 
     BufferedReader reader; 
     BufferedReader input; 
     boolean Listening=false; 
     boolean Sending=false; 
     PrintWriter output; 
     ServerSocket node_listen=null; 
     Socket node_send=null; 
     Socket node_rev=null; 

     public threads(String x){ 
      //name of thead A,B or C 
      name=x;     
     } 

     @Override 
     public void run(){ 
     try{ 
      //openinng the conf file for thread 
      reader = new BufferedReader(new FileReader("conf" + name + ".txt")); 
      //this is for node A because it only sends info does not get info 
      if(name.equals("A")==true){ 
       //making a socket to send on 
       System.out.println("Node " + name + " is starting"); 
       send = Integer.parseInt(reader.readLine()); 
       for(int temp=0;temp!=5;temp++) 
       { 
        temp=Checker(temp); 
       } 
      } 
      //this is for node B becuase it sends and gets info 
      else if(name.equals("B")==true){ 
       //getting ports for send and listen 
       listen = Integer.parseInt(reader.readLine()); 
       send = Integer.parseInt(reader.readLine()); 
       System.out.println("Node " + name + " is starting"); 
       Listening=setUpListen();     
       for(int temp=0;temp!=5;temp++) 
       { 
        temp=Checker(temp); 
       } 
      }  
      //node C only gets info 
      else if(name.equals("C")==true){ 
       //geting listen port 
       listen = Integer.parseInt(reader.readLine()); 
       System.out.println("Node " + name + " is starting"); 
       Listening=setUpListen(); 
      } 
      //sends and gets data until it has no more to send or get 
      while(Listening==true || Sending== true) 
      { 
       //while it needs to get data and was a node that gets data 
       if(Listening==true) 
       { 
        Listen(); 
       } 
       //Has not sent terminate yet and is a sending node 
       if(Sending==true) 
       { 
        Send(); 
       } 
      } 
     } 
     catch(IOException | NumberFormatException e) 
     { 
      System.out.println(e); 
     } 
     catch (InterruptedException ex) 
     { 
       Logger.getLogger(threads.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     System.out.println("Node " + name + " has ended"); 
    } 

    public boolean setUpConnection(){ 
     try { 
      //making a socket for sending 
      System.out.println("Node " + name + " is setting up connection"); 
      node_send = new Socket("hostname", send); 
      output = new PrintWriter(node_send.getOutputStream(),true); 
      } catch (UnknownHostException e) { 
      System.out.println("Error setting up socket connection: " + name + " :" + send); 
      return false; 
      } catch (IOException e) { 
      System.out.println("Error setting up socket connection: " + name + " " + e); 
      return false; 
      } 
     //telling the thread it is a sending thread 
     Sending=true; 
     System.out.println("Node " + name + " is ready for sending"); 
     return true; 
    } 

    public boolean setUpListen(){ 
    try{ 
     //making socket for Listening 
     System.out.println("Node " + name + " is setting up listen"); 
     nodeListen = new ServerSocket(listen); 
     System.out.println("Node " + name + " is trying to listen"); 
     node_rev = node_listen.accept(); 
     System.out.println("Node " + name + " NEVER GETS HERE"); 
     input = new BufferedReader(new InputStreamReader(node_rev.getInputStream())); 
      } catch (UnknownHostException e) { 
       System.out.println("Error setting up socket connection: " + name + " :" + listen); 
       return false; 
      } catch (IOException e) { 
       System.out.println("Error setting up socket connection: " + name + " " + e); 
       return false; 
     } 
     //telling the thread it is a Listening thread 
     System.out.println("Node " + name + " is ready for listening"); 
     return true; 
    } 

    public void Listen(){ 
     try{ 
      line = input.readLine(); 
      //As long as something was sent and it was not terminate prints data 
      if(line.equals("terminate")==false&&line.equals("null")==false) 
      { 
       System.out.println("Node " + name + " received:" + line); 
      } 
      //When terminate was sent it no longer looks for data 
      if(line.equals("terminate")) 
      { 
       Listening=false; 
       node_listen.close(); 
       node_rev.close(); 
       input.close(); 
       System.out.println("Node " + name + " done listening"); 
      } 
     }catch (IOException e) { 
       System.out.println("Error Listening on connection: " + name + " " + e); 
     } 
    } 

    public void Send(){ 
     try{ 
     //reads in the data and sends it to node 
     line=reader.readLine(); 
     System.out.println(line); 
     output.println(line); 
     //if the data sent was terminate then exit sending mode 
     if(line.equals("terminate")==true) 
      { 
       Sending=false; 
       node_send.close(); 
       output.close(); 
       System.out.println("Node " + name + " done sending"); 
      } 
     }catch (IOException e) { 
       System.out.println("Error Sending on connection: " + name + " " + e); 
     } 
    } 

    public int Checker(int temp) throws InterruptedException{ 
     boolean connected=setUpConnection(); 
      if(connected==true) 
      { 
        //connected 
        temp=5; 
      } 
      return temp; 
     } 
} 

各的conf文件被設定這樣的

confA.txt

5002 
This is a sample line of text for node A. 
This is another sample line of text for node A. 
Node B will be printing out these three lines for node A. 
terminate 

confB.txt

5002 
5005 
This is a sample line of text. 
This is another sample line of text. 
Node C will be printing out these three lines. 
terminate 

confC每一件事.txt

5005 

那是出把我弄

run: 
Node A is starting 
Node A is setting up connection 
Node B is starting 
Node B is setting up listen 
Node C is starting 
Node C is setting up listen 
Node B is trying to listen 
Node C is trying to listen 
Error setting up socket connection: A java.net.ConnectException: Operation timed out 
Node A is setting up connection 
Error setting up socket connection: A java.net.ConnectException: Operation timed out 
Node A is setting up connection 
Error setting up socket connection: A java.net.ConnectException: Operation timed out 
Node A is setting up connection 
Error setting up socket connection: A java.net.ConnectException: Operation timed out 
Node A is setting up connection 
Error setting up socket connection: A java.net.ConnectException: Operation timed out 
Node A has ended 

的代碼似乎從來沒有站上罰球線的System.out.println( 「節點」 +姓名+ 「從來沒有得到HERE」);在setUpListen();

謝謝

+0

只要向上看,你可以'if()'而不是'if( == true)'。 – Obicere 2014-09-21 17:05:56

+0

酷我學到了新東西。謝謝。 哇,我有很多。大聲笑 – sixes 2014-09-21 17:31:56

+0

你想連接到服務器端或客戶端?因爲這是兩個不同的連接。 – Juniar 2014-09-21 17:56:35

回答

-1

public class threads?請大會。

O.t:

Error setting up socket connection: C java.net.BindException: Address already in use 

其設置好的組成每個偵聽的端口,是相同的。 (即地址已在使用中)。

+0

這是一箇舊的輸出。那是我測試只是在港口。我的代碼不再做那部分。我用新的輸出編輯它 – sixes 2014-09-21 18:04:41

2

這是因爲ServerSocket.accept()阻塞,直到報告傳入連接。該方法返回Socket

因此,下一行(System.out.println("Node " + name + " NEVER GETS HERE"))永遠不會執行,直到您獲得另一個網絡接口連接到您的收聽ServerSocket

我不知道它到底是什麼你想達到的,但你需要聽在分開的線程傳入的連接:

final ServerSocket serverSocket = createServerSocket(); 
new Thread(new Runnable() { 

    @Override 
    public void run() { 
     Socket socket = serverSocket.accept(); 
     System.out.println("Incoming connection from " + socket.getInetAddress()); 
     doSomethingWithSocket(socket); 
    } 
}).start(); 

PS:請與堅持Java Naming Conventions

  • 類應始終以大寫字母開頭;
  • 字段名稱(或類屬性)應始終以小寫字母開頭;
  • 方法名稱應始終以小寫字母開頭。
+0

我將參與命名約定。我不知道Java有他們這是我的第一個Java項目。謝謝 – sixes 2014-09-21 17:51:49

+0

那麼你剛開始並不是最簡單的部分,我想。 – 2014-09-21 18:21:05

+0

告訴我的老師。 :P – sixes 2014-09-21 21:43:16

0

事實證明,我的問題是node_send = new Socket(「hostname」,send);應該已經node_send = new Socket(「localhost」,send);

我很笨。

不過我想感謝大家的幫助和指點。