简单的UDP 消息的传递以及URL文件下载
来源:互联网 发布:极限挑战网络直播地址 编辑:程序博客网 时间:2024/06/14 06:14
特别注意:int a = s.getBytes().length; 是获取字节数长度,不是字符串长度,否则汉字出现乱码
发送端:
import java.io.*;
import java.net.*;
public class Send {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket();
// File f = new File("D:/ccc.txt");
// FileInputStream fi = new FileInputStream(f);
// BufferedReader br=new BufferedReader(new InputStreamReader(fi));
// String s1 = "";
// while ((s1 = br.readLine()) != null) {
// int a = s1.getBytes().length;
// int count = a / 30;
// int left = a % 30;
// for (int i = 0; i < count; i++) {
// DatagramPacket dp = new DatagramPacket(s1.getBytes(), i * 30, 30,
// InetAddress.getByName("localhost"), 6668);
// ds.send(dp);
// }
// DatagramPacket dp = new DatagramPacket(s1.getBytes(), count * 30, left,
// InetAddress.getByName("localhost"), 6668);
// ds.send(dp);
// }
String s = "hello world,how are you ,i am fine, do dou want to go to the zoo?,我晕";
int a = s.getBytes().length;
int count = a / 30;
int left = a % 30;
for (int i = 0; i < count; i++) {
DatagramPacket dp = new DatagramPacket(s.getBytes(), i * 30, 30, InetAddress.getByName("localhost"),
6668);
ds.send(dp);
}
DatagramPacket dp = new DatagramPacket(s.getBytes(), count * 30, left, InetAddress.getByName("localhost"),
6668);
ds.send(dp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
接收端:
import java.net.*;
public class Save {
public static void main(String[] args) {
try {
DatagramSocket ds = new DatagramSocket(6668);
byte[] b = new byte[30];
while (true) {
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
byte[] data = dp.getData();
String string = new String(data, 0, dp.getLength());
System.out.println(string);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
URL文件下载:
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Test {
public static void main(String[] args) {
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
File f = new File("http://img06.tooopen.com/images/20160712/tooopen_sy_170083325566.jpg");
File file = new File("E:/", f.getName());
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
URL url = new URL("http://img06.tooopen.com/images/20160712/tooopen_sy_170083325566.jpg");
InputStream in = url.openStream();
bi = new BufferedInputStream(in);
FileOutputStream fo = new FileOutputStream(file);
bo = new BufferedOutputStream(fo);
int a = -1;
while ((a = bi.read()) != -1) {
bo.write(a);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bi.close();
bo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 简单的UDP 消息的传递以及URL文件下载
- 基于UDP实现简单的客户端服务端的消息传递
- 基于UDP的简单收发消息代码
- jQuery 插件取url参数[jquery.url.js]的使用以及文件下载
- wget + 下载文件的URL
- TCP、UDP以及HTTP的简单讲解
- TCP、UDP以及HTTP的简单讲解
- TCP、UDP以及HTTP的简单讲解
- TCP、UDP以及HTTP的简单讲解
- android的消息传递简单实例
- <Handler>之简单的消息传递机制
- EventBus的简单使用,消息传递
- 用户自定义消息,以及消息传递的参数设定
- 简单的文件下载
- 简单的文件下载
- 最简单的网络编程代码,ip、url、UDP
- pixhawk通过udp接收mavlink消息的简单例程
- url传递中的特殊符号的编码以及中文编码
- main函数之前执行了哪些操作
- android引用anim帧动画导出签名apk报错
- IQKeyboardManager和UIScrollView问题
- 配置文件使用
- Android 控件不为人知的秘密
- 简单的UDP 消息的传递以及URL文件下载
- PHP命名空间(Namespace)的使用详解
- Java 分支结构
- Struts2+Hibernate简单整合
- 最小生成树与最短路径算法
- 测试相关知识点
- 安装卸载jdk
- git 回退修复bug
- JSP:el表达式