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

}

}

}

}

}

相关推荐
m0_6090004244 分钟前
向日葵好用吗?4款稳定的远程控制软件推荐。
运维·服务器·网络·人工智能·远程工作
suifen_4 小时前
RK3229_Android9.0_Box 4G模块EC200A调试
网络
铁松溜达py4 小时前
编译器/工具链环境:GCC vs LLVM/Clang,MSVCRT vs UCRT
开发语言·网络
衍生星球8 小时前
【网络安全】对称密码体制
网络·安全·网络安全·密码学·对称密码
掘根8 小时前
【网络】高级IO——poll版本TCP服务器
网络·数据库·sql·网络协议·tcp/ip·mysql·网络安全
友友马9 小时前
『 Linux 』HTTP(一)
linux·运维·服务器·网络·c++·tcp/ip·http
2401_872514979 小时前
深入探究HTTP网络协议栈:互联网通信的基石
网络·网络协议·http
chenjingming66610 小时前
windows使用tcpdump.exe工具进行抓包教程
网络·测试工具·tcpdump
初黑子zzz10 小时前
rsync
网络