使用 UDP 多播发送文件
sending file using UDP multicast
我想使用 UDP 多播将我的文件发送给我的朋友,但是当我尝试 运行 代码时,输出是这样的:
输出:
Exception in thread "main" java.lang.IllegalArgumentException: illegal length or offset
at java.net.DatagramPacket.setData(Unknown Source)
at java.net.DatagramPacket.<init>(Unknown Source)
at Source.createConnection(Source.java:44)
at Source.main(Source.java:102)
这是我的代码:
import java.io.*;
import java.net.*;
public class Source {
private DatagramSocket socket = null;
private FE event = null;
private String sourceFilePath = "C:/joe's/file/text/hai.txt";
private String destinationPath = "C:/maria/file2/laboratory/";
private String hostname= "192.168.43.65";
public Source(){ }
public static void main1(String[] args)throws Exception
{
try
{
DatagramSocket s = new DatagramSocket();
byte[]line = new byte[100];
System.out.print("Enter text to send");
int len = System.in.read(line);
InetAddress dest = InetAddress.getByName("192.168.43.65");
DatagramPacket pkt = new DatagramPacket(line,len,dest,16900);
s.send(pkt);
s.close();
}
catch(Exception err){
System.err.println(err);
}
}
public void createConnection() {
try {
socket = new DatagramSocket();
byte[] incomingData = new byte[9999];
event = getFE();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(event);
byte[] data = outputStream.toByteArray();
DatagramPacket sendPacket = new DatagramPacket(data, data.length, 9876);
socket.send(sendPacket);
System.out.println("File sent from AppsA");
DatagramPacket incomingPacket = new DatagramPacket(incomingData,
incomingData.length);
socket.receive(incomingPacket);
String response = new String(incomingPacket.getData());
System.out.println("Response from AppsB:" + response);
Thread.sleep(2000);
System.exit(0);
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public FE getFE() {
FE fileEvent = new FE();
String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1,
sourceFilePath.length());
String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
fileEvent.setDestinationDirectory(destinationPath);
fileEvent.setFilename(fileName);
fileEvent.setSourceDirectory(sourceFilePath);
File file = new File(sourceFilePath);
if (file.isFile())
{
try {
DataInputStream diStream = new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
fileBytes.length - read)) >= 0) {
read = read + numRead;
}
fileEvent.setFileSize(len);
fileEvent.setFileData(fileBytes);
fileEvent.setStatus("Success");
} catch (Exception e) {
e.printStackTrace();
fileEvent.setStatus("Error");
}
}
else {
System.out.println("path specified is not pointing to a file");
fileEvent.setStatus("Error");
}
return fileEvent;
}
public static void main(String[] args)throws Exception {
Source app = new Source();
app.createConnection();
}
}
你的问题在于DatagramPacket的构造函数使用不当:
DatagramPacket sendPacket = new DatagramPacket(data, data.length, 9876);
上面一行是正确的,但是下面的代码:
DatagramPacket incomingPacket = new DatagramPacket(incomingData,
incomingData.length);
应该是这样的:
DatagramPacket incomingPacket = new DatagramPacket(incomingData,
9876);
DatagramPacket 构造函数中的第二个参数是长度。您最初将 sendPacket 上的长度设置为 9876
我想使用 UDP 多播将我的文件发送给我的朋友,但是当我尝试 运行 代码时,输出是这样的:
输出:
Exception in thread "main" java.lang.IllegalArgumentException: illegal length or offset
at java.net.DatagramPacket.setData(Unknown Source)
at java.net.DatagramPacket.<init>(Unknown Source)
at Source.createConnection(Source.java:44)
at Source.main(Source.java:102)
这是我的代码:
import java.io.*;
import java.net.*;
public class Source {
private DatagramSocket socket = null;
private FE event = null;
private String sourceFilePath = "C:/joe's/file/text/hai.txt";
private String destinationPath = "C:/maria/file2/laboratory/";
private String hostname= "192.168.43.65";
public Source(){ }
public static void main1(String[] args)throws Exception
{
try
{
DatagramSocket s = new DatagramSocket();
byte[]line = new byte[100];
System.out.print("Enter text to send");
int len = System.in.read(line);
InetAddress dest = InetAddress.getByName("192.168.43.65");
DatagramPacket pkt = new DatagramPacket(line,len,dest,16900);
s.send(pkt);
s.close();
}
catch(Exception err){
System.err.println(err);
}
}
public void createConnection() {
try {
socket = new DatagramSocket();
byte[] incomingData = new byte[9999];
event = getFE();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(event);
byte[] data = outputStream.toByteArray();
DatagramPacket sendPacket = new DatagramPacket(data, data.length, 9876);
socket.send(sendPacket);
System.out.println("File sent from AppsA");
DatagramPacket incomingPacket = new DatagramPacket(incomingData,
incomingData.length);
socket.receive(incomingPacket);
String response = new String(incomingPacket.getData());
System.out.println("Response from AppsB:" + response);
Thread.sleep(2000);
System.exit(0);
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public FE getFE() {
FE fileEvent = new FE();
String fileName = sourceFilePath.substring(sourceFilePath.lastIndexOf("/") + 1,
sourceFilePath.length());
String path = sourceFilePath.substring(0, sourceFilePath.lastIndexOf("/") + 1);
fileEvent.setDestinationDirectory(destinationPath);
fileEvent.setFilename(fileName);
fileEvent.setSourceDirectory(sourceFilePath);
File file = new File(sourceFilePath);
if (file.isFile())
{
try {
DataInputStream diStream = new DataInputStream(new FileInputStream(file));
long len = (int) file.length();
byte[] fileBytes = new byte[(int) len];
int read = 0;
int numRead = 0;
while (read < fileBytes.length && (numRead = diStream.read(fileBytes, read,
fileBytes.length - read)) >= 0) {
read = read + numRead;
}
fileEvent.setFileSize(len);
fileEvent.setFileData(fileBytes);
fileEvent.setStatus("Success");
} catch (Exception e) {
e.printStackTrace();
fileEvent.setStatus("Error");
}
}
else {
System.out.println("path specified is not pointing to a file");
fileEvent.setStatus("Error");
}
return fileEvent;
}
public static void main(String[] args)throws Exception {
Source app = new Source();
app.createConnection();
}
}
你的问题在于DatagramPacket的构造函数使用不当:
DatagramPacket sendPacket = new DatagramPacket(data, data.length, 9876);
上面一行是正确的,但是下面的代码:
DatagramPacket incomingPacket = new DatagramPacket(incomingData,
incomingData.length);
应该是这样的:
DatagramPacket incomingPacket = new DatagramPacket(incomingData,
9876);
DatagramPacket 构造函数中的第二个参数是长度。您最初将 sendPacket 上的长度设置为 9876