流式套接字:基于TCP协议的Socket网络编程(案例2)

时间:2021-11-18 10:21:33

案例:在案例1的基础上实现一个服务器对应多个客户端(多线程),且获得每个客户端的IP。

线程代码:

package com.yh.mySocket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

public class LoginThread extends Thread {
    Socket socket = null;
    public LoginThread() {}
    public LoginThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            // 获得输入流,获得相应的用户请求
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info;
            while((info = br.readLine())!=null) {
                System.out.println(info);
            }
            // 获得客户端IP
            InetAddress ia = socket.getInetAddress();
            String ip = ia.getHostAddress();
            System.out.println("客户端IP地址为:"+ip);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

客户端代码(多个客户端代码类似):

package com.yh.mySocket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class LoginClient1 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            // 创建一个客户端Socket,参数为客户端IP和指定端口号,此处客户端和服务器在同一台电脑,所以IP地址相同
            socket = new Socket("localhost",5000);
            // 通过输出流,给服务器发送请求
            os = socket.getOutputStream();
            String info = "用户名:YeHuan1; 密码:12345";
            byte[] infos = info.getBytes();
            os.write(infos);
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                os.close();
                socket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

服务器代码:

package com.yh.mySocket;

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

public class LoginServer {
    public static void main(String[] args) {
        ServerSocket serversocket = null;
        Socket socket = null;
        
        try {
            // 创建一个服务器Socket
            serversocket = new ServerSocket(5000);
            while(true) {
                // 使用accept()等待用户的通信
                socket = serversocket.accept();
                LoginThread lt = new LoginThread(socket);
                lt.start();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                socket.close();
                serversocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}