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();
    }
}

效果
请添加图片描述
请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值