2016-04-29 108 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; 
    } 
} 
相关问题