如何让服务器将收到的消息从任何客户端转发到java

时间:2016-05-10 13:05:55

标签: multithreading client-server

我试图使用java制作一个聊天系统项目(仍然不完整),但我需要很小的帮助才能转发客户收到的消息。

服务器正在作为多线程工作,所以很多客户端可以连接到它,我想要的是:(假设场景)如果6个客户端连接到服务器,然后其中一个发送消息,它应该被转发到通过服务器连接所有客户端。

这是2个代码..

服务器端

import java.io.*;
import java.net.*;
public class ChatServer implements Runnable
{
    Socket csocket;
    ChatServer(Socket csocket){ this.csocket = csocket; }

public static void main(String[]args)throws Exception
{
    ServerSocket sersock=new ServerSocket(3000);
    System.out.println("Server  ready for chatting");
    while(true)
    {
        Socket sock =sersock.accept();
        new Thread(new ChatServer(sock)).start();}
}
public void run()
{
    try{
            System.out.println(Thread.currentThread().getName() + ": HELLO");
            BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
            OutputStream ostream=csocket.getOutputStream();
            PrintWriter pwrite=new PrintWriter(ostream, true);
            InputStream istream=csocket.getInputStream();
            BufferedReader receiveRead=new BufferedReader(new InputStreamReader(istream));
            String receiveMessage, sendMessage;

            while(true) {
                    if((receiveMessage=receiveRead.readLine())!=null)
                        {
                            System.out.print(Thread.currentThread().getName() + ": ");
                            if(receiveMessage.equals("QUIT"))
                                Thread.currentThread().stop();
                                else
                                    System.out.println(receiveMessage);}
                        sendMessage=keyRead.readLine(); 
                        pwrite.println(sendMessage); 
                        System.out.flush();}
        } catch (IOException e){ System.out.println(e); }

}

}

客户端

import java.io.*;
import java.net.*;
public class ChatClient
{
public static void main(String[]args)throws Exception
{
Socket sock =new Socket("localhost", 3000);
// reading from keyboard (keyRead object)
BufferedReader keyRead=new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream=sock.getOutputStream();
PrintWriter pwrite=new PrintWriter(ostream, true);

// receiving from server ( receiveRead  object)
InputStream istream=sock.getInputStream();
BufferedReader receiveRead=new BufferedReader(new InputStreamReader(istream));

System.out.println("Start the chitchat, type and press Enter key");

String receiveMessage, sendMessage;
while(true)
{
sendMessage=keyRead.readLine();// keyboard reading
pwrite.println(sendMessage);// sending to server
System.out.flush();// flush the data
    if((receiveMessage=receiveRead.readLine())!=null)//receive from server
{
System.out.println(receiveMessage);// displaying at DOS prompt
}
}
}
}

提前致谢。

1 个答案:

答案 0 :(得分:0)

我经常编写这些类型的服务器应用程序,作为连接客户端通过中继服务器进行直接连接时的一种方式。解决方案很简单,将您从socket函数收到的每个accept()放入您选择的列表或树中。

List<Socket> connectionList = new ArrayList<Socket>();
...
Socket sock =sersock.accept();
connectionList.add(sock);
...
for (Socket connection : connectionList) {
      connection.getOutputStream().write(msgBytes);
}