2012-05-18 151 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

相关问题