关于Java中udp通信的问题,
怎么尽可能的让接收方获取更多的包呢?
我的接收方只能获取两个包,我怀疑循环那里是不是出错了。接收的包总是很小
怎么尽可能的让接收方获取更多的包呢?
我的接收方只能获取两个包,我怀疑循环那里是不是出错了。接收的包总是很小
解决方案 »
- 加载页面时如何异步查询数据,并将返回的数据用c forecach迭代显示在页面上?
- 谁能用Java知识给我解释一下这一句呢?
- 请教,如何优化该程序
- 关于String类中split方法的问题?
- 请教Java做电子地图问题
- java 将接收的数据存到数组 再从数组里取出来判断
- 能不能在一个frame里动态的添加label啊???
- Re:如何长久保存添入下拉列表框的项目信息??
- “不合法的转义字符”的错误!
- 菜鸟请教:手上没有书,请各位前辈帮忙确定几个定义和用法:主键、外键、左连接、右连接、索引。
- 全局变量在定义时初始化和在定义时不初始化而集中到一个函数中初始化有什么区别,各有什么优缺点?
- java udp网络通信关于端口的问题
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UDPFileReceiver {
private int port = 1220;
private DatagramSocket socket;
public UDPFileReceiver() throws SocketException{
socket = new DatagramSocket(port);
socket.setSoTimeout(8000);
}
public void reciveData() throws FileNotFoundException{
byte[] buf = new byte[8192];
FileOutputStream fos = new FileOutputStream( new File("641k.jpg"));
while(true){
DatagramPacket packet = new DatagramPacket(buf, 8192);
try{
socket.receive(packet);
fos.write(packet.getData(), 0, packet.getLength());
}catch(Exception e){
try {
System.out.println("发生异常传输结束");
fos.flush();
fos.close();
socket.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
}finally{
}
}
}
public static void main(String[] args) throws FileNotFoundException, SocketException {
new UDPFileReceiver().reciveData();
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;public class ImageReceive { private int port = 1220;
private DatagramSocket receiver; public ImageReceive() {
try {
receiver = new DatagramSocket(port);
receiver.setSoTimeout(10000);
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
ImageReceive imgReceive = new ImageReceive();
imgReceive.start();
} public void start() {
byte[] buf = new byte[5000];
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("123.jpg"));
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
while (true) {
try {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
receiver.receive(packet);
System.out.println(packet.getLength());
fos.write(packet.getData(), 0, packet.getLength());
}catch (Exception e) {
try {
System.out.println("发生异常传输结束");
fos.flush();
fos.close();
receiver.close();
break;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;public class ImageSend {
DatagramSocket socket =null;
public ImageSend(){
try {
socket=new DatagramSocket();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws UnknownHostException, IOException {
ImageSend send=new ImageSend();
send.send();
}
public void send() throws UnknownHostException, IOException{
FileInputStream fis = new FileInputStream("004.jpg");
byte[] buffer = new byte[5000];
int len = 0;
int count=0;
while((len = fis.read(buffer)) != -1){
System.out.println(len);
count++;
DatagramPacket packet = new DatagramPacket(buffer, len,InetAddress.getByName("localhost"),1220);
socket.send(packet);
}
System.out.println(count);
socket.close();
}
}
wireshark-win32-1.2.5..或者64.。。