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

TCP通信的实现点对点聊天

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

TCP通信的实现点对点聊天

服务端的创建

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;



 class  Send extends Thread{
     private Socket socket;
     public Send(Socket socket){
         this.socket=socket;
     }
    @Override
    public void run() {
      this.sendMsg();
    }

    

    private void sendMsg() {
        Scanner scanner =null;
        PrintWriter pw =null;
        try{
            //创建Scanner对象

            scanner  =new Scanner(System.in);

            //创建向对方输出消息的对象

            pw =new PrintWriter(this.socket.getOutputStream());
            while(true){
                String msg =scanner.nextLine();
                pw.println(msg);
                pw.flush();
            }
        }catch(Exception  e){
            e.printStackTrace();
        }finally{
            if(scanner!=null){
                scanner.close();
            }
            if(pw!=null){
                pw.close();
            }
            if(this.socket!=null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    }

class Receive extends Thread{
    private Socket socket;
    public Receive(Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        this.receiveMsg();

    }

    

    private void receiveMsg() {
        BufferedReader br =null;
        try{
            //创建用于接收对方消息的流对象
            br=new BufferedReader(new
                    InputStreamReader(this.socket.getInputStream()));
            while(true){
                String msg =br.readLine();

                System.out.println("他说:"+msg);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


        }
    }
}
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public   class   ChatSocketServer {
    public static void main(String[] args) {
        ServerSocket serverSocket=null;
        try{
            serverSocket=new ServerSocket(8888);
            System.out.println("服务端启动,等待连接。。。");

            Socket socket=serverSocket.accept();

            System.out.println("连接成功");

            new Send(socket).start();
            new Receive(socket).start();
        }catch(Exception e ){
            e.printStackTrace();
        }finally{
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}

客户端的创建

import java.net.Socket;

public class ChatSocketClient {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1", 8888);

            System.out.println("连接成功!");

            new Send(socket).start();
            new Receive(socket).start();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

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

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

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