zoukankan      html  css  js  c++  java
  • 使用多线程完成Socket

    public class Service {  //服务器
        
        public static void main(String[] args) {
            ServerSocket serverSocket=null;
            Socket socket=null;
            try {
                //创建一个超市
                serverSocket=new ServerSocket(8800);
                while(true){
                //超市开门 等待顾客上门购物  怎么保证超市是24消失营业的??
                 socket = serverSocket.accept();
                //使用多线程来实现多个顾客能同时购物 同时结账
                 ServiceThread thread=new ServiceThread(socket);
                  thread.start();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
        }
        
        
    }
    服务器端代码
    public class ServiceThread extends Thread { 
    
        //没启动一个线程 就相当于有一个顾客 进入 超市
        Socket socket=null;
        
        public ServiceThread(Socket socket) {
            this.socket=socket;
        }
    
        @Override
        public void run() {
            InputStream is=null;
            OutputStream  os=null;
            ObjectInputStream ois=null;  //反序列化 
              try {
                  //拿出钱包,推上购物车
                is=socket.getInputStream();
                os=socket.getOutputStream();
                //反序列化 获取  顾客的信息
                ois=new ObjectInputStream(is);
                User user=(User) ois.readObject();  //读到进入超市的顾客信息
                if (user!=null) {
                    System.out.println("服务器说:您的姓名是:"+user.getUserName());
                }
                //给顾客一个回应
                os.write("欢迎您的光临".getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }finally{
                    try {
                        os.close();
                        ois.close();
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                
            }
            
            
        }
        
        
    }
    服务器需要的线程类
    public class Client1 {  //第一个顾客
    
        public static void main(String[] args) {
            Socket socket=null;
            InputStream  is=null;
            OutputStream os=null;
            ObjectOutputStream oos=null;
            //接收服务器的信息   读  
            BufferedReader  br=null;
            
            
            try {
                //进入了我们指定的  超市购物
                socket=new Socket("localhost", 8800);
                is=socket.getInputStream();
                os=socket.getOutputStream();
                //序列化对象
                oos=new ObjectOutputStream(os);
                User user=new User("小黑黑1", "admin");
                oos.writeObject(user);
                //购物完毕  shutdownOutput  与 close
                socket.shutdownOutput();
                
                //接收到服务器给你说的 欢迎光临
                br=new BufferedReader(new InputStreamReader(is));
                String  line=null;
                while((line=br.readLine())!=null){
                    System.out.println("我是客户端:服务器  对我说:"+line);
                }
                
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    br.close();
                    oos.close();
                    os.close();
                    is.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
            
            
            
            
        }
    }
    客户端代码

    可是重复 创建多个 客户端的代码  让多个客户端都 启动!  服务器 会接收到每一个客户的信息!

    public class Service { // 服务器
        public static void main(String[] args) {
            DatagramPacket dp = null;  //打包 和 拆包数据 
            DatagramSocket ds = null;  // 接收和 发送数据
            //创建一个包 给客户端响应
            DatagramPacket  dpTo=null;
            try {
                byte [] buf=new byte[1024];
                //创建数据包的对象
                dp=new DatagramPacket(buf, buf.length);
                ds=new DatagramSocket(8800);
                //接收
                ds.receive(dp);
                //显示接收的信息     拆包
                String  msg=new String(dp.getData(), 0, dp.getLength());
                //获取 对方的地址   客户端的信息
                System.out.println(dp.getAddress().getHostAddress()+"说====:"+msg);
                
                //回复给 客户端
                byte[]  reply="您好!航母已经发货!".getBytes();
                // dp就是从客户端传来的信封    信封上肯定有 寄件人的地址
                SocketAddress address=dp.getSocketAddress();
                //打包成功
                dpTo=new DatagramPacket(reply, reply.length,address);
                //发送
                ds.send(dpTo);
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
    
        }
    
    }
    基于UDP的客服端代码
    public class Client {    //客户端
        public static void main(String[] args) {
            DatagramPacket dp = null;  //打包 和 拆包数据 
            DatagramSocket ds = null;  // 接收和 发送数据
            //创建一个包 给服务器响应
            DatagramPacket  dpTo=null;
            InetAddress address=null;
            try {
                //你在网上购物  要不要给客服 说你的地址
                byte[]  say="买个航空母舰!".getBytes();
                //获取本机的地址!
                address = InetAddress.getByName("localhost");
                //打包
                dp=new DatagramPacket(say, say.length,address,8800);
                //发送
                ds=new DatagramSocket();
                ds.send(dp);
                
                //接收
                byte [] buf=new byte[1024];
                dpTo=new DatagramPacket(buf, buf.length);
                ds.receive(dpTo);
                //我们看客服给我们回复了 什么     拆包
                String reply=new String(dpTo.getData(),0,dpTo.getLength());
                System.out.println(dpTo.getAddress().getHostAddress()+"说===》"+reply);
            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            
        }
    
    }
    基于UDP的顾客端代码
  • 相关阅读:
    matplotlib数据可视化之柱形图
    xpath排坑记
    Leetcode 100. 相同的树
    Leetcode 173. 二叉搜索树迭代器
    Leetcode 199. 二叉树的右视图
    Leetcode 102. 二叉树的层次遍历
    Leetcode 96. 不同的二叉搜索树
    Leetcode 700. 二叉搜索树中的搜索
    Leetcode 2. Add Two Numbers
    Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/999-/p/6097618.html
Copyright © 2011-2022 走看看