1.创建服务端 package com . yqq . app2 ; /** * @Author yqq * @Date 2021/11/07 22:39 * @Version 1.0 */ import java . io . BufferedReader ; import java . io . IOException ; import java . io . InputStreamReader ; import java . io . PrintWr
1.创建服务端
package com.yqq.app2;/**
* @Author yqq
* @Date 2021/11/07 22:39
* @Version 1.0
*/
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 Receive extends Thread{
private Socket socket;
public Receive(Socket socket){
this.socket=socket;
}
public void run() {
this.receiveMsg();
}
/**
* 用于接受对象消息的方法
*/
private void receiveMsg(){
BufferedReader bf =null;
try {
//创建用于接受对方发送的消息流对象
bf=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
while(true){
String msg = bf.readLine();
System.out.println("他说:"+msg);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(bf!=null){
try {
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* 发送消息线程
*/
class Send extends Thread{
private Socket socket;
public Send(Socket socket){
this.socket=socket;
}
public void run() {
this.sendMsg();
}
/**
* 发送消息
*/
private void sendMsg(){
Scanner scanner = null;
PrintWriter pw = null;
try{
scanner = new Scanner(System.in);
//创建向对方输出消息的流对象
pw=new PrintWriter(this.socket.getOutputStream());
while (true){
String msg = scanner.nextLine();
pw.println(msg);
pw.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(scanner!=null){
scanner.close();
}
if(pw!=null){
pw.close();
}
}
}
}
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.创建客户端
package com.yqq.app2;/**
* @Author yqq
* @Date 2021/11/07 23:08
* @Version 1.0
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* 用于发送消息的线程类
*/
class ClientSend extends Thread{
private Socket socket;
public ClientSend(Socket socket){
this.socket=socket;
}
public void run() {
this.sendMsg();
}
/**
* 发送消息
*/
private void sendMsg(){
Scanner scanner = null;
PrintWriter pw = null;
try{
scanner = new Scanner(System.in);
//创建向对方输出消息的流对象
pw=new PrintWriter(this.socket.getOutputStream());
while (true){
String msg = scanner.nextLine();
pw.println(msg);
pw.flush();
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(scanner!=null){
scanner.close();
}
if(pw!=null){
pw.close();
}
}
}
}
/**
* 接受消息的线程
*/
class ClientReceive extends Thread{
private Socket socket;
public ClientReceive(Socket socket){
this.socket=socket;
}
public void run() {
this.receiveMsg();
}
/**
* 用于接受对象消息的方法
*/
private void receiveMsg(){
BufferedReader bf =null;
try {
//创建用于接受对方发送的消息流对象
bf=new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
while(true){
String msg = bf.readLine();
System.out.println("他说:"+msg);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(bf!=null){
try {
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
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 ClientReceive(socket).start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.通信记录