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


    案例:在案例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();
                }
            }
        }
    }
  • 相关阅读:
    CodeIgniter(3.1.4)框架中成功/错误跳转
    CodeIgniter(3.1.4)框架-url重写,去除index.php
    CodeIgniter(3.1.4)框架中-使用多个公共控制器
    CodeIgniter(3.1.4)框架使用静态文件(js,css)
    PHP无限极分类
    PHP无限极分类
    spring中定时任务quartz2.2.3
    eclipse中git更新操作
    java中日期的换算处理
    使用spring-rabbit测试RabbitMQ消息确认(发送确认,接收确认)
  • 原文地址:https://www.cnblogs.com/YeHuan/p/10792905.html
Copyright © 2020-2023  润新知