栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java socket聊天室

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java socket聊天室

服务端

import javax.naming.ldap.SortKey;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Scanner;

public class Server_Chat {

    private ServerSocket serverSocket = null;

    public Server_Chat(int port) throws IOException {
        serverSocket = new ServerSocket(port);

    }

    public void start() throws IOException, ClassNotFoundException {
        Socket socket = connection();
        User user = getUserMsg(socket);
        sendUserToClient(socket,user);
        printUser(user);

        while (true) {
            Scanner scanner = new Scanner(System.in);
            receiveMsg(socket);
            sendMsg(socket, scanner.next());
        }
    }

    public void sendUserToClient(Socket socket,User user) throws IOException {
        ChatUtils.sendUserToClient(socket,user);
    }

    public void printUser(User user) {
        System.out.println(user + " 上线了");
    }

    public User getUserMsg(Socket socket) {
        String ip = socket.getInetAddress().getHostAddress();
        int port = socket.getPort();
        return new User(ip,port);
    }

    public void receiveMsg(Socket socket) throws IOException {
        User userMsg = getUserMsg(socket);
        String msg = ChatUtils.receiveMsg(socket);
        System.out.println(userMsg +" : "+ msg);
    }

    public void sendMsg(Socket socket,String msg) throws IOException {
        ChatUtils.sendMsg(socket, msg);
    }

    public void sendChatType(Socket socket,ChatType chatType) throws IOException {
        ChatUtils.sendChatType(socket,chatType);
    }

    public ChatType receiveChatType(Socket socket) throws IOException, ClassNotFoundException {
        return ChatUtils.receiveChatType(socket);
    }

    public Socket connection(){
        try {
            Socket accept = serverSocket.accept();
            return accept;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

客户端

import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Client_Chat {
    private String ip;
    private int port;

    public Client_Chat() throws IOException {
    }

    public void start() throws Exception {
        Socket socket = connection("127.0.0.1",6666);
        User user = receiveUserToClient(socket);
        printUser(user);

        while (true) {
            Scanner scanner = new Scanner(System.in);
            sendMsg(socket, scanner.next());
            System.out.println(user +" : "+receiveMsg(socket));
        }
    }

    public User receiveUserToClient(Socket socket) throws Exception {
        User user = ChatUtils.receiveUserToClient(socket);
        return user;
    }

    public void printUser(User user) {
        System.out.println(user + "上线了");
    }

    public Socket connection(String ip,int port) throws IOException {
        return new Socket(ip,port);
    }

    public void sendMsg(Socket socket,String msg) throws IOException {
        ChatUtils.sendMsg(socket, msg);
    }

    public String receiveMsg(Socket socket) throws IOException {
        return ChatUtils.receiveMsg(socket);
    }

    public void sendChatType(Socket socket,ChatType chatType) throws IOException {
        ChatUtils.sendChatType(socket,chatType);
    }

    public ChatType receiveChatType(Socket socket) throws IOException, ClassNotFoundException {
        return ChatUtils.receiveChatType(socket);
    }
}

信息收发工具类

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class ChatUtils {

    public static void sendMsg(Socket socket,String msg) throws IOException {
        OutputStream outputStream = socket.getOutputStream();
        byte[] bytes = msg.getBytes();
        outputStream.write(msg.getBytes(StandardCharsets.UTF_8));
    }

    public static String receiveMsg(Socket socket) throws IOException {
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = inputStream.read(bytes);
        return new String(bytes,0,read);
    }

    public static void sendUserToClient(Socket socket,User user) throws IOException {
        ObjectOutputStream oop = new ObjectOutputStream(socket.getOutputStream());
        oop.writeObject(user);
    }

    public static User receiveUserToClient(Socket socket) throws Exception {
        ObjectInputStream oip = new ObjectInputStream(socket.getInputStream());
        return (User) oip.readObject();
    }

    public static void sendChatType(Socket socket,ChatType chatType) throws IOException {
        ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
        oos.writeObject(chatType);
    }

    public static ChatType receiveChatType(Socket socket) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        return (ChatType)ois.readObject();
    }
}

效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/293443.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号