2016-04-29 67 views
0

我有2個班消息給特定的客戶端使用套接字

public class ServerStart implements Runnable 
    { 
     @Override 
     public void run() 
     { 
      try 
      { 
       serverSock = new ServerSocket(2101); 
       while (true) 
       { 
        sock = serverSock.accept(); 
        System.out.println(sock); 
        clients.put(sock.getPort(),sock); 
        HandleMultipleClients hmc=new HandleMultipleClients(); 
        hmc.messagetospecificclients(String ipaddress,String choice) 
       } 

第二類是

public class HandleMultipleClients 
{ 

    Socket soc; 
    ServerSocket serverSock; 
    DataOutputStream dos; 
    DataInputStream dis; 
    public HandleMultipleClients() 
    { 

    } 
    public void messagetospecificclients(String ipaddress,String choice) throws IOException, InterruptedException 
    { 
     System.out.print(ipaddress+"\n"+choice); 
     for (Iterator<Integer> iter = clients.keySet().iterator(); iter.hasNext();) 
     { 
      System.out.print("ok1"); 
      int key = iter.next(); 
      java.net.Socket client = clients.get(key); 
      InetAddress zee = client.getInetAddress(); 
      String s = zee.getHostAddress(); 
      System.out.print(s); 
      if (zee.getHostAddress().equals(ipaddress)) 
      { 
       System.out.print("ok2"); 
       dos =new DataOutputStream(client.getOutputStream()); 
       dos.writeUTF(choice); 
      } 

我怎樣才能獲得通過的第2類功能,即messagetospecificclients的循環(字符串ip地址,字符串的選擇)當我將客戶添加到第一堂課時,請幫助我。我的代碼應該是這樣的,我應該添加客戶端到第一類,並應遍歷第二類的for循環

回答

0

我會重構代碼。分離套接字服務器的機制代碼和處理返回數據。返回數據代碼可以從messageToSpecificClient方法啓動。你可以啓動一整堂課或其他任何東西。 我沒有運行這個,但它應該運行。 關於這個代碼的另一個警告是它會有一些unicode的問題。 所以,如果這是你的需求,將需要改變一些。

import java.io.IOException; 
import java.io.InputStream; 
import java.io.PrintStream; 
import java.net.InetAddress; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class MultiThreadServer implements Runnable 
{ 
    Socket csocket; 

    MultiThreadServer(Socket csocket) 
    { 
     this.csocket = csocket; 
    } 

    public static void main(String args[]) throws Exception 
    { 
     ServerSocket ssock = new ServerSocket(1234); 
     System.out.println("Listening"); 
     while (true) 
     { 
      Socket sock = ssock.accept(); 
      System.out.println("Connected"); 
      new Thread(new MultiThreadServer(sock)).start(); 
     } 
    } 

    public void run() 
    { 
     PrintStream pstream = null; 
     InputStream input = null; 
     try 
     { 
      pstream = new PrintStream(csocket.getOutputStream()); 
      input = csocket.getInputStream(); 
      String stringFromClient = readFromInputStream(input); 
      String response = messageTosSpecificClients(stringFromClient); 
      pstream.write(response.getBytes()); 
     } 
     catch (IOException e) 
     { 
      System.out.println(e); 
     } 
     finally 
     { 
      if (pstream != null) 
       try 
       { 
        pstream.close(); 
       } 
       catch (Throwable t) 
       { 
       } 
      if (input != null) 
       try 
       { 
        input.close(); 
       } 
       catch (Throwable t) 
       { 
       } 
      if (csocket != null) 
       try 
       { 
        csocket.close(); 
       } 
       catch (Throwable t) 
       { 
       } 
     } 
    } 

    String readFromInputStream(InputStream inputStream) throws IOException 
    { 
     int ch; 
     StringBuilder sb = new StringBuilder(); 
     while ((ch = inputStream.read()) != -1) 
      sb.append((char) ch); 
     return sb.toString(); 
    } 

    public String messageTosSpecificClients(String choice) throws IOException 
    { 
     String ipaddress = "127.0.0.1"; 

     String retData = "General Return String"; 
     System.out.print(ipaddress + "\n" + choice); 
     InetAddress zee = csocket.getInetAddress(); 
     String s = zee.getHostAddress(); 
     System.out.print(s); 
     if (zee.getHostAddress().equals(ipaddress)) 
     { 
      retData = "Specific Return String"; 
     } 
     return retData; 
    } 
} 
相關問題