java socket 通信问题 关于Java中udp通信的问题,怎么尽可能的让接收方获取更多的包呢?我的接收方只能获取两个包,我怀疑循环那里是不是出错了。接收的包总是很小 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是接收方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.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 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(); }} java udp发送多个包,为何不用NIO去实现? 关于udp的发包和接收包的问题。。我们一般通过抓包工具去看。看看你发送的udp包,和你接收的udp包。。比较一下。。看看是否有数据丢失不过udp的包。。有时的确会存在丢包现象抓包工具:wireshark-win32-1.2.5..或者64.。。 文件大小 比一下就知道了,UDP 丢包不奇怪…… udp没有反馈,发送方无法知道接收方是否成功收取,那就同一个包多发几次 正则表达式 取之间的部分 在JEditorPane中改变选中的文字的颜色 一个面试题,求解 急 !!!!运行jar 包出现这个错误是为什么! 在深圳的朋友进来一下,下班前揭贴 Java基础,接口 我的Eclipse为什么不是中文的,我的朋友的怎么是中文的呀 问一个很简单的问题 一个简单的程序到底错在哪? 我写了一个Applet小程序(用JBuilder5),运行正常,但是…… 全局变量在定义时初始化和在定义时不初始化而集中到一个函数中初始化有什么区别,各有什么优缺点? 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.。。