Chat聊天 - 服务器端

引用
ChatServer.java

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务器端
 * @author Administrator
 */
public class ChatServer {

    private ServerSocket ss = null;
    //解决空指针问题;
    private boolean started = false;
    //使用了乏型把每一个客户端放在一个数组里面;
    List<Client> lists = new ArrayList<Client>();

    public static void main(String[] args) {
        new ChatServer().start();
    }

    //连接客户端
    public void start() {
        try {
            ss = new ServerSocket(8888);
            started = true;
        } catch (BindException e) {
            System.out.println("Server is being used!");
            System.out.println("Please shutdown Server!");
            System.exit(0);
        } catch (IOException ex) {
            ex.printStackTrace();
        }


        try {
            while (started) {
                Socket s = ss.accept();
                Client c = new Client(s);
                new Thread(c).start();
                //把客户端放进数组里;
                lists.add(c);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                try {
                    ss.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    }

    /**
     * 1、接收客户端发送过来的消息;
     * 2、将客户端发送过来的每条消息再发送给每个客户端;
     */
    public class Client implements Runnable {

        private Socket s;
        private DataInputStream dis = null;
        private DataOutputStream dos = null;
        private boolean bconnect = false;

        //取得输入,输出流;
        private Client(Socket s) {
            this.s = s;
            try {
                System.out.println("a client connected!");
                dis = new DataInputStream(s.getInputStream());
                dos = new DataOutputStream(s.getOutputStream());
                bconnect = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //将客户端发过来的消息转发给每一个客户端;
        public void send(String str) {
            try {
                dos.writeUTF(str);
                dos.flush();               
            } catch (IOException e) {
                lists.remove(this);
                System.out.println("a client quit!");
            }
        }

        public void run() {
            try {
                while (bconnect) {
                    String str = dis.readUTF();//是阻塞式的
System.out.println(str);
                    for (int i = 0; i < lists.size(); i++) {
                        Client c = lists.get(i);
                        c.send(str);
                    }
                    /*// Iterator有锁的概念;
                    for(Iterator<Client> it = lists.iterator(); it.hasNext(); ) {
                    Client c = it.next();
                    c.send(str);
                    }
                    Iterator<Client> it = lists.iterator();
                    while(it.hasNext()) {
                    Client c = it.next();
                    c.send(str);
                    }
                     */
                }
            } catch (EOFException e) {
                System.out.println("Client to close!");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (dos != null) {
                        dos.close();
                    }
                    if (dis != null) {
                        dis.close();
                    }
                    if (s != null) {
                        s.close();
                    }
                } catch (SocketException e) {
                    System.out.println("System exit!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

你可能感兴趣的:(java,C++,c,socket,dos)