多线程通信之客户端与服务器端


import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class LoginServer {//多线程处理客户端

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        Socket socket;

        System.out.println("服务器等待请求。。。。");
        while (true) {//实现一直监听
            socket = serverSocket.accept();
            System.out.println("服务器接收到请求");
            //多线程
            LoginThread thread = new LoginThread(socket);
            thread.start();
        }
    }
}

 

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class LoginClient1 {
    public static void main(String[] args) throws IOException {
        User user = new User("张三", "123");

        Socket socket = new Socket(InetAddress.getLocalHost(), 9000);

        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();

        ObjectOutputStream oos = new ObjectOutputStream(os);
        ObjectInputStream ois = new ObjectInputStream(is);

        System.out.println("我是客户端1,给服务器发送登录信息。。。");
        oos.writeObject(user);
        //告诉服务已经发送完了消息
        socket.shutdownOutput();
        //接收服务器的信息
        byte[] temp = new byte[1024];
        int len = is.read(temp);
        System.out.println("服务器的响应"+new String(temp,0,len));
        socket.close();
    }
}

 

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;

public class LoginClient2 {
    public static void main(String[] args) throws IOException {
        User user = new User("李四", "123");
        Socket socket = new Socket(InetAddress.getLocalHost(), 9000);
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);

        System.out.println("我是客户端2,给服务器发送信息");
        oos.writeObject(user);
        //告诉服务已经发送完了消息
        socket.shutdownOutput();
        //接收服务器的信息
        byte[] temp = new byte[1024];
        int len = is.read(temp);
        System.out.println("服务器的响应"+new String(temp,0,len));
        socket.close();
    }
}

 

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class LoginThread extends Thread {

    private Socket socket;

    public LoginThread(Socket socket) {
        this.socket = socket;
    }


    @Override
    public void run() {
        //处理客户端请求
        try (//写在小括号里会自动关闭资源close
                InputStream is = socket.getInputStream();
                OutputStream os = socket.getOutputStream();
                ObjectOutputStream oos = new ObjectOutputStream(os);
                ObjectInputStream ois = new ObjectInputStream(is);
        ) {

            //得到客户端的信息
            User user = (User) ois.readObject();
            //给客户端回信息
            String msg = null;
            if (user == null) {
                msg = "没有发送登录信息,登录失败";
            } else {
                msg = "欢迎你" + user.getUsername();
            }
            System.out.println(msg);

            os.write(msg.getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

 

原创文章,作者:端木书台,如若转载,请注明出处:https://blog.ytso.com/275472.html

(0)
上一篇 2022年7月19日
下一篇 2022年7月19日

相关推荐

发表回复

登录后才能评论