Java多客户端聊天服务器

时间:2017-02-16 18:12:53

标签: java server client chat

我是Java编程的新手,正在尝试开发多客户端聊天服务器。

以下是我的代码:

package Server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

public class server implements Runnable{

    private List<ServerClient> clients = new ArrayList<ServerClient>();

//private String UID;
private int port;
private ServerSocket socket;
private Thread run, manage, send, receive;
private boolean running = false;
public int i=0;

    public server(int port){
//      this.UID = UID;
        this.port = port;
        try {
            socket = new ServerSocket(port);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        run = new Thread(this, "Server");
        run.start();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        running = true;
        ManageClients();
        receive();
    }
    private void ManageClients(){
        manage = new Thread("Manage"){
            public void run(){
                while(running){
                    //managing
                }
            }
        };
        manage.start();
    }

    private void receive(){
        receive = new Thread("Receive"){
            public void run(){
                Socket S1 = null;
                try {
                    S1 = socket.accept();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                while(running){

/*                  byte [] data = new byte[1024];
                    DatagramPacket packet = new DatagramPacket(data, data.length);
                    try {
                        socket.receive(packet);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    String string = new String(packet.getData());
                    System.out.println(string);*/
/*                  BufferedReader br = null;
                    try {
                        br = new BufferedReader(new InputStreamReader(S1.getInputStream()));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    try {
                        String string = br.readLine();
                        System.out.println(string);
                        process(string);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }*/
                    process(S1);
                    System.out.println(String.valueOf(i));
                    i++;
                }
            }
        };
        receive.start();
    }
    private void process(Socket S1){
        BufferedReader br = null;
        String string = "";
        try {
            br = new BufferedReader(new InputStreamReader(S1.getInputStream()));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            string = br.readLine();
            System.out.println(string);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if(string.startsWith("/c/")){
            clients.add(new ServerClient("name", S1.getInetAddress(), S1.getPort(), "UID"));
        }
        else if(string.startsWith("/ip/")){
            //send ip address to raspberry
            System.out.println(string);
        }
        else if(string.startsWith("/r/")){
            //send relay command to relay
            System.out.println(string);
        }
    }
}

服务器只接受一个连接。如果我尝试连接其他用户,则控制台不会显示任何输出。

请你指导我出错的地方。

谢谢

1 个答案:

答案 0 :(得分:0)

您需要为每个想要连接的客户端多次运行accept方法...一次。通常人们在while循环中的单独线程上执行此操作,因为accept方法正在阻止。

在您的代码中,您似乎只调用accept一次,因此您只能获得一个客户端连接。

我建议你在这里更改你的代码架构,但是如果你想保持它相同,并且你知道有多少客户端将连接,那么你可以在同步锁中包含对socket的访问,并调用你的{每个客户一次{1}}方法。