聊天室(群聊功能)

服务器代码:

package main.demo;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TcpChatServer {
    public static void main(String[] args) throws IOException {

        ExecutorService es = Executors.newCachedThreadPool();
        ServerSocket lisenler = new ServerSocket(9988);
        System.out.println("聊天服务器已经开启");
        while(true){
            Socket socket = lisenler.accept();
            es.submit(new handel(socket));

        }


    }

     static class handel implements Runnable{
        private Socket socket;

         public handel(Socket socket) {
             this.socket = socket;
         }

         public handel() {
         }

         @Override
        public void run() {
             if (socket != null){
                 InputStream is = null;
                 BufferedReader br = null;
                 try {
                     is = socket.getInputStream();
                     br = new BufferedReader(new InputStreamReader(is,"utf-8"));
                     String data;
                     while((data = br.readLine())!= null) {
                         System.out.println(socket.getInetAddress().getHostAddress()+"说:"+data);
                     }
                     System.out.println(socket.getInetAddress().getHostAddress() +"退出了");
                 } catch (IOException e) {
                     e.printStackTrace();
                     System.out.println(socket.getInetAddress().getHostAddress() +"异常退出了");
                 }finally {
                     try {
                         br.close();
                         socket.close();
                     } catch (IOException e) {
                         e.printStackTrace();

                     }
                 }
             }





        }
    }
}

客户端代码:

package main.demo;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpChatCLient {
    public static void main(String[] args)throws Exception {
        Socket socket = new Socket("192.168.109.1",9988);
        OutputStream os = socket.getOutputStream();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"utf-8"));
        Scanner input = new Scanner(System.in);

        while(true){
            String next = input.next();
            bw.write(next);
            bw.newLine();
            bw.flush();
            if (next.equals("886")){
                break;
            }

        }

        bw.close();
        socket.close();

    }
}

聊天效果:
服务器端:
在这里插入图片描述
客户端:
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值