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

}

}

}

}

}

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

结果:

相关推荐
远游客07134 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
fantasy_arch5 小时前
CPU性能优化-磁盘空间和解析时间
网络·性能优化
LIKEYYLL6 小时前
GNU Octave:特性、使用案例、工具箱、环境与界面
服务器·gnu
云云3216 小时前
搭建云手机平台的技术要求?
服务器·线性代数·安全·智能手机·矩阵
云云3217 小时前
云手机有哪些用途?云手机选择推荐
服务器·线性代数·安全·智能手机·矩阵
CircleMouse7 小时前
Centos7, 使用yum工具,出现 Could not resolve host: mirrorlist.centos.org
linux·运维·服务器·centos
是Dream呀7 小时前
Python从0到100(七十八):神经网络--从0开始搭建全连接网络和CNN网络
网络·python·神经网络
木子Linux8 小时前
【Linux打怪升级记 | 问题01】安装Linux系统忘记设置时区怎么办?3个方法教你回到东八区
linux·运维·服务器·centos·云计算
kaixin_learn_qt_ing8 小时前
了解RPC
网络·网络协议·rpc
不惑_8 小时前
小白入门 · 腾讯云轻量服务器部署 Hadoop 3.3.6
服务器·hadoop·腾讯云