Java学习笔记52(网络编程:UDP协议案例)

2018-06-18 03:43:29来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

InetAddress类:

表示互联网中的IP地址,示例:

package demo;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        function1();
        function2();
    }

    public static void function1() throws UnknownHostException {
        InetAddress inet = InetAddress.getLocalHost();
        System.out.println(inet.getHostName());// 获取主机名
        // 输出:DESKTOP-Q3O8AEO
        System.out.println(inet.getHostAddress());// 获取主机IP
        // 输出:192.168.87.1
    }

    public static void function2() throws UnknownHostException {
        // 获得其他IP
        InetAddress inet = InetAddress.getByName("www.baidu.com");
        System.out.println(inet);
        // 输出:www.baidu.com/111.13.100.92
    }
}
View Code

 

UDP协议:无连接通信协议,最大64KB,不安全

TCP协议:面向连接通信协议,可靠安全,三次握手以确认连接,相对速度慢,适合大数据传输

 

UDP示例:

发送端:

package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPSend {
    public static void main(String[] args) {
        try {
            byte[] data = "你好".getBytes();
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            DatagramPacket dp = new DatagramPacket(data, data.length, inet, 6000);
            DatagramSocket ds = new DatagramSocket();
            ds.send(dp);
            ds.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

 

接收端:

package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket(6000);
            byte[] data = new byte[1024];
            DatagramPacket dp = new DatagramPacket(data, data.length);
            ds.receive(dp);
            int length = dp.getLength();
            String ip = dp.getAddress().getHostAddress();
            System.out.println(ip + ":" + new String(data, 0, length));
            ds.close();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

先运行接收端,再运行发送端,结果:

输出:127.0.0.1:你好

 

功能可以提升,实现键盘输入的聊天:

发送端:

package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPSend {
    public static void main(String[] args) {
        try {
            Scanner sc = new Scanner(System.in);
            DatagramSocket ds = new DatagramSocket();
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            while (true) {
                String message = sc.nextLine();
                byte[] data = message.getBytes();
                DatagramPacket dp = new DatagramPacket(data, data.length, inet, 7000);
                ds.send(dp);
            }
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

接收端:

package demo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class UDPReceive {
    public static void main(String[] args) {
        try {
            DatagramSocket ds = new DatagramSocket(7000);
            byte[] data = new byte[1024];
            while (true) {
                DatagramPacket dp = new DatagramPacket(data, data.length);
                ds.receive(dp);
                int length = dp.getLength();
                String ip = dp.getAddress().getHostAddress();
                System.out.println(ip + ":" + new String(data, 0, length));
            }
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }
}
View Code

 

先后运行,在发送端发送的消息可以随时在接收端收到,这里就实现了一个简单的聊天功能

同个局域网下的多台电脑之间也可以聊天,只要修改成相应的IP地址即可

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:svn插件使用教程

下一篇:Caused by: com.fasterxml.jackson.core.JsonParseException: Un