UDP是一种不可靠的协议,它在通信两端各建立一个socket,这两个socket不会建立持久的通信连接,只会单方面向对方发送数据,不检查发送结果。
java中基于UDP协议的通信使用DatagramSocket类的receive和send方法即可,但消息需要通过一个特定的类封装(DatagramPacket)
下面是一个基于UDP协议的通信的例子,
服务器端,
package udp; import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket; public class Server {
private static final int PORT = 3000;
//数据报大小
private static final int DATA_LEN = 4096;
//接受网络数据的字节数组
byte[] inBuff = new byte[DATA_LEN];
//接收数据
private DatagramPacket inPacket = new DatagramPacket(inBuff, inBuff.length);
//发送数据
private DatagramPacket outPacket;
String[] books = new String[]
{
"疯狂英语",
"康熙词典",
"TCP协议",
"NIO非阻塞channel"
};
public void init() throws IOException {
try {
DatagramSocket socket = new DatagramSocket(PORT);
for (int i = 0; i < 1000; i++) {
//读取socket中的数据
socket.receive(inPacket);
System.out.println(inBuff == inPacket.getData());
System.out.println(new String(inBuff, 0, inPacket.getLength())); byte[] sendData = books[i % 4].getBytes();
outPacket = new DatagramPacket(sendData, sendData.length, inPacket.getAddress(), PORT);
socket.send(outPacket);
}
} catch (IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
new Server().init();
}
}
客户端,
package udp; import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner; public class Client {
private static final int PORT = 3000;
//数据报大小
private static final int DATA_LEN = 4096;
private static final String DEST_IP = "127.0.0.1";
//接受网络数据的字节数组
byte[] inBuff = new byte[DATA_LEN];
//接收数据
private DatagramPacket inPacket = new DatagramPacket(inBuff, inBuff.length);
//发送数据
private DatagramPacket outPacket; public void init() throws IOException {
try {
//使用随机端口创建socket
DatagramSocket socket = new DatagramSocket();
outPacket = new DatagramPacket(new byte[0], 0, InetAddress.getByName(DEST_IP), PORT);
Scanner scann = new Scanner(System.in);
while (scann.hasNextLine()) {
byte[] buff = scann.nextLine().getBytes();
outPacket.setData(buff);
socket.send(outPacket);
socket.receive(inPacket);
System.out.println(new String(inBuff, 0, inPacket.getLength()));
}
} catch(IOException e) {
e.printStackTrace();
}
} public static void main(String[] args) throws IOException {
new Client().init();
}
}
执行结果,启动一个服务器端,再启动一个客户端发一条信息,服务器端输出了信息,
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAi4AAAF0CAIAAACL1SWEAAAgAElEQVR4nO3d6bMsR3nn8X5tgw0YvIDNsIpFIIlFEqAVSRhbC0hI2JYEWjGIRWjDIAHaEWgBhGQkwRXGrBICgzDaAEeYxhEeX0AO3wmfN34x2BPMeIgxMfbM/AE9L/LcvHnzyefJp6qrs6pPfz9RcaM6Kysrq8859btZVV09WywWW3t2MzExMTExjTXNiCImJiYmpnGnfVF03Qpcr7tBd6PuI7qbTB/VfUx3s+4W3a2m23Qf131C90nd7aZP6e7Q3an7U92ndXeZ7tbdo/uM7rO6XaZ7dZ/T/Znu87o/N31B90Xdl3Rf1n1F91XTfbr7dV/TPaD7uukbur/QfVP3Ld2Dpm/r/lL3Hd1Duod1j5ge1T2m+67ue7rvJ4gioogoIoqIIqKIKCKKiCKiiCgiioiiNIpuv/OeTlNMnSzPwksjirQzhjGKtApEEVFEFBFFRNHOj6J//Kd/0aaLrroj/VdGUZh++NfflRGSxs+P//MPYvDImTSK0iFRmygKW5l+FIV+rmkUhc4TRUQRUUQUqVH048f/3jnFKIotbO3Z/djDD6Yvs7FRFkvVKBpqVJQ10imKtvbs3v23fx3/3dqzO0ZReOmMorj1nRpF6TscQygtHDaKYrNEEVFEFO3AKJq5ZVFkT8UoMk7QDXitKI2fYaOo66ho+lEUmu0RRWkCxfk4k8XPUKMioogoIoqIojyKtNsWjCjSRkV2qqUJFEucUZRJg0em0ZSjKKbRRKIoPUFXjaL0JVFEFBFFRJEaRYvFYrFYpMGTvUyjyB4byVsVqteKtsSFourlon5RVLxtIYuikEZxiteKYkkMoWzHi2fn0ijKSrb27P7R3/4g/ru1Z3fIoaxZI4qymrfffnuc/8nu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6gxFDCWLomMBYJWsKHIe+ouVZ74RjNZ4cRNam0TR4GQUjd0jADtaj1GRFkWypPfAhSgaF1EEoKmhrhUVo0huzhlORNG4iCIATQ04Kspixhk2RryllT2hiKEQRQCaGupakT1vvPQMdBgVNUYUAWjKjqJYzTjcE0U7D1EEoCn7BF06Y0SONnLyjKWIogkiigA0Vb1WlI6Q5GiptyzksrjSltqW7xUCoghAU/KIY488ikd8u5qxipYfnnKyZ3WIIgBNGVGkHfdlBlTPmxVXMbLEDjy7GpZHFAFoKjviaOOb7KBfPJlWXLFYp1hibK5aH8MiigA0teSXRDiToFNgdB39kEaDI4oANMURBxJRBKApjjiQiCIATXHEgUQUAWiKIw4koghAUxxxIBFFAJpyPpkbG4UoAtBU8SOuRNGGI4oANCU/4lp8sFv6UpbL+XQVgm3tEEUAmvKPirIo8scSabR2tCha/r8X1f/rFNs3NpqtItuZm/89srfr3JEO+9+l2UEaX7IRrT92P43OZ6vIdub8vIb4eS3ffrXmwG9m1yia759A8rckq7yKHz9WrRhF2s9ao1Wwf2eMefs303hptylf2v3XNjSUru+zv7VqzU79mdrPq2oDf17LtD9sU3U9RkVa4bA/EoxIRlH18CF/4p5fpOq8vV2jM53qeFaxd2RAY/0drfvPq2oDf16r28rwjCjq+hvjmcdaqEZRZr5XsdworM4vedhy/hL2+6subtfuTLW+sYpdf/lybX/t/mTzxu5rhVk7xop2s1We91/bQa2+sUrX979rueyhrO/cX619bStd2y+2k9e3Lwn43465+EWs7iEmq2sUGezfY+13zP71tTfhaafayWUWadv11Nf637t94w301Df643mf/V2y26l20mOQ99PZ/97tG2+gp351la7td9q03WalPjdKQRowima+w1Nabvz6dqozG+h/2c4/rfn++tWvbs6uX901u3KnVWJ5dR+ddWYrGBV1ff+71jdW8dSv7ppnT5396VG5U/my7ydRBGmQa0Wprr/xcV7bbvVX3GjcuUpxdc+f2TLz2qLqG+h5Vz17Wu2ev5/O/nt2p99eGNsdal5bVH0DPe+qUa3Hbvo77+mnc6Md5okiSF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GMg37x/rpZkhlGZU+zWvsZ4md1iCIATRlR1KmdHmkhl2r1iaLGiCIATcnHoYbyTgf6bFSUlTtX92/LGD9hEEQRgKaKT1uY+c62xQrZjFxk65El1dN3WAZRBKApTxRlq2gx0CmKPJeI0mrOPmAQRBGApqrXitIk0E7EZasYJcVF1Vyxh2haXKE3oghAU8Yz6OJ88d9Mtko1J/pFEdogigA05T9BJ2ciLaIGHxWhDaIIQFPVO+iqUVSNqKzEr9iOZ8iFJRFFAJryXCsq/ptVm3UcFWV17JrLbwWdEEUAmtKuFaUzRiyleoSEnTHZIv92sSSiCEBL/x9lSrBKko+VtgAAAABJRU5ErkJggg==" alt="" />
JAVA基础知识之网络编程——-基于UDP协议的通信例子的更多相关文章
-
JAVA基础知识之网络编程——-基于TCP通信的简单聊天室
下面将基于TCP协议用JAVA写一个非常简单的聊天室程序, 聊天室具有以下功能, 在服务器端,可以接受客户端注册(用户名),可以显示注册成功的账户 在客户端,可以注册一个账号,并用这个账号发送信息 发 ...
-
JAVA基础知识之网络编程——-TCP/IP协议,socket通信,服务器客户端通信demo
OSI模型分层 OSI模型是指国际标准化组织(ISO)提出的开放系统互连参考模型(Open System Interconnection Reference Model,OSI/RM),它将网络分为七 ...
-
JAVA基础知识之网络编程——-基于AIO的异步Socket通信
异步IO 下面摘子李刚的<疯狂JAVA讲义> 按照POSIX标准来划分IO,分为同步IO和异步IO.对于IO操作分为两步,1)程序发出IO请求. 2)完成实际的IO操作. 阻塞IO和非阻塞 ...
-
JAVA基础知识之网络编程——-基于NIO的非阻塞Socket通信
阻塞IO与非阻塞IO 通常情况下的Socket都是阻塞式的, 程序的输入输出都会让当前线程进入阻塞状态, 因此服务器需要为每一个客户端都创建一个线程. 从JAVA1.4开始引入了NIO API, NI ...
-
JAVA基础知识之网络编程——-网络基础(Java的http get和post请求,多线程下载)
本文主要介绍java.net下为网络编程提供的一些基础包,InetAddress代表一个IP协议对象,可以用来获取IP地址,Host name之类的信息.URL和URLConnect可以用来访问web ...
-
网络编程(基于udp协议的套接字/socketserver模块/进程简介)
一.基于UDP协议的套接字 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议. 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号,就 ...
-
网络编程: 基于UDP协议的socket
udp是无链接的,启动服务之后可以直接接受消息,不需要提前建立链接 UDP协议的通信优势: 允许一个服务器同时和多个客户端通信, TCP不行 服务端 import socket sk = socket ...
-
JAVA基础知识之网络编程——-网络通信模型(IO模型)
<Unix网络编程:卷1>中介绍了5中I/O模型,JAVA作为运行在宿主机上的程序,底层也遵循这5中I/O模型规则.这5中I/O模型分别是: 阻塞式IO 非阻塞式IO I/O复用 信号驱动 ...
-
UNIX网络编程——基于UDP协议的网络程序
一.下图是典型的UDP客户端/服务器通讯过程 下面依照通信流程,我们来实现一个UDP回射客户/服务器: #include <sys/types.h> #include <sys/so ...
随机推荐
-
深入理解javascript中的焦点管理
× 目录 [1]焦点元素 [2]获得焦点 [3]失去焦点[4]焦点事件 前面的话 焦点作为javascript中的一个重要功能,基本上和页面交互都离不开焦点.但却少有人对焦点管理系统地做总结归纳.本文 ...
-
HTML <;meta>; 标签 遇到<;meta http-equiv=";refresh"; content=";0; url=";>;详解
页面定期刷新,如果加url的,则会重新定向到指定的网页,content后面跟的是时间(单位秒),把这句话加到指定网页的<head></head>里一般也用在实时性很强的应用中, ...
-
将webkit内核封装为duilib的浏览器控件
转载请说明出处,谢谢~~ 原本的duilib是自带浏览器控件的,但是使用了IE内核,我在做仿酷狗音乐播放器时,在右侧乐库要用到浏览器控件,而我使用自带的IE控件却发现了不少缺点,这也是duilib一直 ...
-
Linux基础教程
Linux基础教程之<Linux就该这么学>之学习笔记第一篇... ========================= 一.Basic Linux Commands 基本的Linux ...
-
计算机基础--Java中int char byte的关系
计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...
-
【python原理解析】python中分片的实现原理及使用技巧
首先:说明什么是序列? 序列中的每一个元素都会被分配一个序号,即元素的位置,也称为索引:在python中的序列包含:字符串.列表和元组 然后是:什么是分片? 分片就是通过操作索引访问及获得序列的一个或 ...
-
并发编程(二)------并发类容器ConcurrentMap
并发类容器: jdk5.0以后提供了多种并发类容器来替代同步类容器从而改善性能. 同步类容器的状态都是串行化的. 他们虽然实现了线程安全,但是严重降低了并发性,在多线程环境时,严重降低了应用程序的吞吐 ...
-
tp查询中2个表格中字段,比较大小
$where['_string'] = '`has_number` < `number`';//~~~注意:这里`不能丢了: $coupon_flag = $coupon->where($ ...
-
面试&;笔试---c语言之字符串处理
1.字串拷贝库函数strcpy 函数介绍: 原型声明:extern char *strcpy(char *dest,const char *src); 头文件:string.h 功能:把从src地址开 ...
-
java中复制数组的5种方法
“=”,相当于将一个数组变量的引用传递给另一个数组;如果一个数组发生改变,那么引用同一数组的变量也要发生改变.,这一种勉强算是吧 使用FOR循环,将数组的每个元素复制或者复制指定元素,不过效率差一点 ...