2012-05-18 214 views
0

嗨,我爲客戶端和服務器寫入acode,現在我想在clint之間傳遞消息clint two,並且我沒有成功在服務器端做到這一點我想構建數組姓名和身份證後,我從客戶端發送的消息,我可以選擇在哪裏或哪個名稱服務器傳遞消息的請求幫我寫詩這 所以這是克林特側詢問客戶端到客戶端之間傳送消息

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 


public class client { 

    public static void main(String[] args) { 
     Socket socket = null; 
     try { 
      socket = new Socket("127.0.0.1", 7777); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      BufferedReader readerFromCommandLine = new BufferedReader(new InputStreamReader(System.in)); 
      PrintWriter writer = new PrintWriter(socket.getOutputStream()); 

      while(true) {        
       System.out.println("Say something:"); 
       String userInput = readerFromCommandLine.readLine(); 
       writer.println(userInput); 
       writer.flush(); 

       String input = reader.readLine();    

       System.out.println("Got from server: "+input); 

       if (userInput.equalsIgnoreCase("bye")) { 
        break; 
       } 
      } 
     } 
     catch(Exception e) { 
      System.err.println(e); 
      e.printStackTrace(); 
     } 
     finally { 
      if (socket != null) {    
       try { 
        socket.close(); 
       } 
       catch (Exception e) { 
        System.err.println(e); 
        e.printStackTrace(); 
       }    
      } 
     } 
    } 

} 

所以現在我的代碼shuold通過看起來像這樣? 我因爲...尚未可以從一個客戶端發送到客戶端2

import java.awt.List; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.ServerSocket; 
import java.net.Socket; 
import java.util.ArrayList; 


public class server { 

    public static void main(String[] args) { 
     ArrayList<Channel> my_clients = new ArrayList<Channel>(); 
     ServerSocket ss = null; 

     try { 
      ss = new ServerSocket(7777);    

      while (true) { 
       //wait for a new client call - once got it, get the socket for 
       //that channel 

       System.out.println("Waiting for an incoming call"); 
       Socket client = ss.accept(); 
       Channel my_new_client = new Channel(client); 
       my_clients.add(my_new_client); 
       my_new_client.start(); 
       //once the call has started read the client data 
       for(Channel my_client : my_clients) { 
        if(my_client.getName() == "Me") { 
         //my_client.writer("HELLO!"); 

        } 
       } 
       //System.out.println("Accepted a new call");         
       //new Channel(client).start(); 

      } 
     } 
     catch(Exception e) { 
      System.err.println(e); 
      e.printStackTrace(); 
     } 
     finally { 
      if (ss != null) { 
       try { 
        ss.close(); 
       } 
       catch(Exception e) { 
        System.err.println(e); 
        e.printStackTrace(); 
       } 
      } 
     } 

    } 



    public static class Channel extends Thread { 

     private static int clientIndex = 0; 
     private int index; 

     private Socket socket = null; 

     public Channel(Socket socket) { 
      clientIndex++; 
      index = clientIndex; 
      this.socket = socket; 
     } 

     @Override 
     public void run() { 

      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
       PrintWriter writer = new PrintWriter(socket.getOutputStream()); 

       while (true) { 

        String input = reader.readLine();     
        System.out.println("Got from client "+index+": "+input); 

        //bye bye 
        if (input.equalsIgnoreCase("bye")) { 
         break; 
        } 
        writer.println("Gotcha"); 
        writer.flush();       
       } 
      } 
      catch(Exception e) { 
       System.err.println(e); 
       e.printStackTrace(); 
      } 
      finally { 
       if (socket != null) { 
        try { 
         socket.close(); 
        } 
        catch(Exception e) { 
         System.err.println(e); 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 

    } 

} 

回答

0

字符串userInput = readerFromCommandLine.readLine();

BufferedReader.readLine()在這裏是一個問題。它會阻止你的線程,直到收到輸入。這意味着溝通一次只能朝一個方向發展,如果兩個客戶都在等待,可能會完全阻止。

DataFetcher可以解決這個問題;你可以用它在一個單獨的線程

http://tus.svn.sourceforge.net/viewvc/tus/tjacobs/io/

0

有你一半聽。

您創建了一個Threaded Server,每個來自客戶端的連接都打開一個線程。此線程然後循環並等待消息。

將這些線程想象爲您將客戶端連接到自己的對象/屬性及其流以便寫入和讀取它們。

因此,每次客戶端連接你想創建他們的線程添加到某種列表並啓動他們的線程。例如:

在類

List<Channel> my_clients = new List<Channel>(); 

的頂部。在while循環

Channel my_new_client = new Channel(client); 
my_clients.add(my_new_client); 
my_new_client.start(); 

然後,當你想將消息發送到某些客戶端。您可以循環所有線程並查找具有某種名稱或唯一標識符的線程。例如:

for(Channel my_client : my_clients) { 
    if(my_client.getName() == "Me") { 
     my_client.write("HELLO!"); 
    } 
} 

或同日而語,你可以發送郵件到所有客戶端(廣播):

for(Channel my_client : my_clients) { 
    my_client.write("HELLO!"); 
} 

記得,當他們斷開太刪除客戶端!

// Can't remember the precise exception correct my if I'm wrong! 
catch(SocketException ex) { 
    my_clients.remove(this); 
} 

注意這個期望你一些如何鑑別和了解客戶的名稱,或者提供它們,你引用時,您將被要求把他們的東西UID。並且Channel類具有用於縱容的寫入方法。

希望幫助!

+0

嗨,我編輯服務器代碼就像你說的,但現在當我運行服務器,在此之後我運行客戶端,我寫嗨,在此之後,我再次運行客戶端我寫我客戶端二,但我不能不能返回客戶端並且回覆我想知道我做得不好的消息。並非常感謝你的幫助! – user1346532

+0

更新您的代碼我們可以看看? –

+0

我編輯樓上我的meessag與代碼在服務器端的新更新,因爲我不能複製過去的代碼在這裏,我不知道我可以在這裏複製代碼評論。 – user1346532

相關問題