zoukankan      html  css  js  c++  java
  • 利用线程分离发送和接受,这样每个客服端都可以分离

    这个代码是用线程实现建立连接

    这样就可以实现发送和接受分离,大家互相不会影响到彼此

    package com.udpcom;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    import java.util.Scanner;
    
    public class SendThread implements Runnable {
        InetAddress ia;
        int port;
        DatagramPacket dp;
        DatagramSocket ds;
    
        Scanner in = new Scanner(System.in);
        public SendThread(InetAddress ia, int port) {
            this.ia = ia;
            this.port = port;
            try {
                ds = new DatagramSocket();
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void run() {
            do{
            System.out.println("please input here:");
            String str = in.nextLine();
            //包装
            dp = new DatagramPacket(str.getBytes(),str.getBytes().length,ia,port);
            try {
                ds.send(dp);
            } catch (IOException e) {
                e.printStackTrace();
            }   
            }while(true);
        }
    
    }
    
    package com.udpcom;
    
    import java.io.IOException;
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.SocketException;
    
    public class ReceiveThread implements Runnable {
        InetAddress ia;
        int port;
        DatagramPacket dp;
        DatagramSocket ds;  
        byte buff[]=new byte[2014];
        public ReceiveThread(int port) {
            this.port = port;
            dp = new DatagramPacket(buff,2014);
            try {
                ds =new DatagramSocket(port);
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            do {
            try {
                ds.receive(dp);
                System.out.println(new String(dp.getData(),0,dp.getLength()));          
            } catch (SocketException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            }while(true);
    
        }
    
    }
    
    package com.udpcom;
    
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class ClientA {
        public static void main(String[] args) {
            int port = 20000;
            String ip = "localhost";
            try {
                InetAddress ia = InetAddress.getByName(ip);
                Thread a = new Thread(new SendThread(ia,port));
                Thread b = new Thread(new ReceiveThread(20001));
                b.start();  
                a.start();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    
    }
    
    
    package com.udpcom;
    
    import java.net.DatagramPacket;
    import java.net.DatagramSocket;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.util.Scanner;
    
    public class ClientB {
    /**
     * 只要输入对方的地址,加上一个端口号,我就可以发送
     * 根据地址和端口号,包装成为数据报,然后利用套接字发送
     * 发送利用20001 接受用20000端口,错开和另外一个main进程交互
     */
        public static void main(String[] args) {
            int port = 20000;
            String ip = "localhost";
            try {
                InetAddress ia = InetAddress.getByName(ip);
                Thread a = new Thread(new SendThread(ia,20001));
                Thread b = new Thread(new ReceiveThread(port));
                a.start();  
                b.start();
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }
    
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    JavaScript function (简单总结)
    JavaScript 数组 (简单总结)
    yum update 和 yum upgrate 区别
    git clone警告,提示Warning:Permission denied (publickey)
    ''退格符号笔记
    MySQL Workbench导出Model提示['ERROR 1064 (42000): You have an error in your SQL syntax....syntax to use near 'VISIBLE']
    《Python编程从入门到实践》--- 学习过程笔记(3)列表
    《Python编程从入门到实践》--- 学习过程笔记(2)变量和简单数据类型
    Windows+MyEclipse+MySQL【连接数据库报错caching_sha2_password】
    测试 | 让每一粒尘埃有的放矢
  • 原文地址:https://www.cnblogs.com/mrcharles/p/4731759.html
Copyright © 2011-2022 走看看