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

TCP点对点通信(多线程场景下)

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

TCP点对点通信(多线程场景下)

1.创建服务端
package com.yqq.app2;



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 Receive extends Thread{
    private Socket socket;
    public Receive(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.receiveMsg();
    }
    
    private void receiveMsg(){
        BufferedReader bf =null;
        try {
            //创建用于接受对方发送的消息流对象
            bf=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while(true){
                String msg = bf.readLine();
                System.out.println("他说:"+msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(bf!=null){
                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


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

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

2.创建客户端
package com.yqq.app2;



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


class ClientSend extends Thread{
    private Socket socket;
    public ClientSend(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.sendMsg();
    }
    
    private void sendMsg(){
        Scanner scanner = null;
        PrintWriter pw = null;
        try{
            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();
            }
        }
    }
}


class ClientReceive extends Thread{
    private Socket socket;
    public ClientReceive(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        this.receiveMsg();
    }
    
    private void receiveMsg(){
        BufferedReader bf =null;
        try {
            //创建用于接受对方发送的消息流对象
            bf=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while(true){
                String msg = bf.readLine();
                System.out.println("他说:"+msg);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(bf!=null){
                try {
                    bf.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

public class ChatSocketClient {
    public static void main(String[] args) {
        try {
            Socket socket =new Socket("127.0.0.1",8888);
            System.out.println("连接成功");
            new ClientSend(socket).start();
            new ClientReceive(socket).start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

3.通信记录


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

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

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