导航:首页 > 编程语言 > java实现udp

java实现udp

发布时间:2022-08-20 09:12:46

❶ 5 java 如何使用udp协议传送文件

//发送端SocketSendFile.java
import java.io.*;
import java.net.*;

public class SocketSendFile {
public static final int SER_PORT=666;
public static final int CLI_PORT=8484;
public static final String SER_IP="192.168.0.35";
public static int bufSize = 1024;
public static byte] mess = new bytebufSize];
//建立Socket引用
public static DatagramSocket dp;

public static void main(String] args) throws Exception {
dp = new DatagramSocket(SER_PORT);
//调用构造函数SocketSendFile,并传递参数args0](所要传输的文件名)
SocketSendFile(args0]);
}

public static void SocketSendFile(String file2) throws Exception {
//定义一个计数器
int pos =0;
//设置写入流
FileInputStream fis = new FileInputStream(file2);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);

int i;
do {
i = dis.read();
int j=0;
while (j<1024 & i != -1) {
messpos++] = (byte) i;
i=dis.read();
j++;
}
dp.send(new DatagramPacket(mess,pos,InetAddress.getByName(SER_IP),CLI_PORT));
}
while (i != -1);
fis.close();
}
}

//接收端SocketReceiveFile.java
import java.net.*;
import java.io.*;

public class SocketReceiveFile {
public static int bufSize=1024;
public static byte] mess=new bytebufSize];
public static DatagramSocket dp;
public static final int SER_PORT=8484;

public static void main(String] args) throws Exception {
dp = new DatagramSocket(SER_PORT);
SocketReceiveFile(args0]);
}

public static void SocketReceiveFile(String file1) throws Exception {
FileOutputStream fos = new FileOutputStream(file1);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);

int i;
DatagramPacket p = new DatagramPacket(mess,mess.length);

while(true) {
boolean j=false;
while (p.getData().length != 0) {
dos.write(p.getData());
dp.receive(p);
j=true;
}
// System.out.println(new String(p.getData(),0,p.getLength()));
if (j)
System.out.println("文件传送完毕.");
}
// fos.close();
}
}

❷ Java写的一个udp的demo用来计算探针传送过来的数据,怎么实现一个接口来调用这个数据

Java UDP通信简单实现
1、Java实现方式
1)server端



/**
*UDPserver端
**/
publicclassUdpServer{
//定义一些常量
privatefinalintMAX_LENGTH=1024;//最大接收字节长度
privatefinalintPORT_NUM=5066;//port号
//用以存放接收数据的字节数组
privatebyte[]receMsgs=newbyte[MAX_LENGTH];
//数据报套接字
;
//用以接收数据报
;

publicUdpServer(){
try{
/*******接收数据流程**/
//创建一个数据报套接字,并将其绑定到指定port上
datagramSocket=newDatagramSocket(PORT_NUM);
//DatagramPacket(bytebuf[],intlength),建立一个字节数组来接收UDP包
datagramPacket=newDatagramPacket(receMsgs,receMsgs.length);
//receive()来等待接收UDP数据报
datagramSocket.receive(datagramPacket);

/******解析数据报****/
StringreceStr=newString(datagramPacket.getData(),0,datagramPacket.getLength());
System.out.println("ServerRece:"+receStr);
System.out.println("ServerPort:"+datagramPacket.getPort());

/*****返回ACK消息数据报*/
//组装数据报
byte[]buf="Ireceivethemessage".getBytes();
DatagramPacketsendPacket=newDatagramPacket(buf,buf.length,datagramPacket.getAddress(),datagramPacket.getPort());
//发送消息
datagramSocket.send(sendPacket);
}catch(SocketExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭socket
if(datagramSocket!=null){
datagramSocket.close();
}
}
}
}

2)client


/***
*UDPclientClient端
***/
publicclassUdpClient{

privateStringsendStr="SendString";
privateStringnetAddress="127.0.0.1";
privatefinalintPORT_NUM=5066;

;
;

publicUdpClient(){
try{

/***发送数据***/
//初始化datagramSocket,注意与前面Server端实现的差别
datagramSocket=newDatagramSocket();
//使用DatagramPacket(bytebuf[],intlength,InetAddressaddress,intport)函数组装发送UDP数据报
byte[]buf=sendStr.getBytes();
InetAddressaddress=InetAddress.getByName(netAddress);
datagramPacket=newDatagramPacket(buf,buf.length,address,PORT_NUM);
//发送数据
datagramSocket.send(datagramPacket);

/***接收数据***/
byte[]receBuf=newbyte[1024];
DatagramPacketrecePacket=newDatagramPacket(receBuf,receBuf.length);
datagramSocket.receive(recePacket);

StringreceStr=newString(recePacket.getData(),0,recePacket.getLength());
System.out.println("ClientReceAck:"+receStr);
System.out.println(recePacket.getPort());


}catch(SocketExceptione){
e.printStackTrace();
}catch(UnknownHostExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭socket
if(datagramSocket!=null){
datagramSocket.close();
}
}
}
}

❸ java怎么实现JSON打包UDP

java实现JSON打包UDP cJSON支持在C程序中创建和解析JSON数据,其提供多种方法供C程序使用,最直接的是将cJSON.c和cJSON.h加入到C工程中

(1) QJsonObject用于在Qt中创建JSON对象

(2)数据传输通过UDP运行

❹ Java语言中如何使用UDP协议进行数据传输的

java里UDP协议传送的方式细节已经封装了到类DatagramSocket里了,只要用DatagramSocket的对象进行传输数据就可以了.

底层实现是应该依靠本地C语言程序来完成的

❺ Java语言应用中实现UDP协议编程的方法

java UDP主要用到了DatagramSocket和DatagramPacket两个类。
DatagramSocket类表示用来发送和接收数据报包的套接字。
数据报套接字是包投递服务的发送或接收点。每个在数据报套接字上发送或接收的包都是单独编址和路由的。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达。

DatagramPacket类表示数据报包。
数据报包用来实现无连接包投递服务。每条报文仅根据该包中包含的信息从一台机器路由到另一台机器。从一台机器发送到另一台机器的多个包可能选择不同的路由,也可能按不同的顺序到达。不对包投递做出保证。

建议看下API

❻ java 中怎么使用UDP

发送步骤:

使用 DatagramSocket(int port) 建立socket(套间字)服务。

将数据打包到DatagramPacket中去

通过socket服务发送 (send()方法)

关闭资源

importjava.io.IOException;
importjava.net.*;

publicclassSend{

publicstaticvoidmain(String[]args){

DatagramSocketds=null;//建立套间字udpsocket服务

try{
ds=newDatagramSocket(8999);//实例化套间字,指定自己的port
}catch(SocketExceptione){
System.out.println("Cannotopenport!");
System.exit(1);
}

byte[]buf="Hello,Iamsender!".getBytes();//数据
InetAddressdestination=null;
try{
destination=InetAddress.getByName("192.168.1.5");//需要发送的地址
}catch(UnknownHostExceptione){
System.out.println("Cannotopenfindhost!");
System.exit(1);
}
DatagramPacketdp=
newDatagramPacket(buf,buf.length,destination,10000);
//打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!)

try{
ds.send(dp);//发送数据
}catch(IOExceptione){
}
ds.close();
}
}

接收步骤:

使用 DatagramSocket(int port) 建立socket(套间字)服务。(我们注意到此服务即可以接收,又可以发送),port指定监视接受端口。

定义一个数据包(DatagramPacket),储存接收到的数据,使用其中的方法提取传送的内容

通过DatagramSocket 的receive方法将接受到的数据存入上面定义的包中

使用DatagramPacket的方法,提取数据。

关闭资源。

importjava.net.*;

publicclassRec{

publicstaticvoidmain(String[]args)throwsException{

DatagramSocketds=newDatagramSocket(10000);//定义服务,监视端口上面的发送端口,注意不是send本身端口

byte[]buf=newbyte[1024];//接受内容的大小,注意不要溢出

DatagramPacketdp=newDatagramPacket(buf,0,buf.length);//定义一个接收的包

ds.receive(dp);//将接受内容封装到包中

Stringdata=newString(dp.getData(),0,dp.getLength());//利用getData()方法取出内容

System.out.println(data);//打印内容

ds.close();//关闭资源
}
}

❼ Java如何实现UDP自动回复

发送步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。
将数据打包到DatagramPacket中去
通过socket服务发送 (send()方法)
关闭资源

import java.io.IOException; import java.net.*; public class Send { public static void main(String[] args) { DatagramSocket ds = null; //建立套间字udpsocket服务 try { ds = new DatagramSocket(8999); //实例化套间字,指定自己的port } catch (SocketException e) { System.out.println("Cannot open port!"); System.exit(1); } byte[] buf= "Hello, I am sender!".getBytes(); //数据 InetAddress destination = null ; try { destination = InetAddress.getByName("192.168.1.5"); //需要发送的地址 } catch (UnknownHostException e) { System.out.println("Cannot open findhost!"); System.exit(1); } DatagramPacket dp = new DatagramPacket(buf, buf.length, destination , 10000); //打包到DatagramPacket类型中(DatagramSocket的send()方法接受此类,注意10000是接受地址的端口,不同于自己的端口!) try { ds.send(dp); //发送数据 } catch (IOException e) { } ds.close(); } }

接收步骤:
使用 DatagramSocket(int port) 建立socket(套间字)服务。(我们注意到此服务即可以接收,又可以发送),port指定监视接受端口。
定义一个数据包(DatagramPacket),储存接收到的数据,使用其中的方法提取传送的内容
通过DatagramSocket 的receive方法将接受到的数据存入上面定义的包中
使用DatagramPacket的方法,提取数据。

❽ java中UDP文件传输怎么实现

java UDP连接,如果要发送文件的话,你只能自己定义一系列的协议
因为TCP UDP 双方发送都是二进制数据
那么这个实现非常复杂
得不停的发送数据,写数据,建议使用http协议

❾ 用java实现UDP校验和计算

public static void main(String[] arg) {
String str = "hello";
System.out.println(toByte(str));
// 得到结果:byte[] sbytes = { 0X68, 0X65, 0X6c, 0X6c, 0X6f };
}

private static String toByte(String str) {
byte[] bytes = str.getBytes();
StringBuffer sb = new StringBuffer();
sb.append("byte[] sbytes = { ");
for (int i = 0; i < bytes.length; i++) {
sb.append(String.format("0X%s", Integer.toHexString(bytes[i])));
if (i < bytes.length - 1) {
sb.append(", ");
}
}
sb.append(" };").append("\r\n");
return sb.toString();
}

阅读全文

与java实现udp相关的资料

热点内容
pdf手写笔 浏览:173
别永远伤在童年pdf 浏览:984
爱上北斗星男友在哪个app上看 浏览:414
主力散户派发源码 浏览:665
linux如何修复服务器时间 浏览:55
荣县优途网约车app叫什么 浏览:473
百姓网app截图是什么意思 浏览:222
php如何嵌入html 浏览:811
解压专家怎么传输 浏览:743
如何共享服务器的网络连接 浏览:132
程序员简易表白代码 浏览:167
什么是无线加密狗 浏览:63
国家反诈中心app为什么会弹出 浏览:68
cad压缩图打印 浏览:102
网页打开速度与服务器有什么关系 浏览:863
android开发技术文档 浏览:65
32单片机写程序 浏览:52
三星双清无命令 浏览:839
汉寿小程序源码 浏览:345
易助erp云服务器 浏览:533