500分 求解关于UDP数据报的接收问题 第一个问题:调用receive时,覆盖bigArray第二个问题:进行ANSI和unicode转换,第三个问题:看看server端是否在发数据 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 下面是一个客户端向服务端发文件的例子。服务端发向客户端大致相同。希望能对你有帮助。import java.io.FileOutputStream;import java.net.DatagramPacket;import java.net.DatagramSocket;public class UDPServer { final int PORT = 1111; final int PACKET_SIZE = 512; void start() throws Exception { String filename = "test1.txt"; // target file name, change it System.out.println("UDPServer started..."); DatagramSocket socket = new DatagramSocket(PORT); FileOutputStream fos = new FileOutputStream(filename); boolean endFlag = false; int i = 0; while (true) { i++; byte[] buf = new byte[PACKET_SIZE]; DatagramPacket dp = new DatagramPacket(buf, buf.length); socket.receive(dp); fos.write(dp.getData(), 0, dp.getLength()); if (dp.getLength() < PACKET_SIZE) { break; } } fos.close(); System.out.println("File received and wrote to: \"" + filename + "\""); socket.close(); } public static void main(String[] args) throws Exception { new UDPServer().start(); }}//////////import java.io.FileInputStream;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;public class UDPClient { final int PORT = 1111; // server port final int PACKET_SIZE = 512; final String serverIp = "127.0.0.1"; // change it void sendFile(String filename) throws Exception { DatagramSocket socket = new DatagramSocket(); byte[] buf = new byte[PACKET_SIZE]; InetAddress address = InetAddress.getByName(serverIp); FileInputStream fis = new FileInputStream(filename); boolean endFlag = false; int i = 0; int len = -1; while ((len = fis.read(buf, 0, buf.length)) != -1) { i++; DatagramPacket packet = new DatagramPacket(buf, len, address, PORT); socket.send(packet); } fis.close(); System.out.println("File sent to: " + serverIp); socket.close(); } public static void main(String[] args) throws Exception { String filename = "test.txt"; if (args.length == 1) { filename = args[0]; } UDPClient client = new UDPClient(); client.sendFile(filename); }} 关注..学习中...给点系统资料好不好[email protected] 请问谁有工具类sqlhelper类及dbutil HttpClient 获取图片的一个问题 hibernate 一对一的问题 ssh整合问题 jquery Struts标签和Jstl标签 请教大手:AOP中不能拦截什么方法 学习搜索引擎技术有什么好的网站和书,语言不限 iText怎样才能输出用“Windows造字程序”画出的字符? 关于TableDataSet与Provider与Resovler的问题。谢谢各位了。 哪位知道如何配置MDB? 看书看到RowSet,不明白,请指点一下
希望能对你有帮助。import java.io.FileOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;public class UDPServer { final int PORT = 1111;
final int PACKET_SIZE = 512; void start() throws Exception {
String filename = "test1.txt"; // target file name, change it
System.out.println("UDPServer started...");
DatagramSocket socket = new DatagramSocket(PORT);
FileOutputStream fos = new FileOutputStream(filename);
boolean endFlag = false;
int i = 0;
while (true) {
i++;
byte[] buf = new byte[PACKET_SIZE];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
socket.receive(dp);
fos.write(dp.getData(), 0, dp.getLength());
if (dp.getLength() < PACKET_SIZE) {
break;
}
}
fos.close();
System.out.println("File received and wrote to: \"" + filename + "\"");
socket.close();
} public static void main(String[] args) throws Exception {
new UDPServer().start();
}
}
//////////
import java.io.FileInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;public class UDPClient { final int PORT = 1111; // server port
final int PACKET_SIZE = 512;
final String serverIp = "127.0.0.1"; // change it void sendFile(String filename) throws Exception {
DatagramSocket socket = new DatagramSocket();
byte[] buf = new byte[PACKET_SIZE];
InetAddress address = InetAddress.getByName(serverIp);
FileInputStream fis = new FileInputStream(filename);
boolean endFlag = false;
int i = 0;
int len = -1;
while ((len = fis.read(buf, 0, buf.length)) != -1) {
i++;
DatagramPacket packet = new DatagramPacket(buf, len, address, PORT);
socket.send(packet);
}
fis.close();
System.out.println("File sent to: " + serverIp);
socket.close();
} public static void main(String[] args) throws Exception {
String filename = "test.txt";
if (args.length == 1) {
filename = args[0];
}
UDPClient client = new UDPClient();
client.sendFile(filename);
}
}