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

}

}

}

}

}

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

结果:

相关推荐
傻啦嘿哟4 分钟前
开发者视角:应用程序中HTTP代理的集成指南
网络·网络协议·http
爱写代码的小朋友20 分钟前
IP 地址规划中的子网划分:/18 网络容纳 64 个 C 段(/24)的原理与应用解析
网络·网络协议·tcp/ip
十五年专注C++开发1 小时前
WebSocket原理详解(二)
网络·websocket·网络协议
阳区欠1 小时前
【Linux】进程通信
linux·运维·服务器·共享内存·进程通信·system v·管道文件
may_一一2 小时前
终端SSH连接工具SecureCRT安装和连接Linux
运维·服务器·ssh
蜗牛沐雨2 小时前
MCP协议的Streamable HTTP:革新数据传输的未来
网络·网络协议·http
金丝猴也是猿2 小时前
如何在Flutter上实现高性能的动态模板渲染
websocket·网络协议·tcp/ip·http·网络安全·https·udp
swift开发pk OC开发2 小时前
Flutter性能优化细节
websocket·网络协议·tcp/ip·http·网络安全·https·udp
网络抓包与爬虫2 小时前
Flutter 环境搭建、常用指令、开发细节
websocket·网络协议·tcp/ip·http·网络安全·https·udp
厦门德仔3 小时前
【C#】C#字符串拼接的6种方式及其性能分析对比
服务器·windows·c#