码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-01-23 14:18:38      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:demo   tag   packet   .net   java学习   电脑   com   提升   src   

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地址即可

 

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

标签:demo   tag   packet   .net   java学习   电脑   com   提升   src   

原文地址:https://www.cnblogs.com/xuyiqing/p/8334646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!