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

}

}

}

}

}

客户端代码: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();

}

}

}

}

}

运行服务端代码后,再运行客户端代码,即可实现一对一聊天。

结果:

相关推荐
IPFoxy66638 分钟前
探索路由器静态IP的获取方式
网络·智能路由器
menge23331 小时前
VLAN:虚拟局域网
网络·智能路由器
小鹿( ﹡ˆoˆ﹡ )1 小时前
探索IP协议的神秘面纱:Python中的网络通信
python·tcp/ip·php
ZachOn1y2 小时前
计算机网络:计算机网络概述 —— 初识计算机网络
网络·计算机网络·知识点汇总·考研必备
三金121382 小时前
SpringIoC容器的初识
网络·网络协议·rpc
韩楚风2 小时前
【linux 多进程并发】linux进程状态与生命周期各阶段转换,进程状态查看分析,助力高性能优化
linux·服务器·性能优化·架构·gnu
陈苏同学2 小时前
4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
linux·服务器·ide·人工智能·python·深度学习·pycharm
Pythonliu72 小时前
茴香豆 + Qwen-7B-Chat-Int8
linux·运维·服务器
我是哈哈hh3 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
郭二哈3 小时前
C++——模板进阶、继承
java·服务器·c++