TCP点对点聊天通信

1.创建服务端

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.sendMsg();
    }
//发送消息
private void sendMsg() {
    Scanner scanner = null;
    PrintWriter pw = null;
    try {
        //创建 Scanner 对象
        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 (this.socket != null) {
            try {
                this.socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (pw != null) {
            pw.close();
        }
    }
}}

/*** 接收消息的线程 */
class Receive extends Thread{
    private Socket socket;

    public  Receive(Socket socket){

        this.socket=socket;
    }
    @Override
    public void run(){

        this.receiveMsg();
    }
    //用于接收对方消息的方法
    public void receiveMsg(){
        BufferedReader br =null;
        try{
            //创建用于接收对方发送消息的流对象
            br =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while(true){
                String msg = br.readLine();
                System.out.println("他说:"+msg);
            }

        }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;
        Socket socket=null;
        try {
            serverSocket=new ServerSocket(8888);
            System.out.println("服务器启动,等待连接!");
            socket=serverSocket.accept();
            System.out.println("连接成功!");
            new Send(socket).start();
            new Receive(socket).start();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2,创建客户端

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;
    }
    @Override
    public void run() {
        this.sendMsg();
    }
    /**
     * 发送消息
     */
    private void sendMsg(){
        Scanner scanner = null;
        PrintWriter pw = null;
        try{
            //创建 Scanner 对象
            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();
            }
            if(this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
/**
 * 用于接收消息的线程类
 */
class ClientReceive extends Thread{
    private Socket socket;
    public ClientReceive(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 msg = br.readLine();
                System.out.println("他说:"+msg);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(br != null){
                try {
                    br.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(Exception e){
            e.printStackTrace();
        }
    }
}

3,运行如图:

 刚开始的时候,能够运行,在提示连接成功后,给报了java.lang.NullPointerException at com.wlbc.Receive.receiveMsg(ChatSocketServer.java:74) at com.wlbc.Receive.run(ChatSocketServer.java:67   .//其实刚开始我也不知道为啥会标红?但后期重新运行的时候又能够正常运行,希望得到大佬指点!谢谢!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值