JS 与 trick 代码的运用及解析全攻略
757
2022-11-05
TCP点对点通信(多线程场景下)
1.创建服务端
package com.yqq.app2;/** * @Author yqq * @Date 2021/11/07 22:39 * @Version 1.0 */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java-.ServerSocket;import java-.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;/** * @Author yqq * @Date 2021/11/07 23:08 * @Version 1.0 */import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java-.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.通信记录
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~