TCP通讯

一,创建类

1.ChatSocketServer类

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.sendMsy();

}

/**

* 发送消息

*/

private void sendMsy(){

Scanner scanner =null;

PrintWriter pw =null;

try{

scanner =new Scanner(System.in);

pw =new PrintWriter(this.socket.getOutputStream());

while(true){

String str =scanner.nextLine();

pw.println(str);

pw.flush();

}

}catch (Exception e){

e.printStackTrace();

}finally {

if (scanner!=null){

scanner.close();

}

if (pw!=null){

pw.close();

}

if (socket!=null){

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

/**

* 接收消息的线程

*/

class receive extends Thread{

private Socket socket=null;

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 mr = br.readLine();

System.out.println("A说:"+mr);

}

}catch (Exception e){

e.printStackTrace();

}finally {

if (br!=null){

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

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.ChatSocketClient

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.PrintWriter;

import java.net.Socket;

import java.util.Scanner;

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 Clientreive(socket).start();

} catch (IOException e) {

e.printStackTrace();

}

}

}

/**

* 用于发送消息线程类

*/

class ClientSend extends Thread{

@Override

public void run() {

this.sendMsy();

}

private Socket socket;

public ClientSend(Socket socket){

this.socket =socket;

}

/**

* 发送消息

*/

private void sendMsy(){

Scanner scanner =null;

PrintWriter pw =null;

try{

scanner =new Scanner(System.in);

pw =new PrintWriter(this.socket.getOutputStream());

while(true){

String str =scanner.nextLine();

pw.println(str);

pw.flush();

}

}catch (Exception e){

e.printStackTrace();

}finally {

if (scanner!=null){

scanner.close();

}

if (pw!=null){

pw.close();

}

if (socket!=null){

try {

socket.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

/**

*用于接收消息线程类

*/

class Clientreive extends Thread{

private Socket socket=null;

public Clientreive(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 mr = br.readLine();

System.out.println("B说:"+mr);

}

}catch (Exception e){

e.printStackTrace();

}finally {

if (br!=null){

try {

br.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

三,结果(先服务 再客户)

相关推荐
笨蛋不要掉眼泪8 分钟前
deepseek封装结合websocket实现与ai对话
人工智能·websocket·网络协议
小李独爱秋3 小时前
计算机网络经典问题透视:当路由器需要同时连接以太网和ATM网络时,需要添加什么硬件?
运维·网络协议·计算机网络·网络安全·智能路由器
国服第二切图仔4 小时前
Rust开发之错误处理与日志记录结合(log crate使用)
网络·算法·rust
天***88964 小时前
驱动精灵、驱动人生、NVIDIA专业显卡驱动、360驱动大师、联想乐驱动,电脑驱动修复工具大全
网络·电脑·负载均衡
AORO20254 小时前
三防平板三防是指哪三防?适合应用在什么场景?
服务器·网络·智能手机·电脑·1024程序员节
王道长服务器 | 亚马逊云5 小时前
AWS + SeyouCMS:海外资源站的高性能部署实战
服务器·网络·数据库·云计算·软件构建·aws
泷羽Sec-静安5 小时前
Less-7 GET-Dump into outfile-String
android·前端·网络·sql·安全·web安全
一瓢一瓢的饮 alanchan5 小时前
HTTPS接口国密安全设计-示例
网络协议·安全·https·国密 国家商用密码·数字签名 数字证书·sm2 sm4 sm3·对称加密 非对称加密
2501_915909065 小时前
HTTPS 错误排查实战,从握手到应用层的工程化流程
网络协议·http·ios·小程序·https·uni-app·iphone
渡我白衣5 小时前
C++:链接的两难 —— ODR中的强与弱符号机制
开发语言·c++·人工智能·深度学习·网络协议·算法·机器学习