使用Socket实现一对一一对多聊天

用java中的Socket实现一对一一对多聊天,不过这个只是限制在局域网聊天。
下面代码是服务端Server和客户端Client的代码。
//Server
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Server {
    /**
     * @param args
     * @throws IOException 
     */
    static Scanner input = new Scanner(System.in);
    static List list = new ArrayList();
    static int count =0;
    public static void main(String[] args) throws IOException {
        //创建服务器
        ServerSocket server = new ServerSocket(9998);
        System.out.println("已经启动服务器...");
        while (true) {
            list.add(server.accept());
            System.out.println("有" + (count + 1)+ "个人连接了服务器!");
            //发信息(有多个人)
            if (count == 0) {
                new SendData().start();
            }
            //收信息
            new GetData(new DataInputStream(list.get(count).getInputStream())).start();
            count++;
        }
    }

    //给客户端发送信息
    public static class SendData extends Thread {
        private DataOutputStream dOutput;
        @Override
        public void run() {
            while (true) {
                try {
                    String msg =input.next();
                    for (Socket socket : list) {
                        dOutput=new DataOutputStream(socket.getOutputStream());
                        dOutput.writeUTF(msg);
                    }
                } catch (IOException e) {
                    break;
                }
            }
        }
    }

    //收取客户端发来的信息
    public static class GetData extends Thread {
        private DataInputStream dInput;
        public GetData(DataInputStream _dInput) {
            dInput=_dInput;
        }
        @Override
        public void run() {
            while (true) {
                try {
                    String msg = dInput.readUTF();
                    if (msg != null) {
                        System.out.println("客户端来信:" + msg);
                    }
                } catch (IOException e) {
                    try {
                        dInput.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    break;
                }
            }
        }
    }
}
//Client
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    static Scanner input = new Scanner(System.in);
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket socket = new Socket("127.0.0.1", 9998);
        System.out.println("已经连上了服务器!");

        new GetData(new DataInputStream(socket.getInputStream())).start();
        new SendData(new DataOutputStream(socket.getOutputStream())).start();
    }

    //收取服务端发来的信息
    public static class GetData extends Thread {
        private DataInputStream dInput;
        public GetData(DataInputStream _dInput) {
            dInput = _dInput;
        }
        @Override
        public void run() {
            while (true) {
                try {
                    String msg = dInput.readUTF();
                    if (msg != null) {
                        System.out.println("服务器来信:" + msg);
                    }
                } catch (IOException e) {
                    try {
                        dInput.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    //给服务端发送信息
    public static class SendData extends Thread {
        private DataOutputStream dOutput;
        public SendData(DataOutputStream _dOutput) {
            dOutput = _dOutput;
        }
        @Override
        public void run() {
            while (true) {
                try {
                    System.out.println("给服务器发信:");
                    String msg =input.next();
                    if (msg != null) {
                        dOutput.writeUTF(msg);
                    }
                } catch (IOException e) {
                    break;
                }
            }
        }
    }
}

你可能感兴趣的:(Java)