This is more a follow up to a previous question asked earlier however I have modified my original code now, to be able to send a file Instead of text) via a UDP socket with a specific packet size. The program runs perfectly if I specify a 1, 2 or 4 byte packet, however if I try for anything larger, such as a 8, 16 etc, the program grinds to a halt. No exception is thrown in the console, however Netbeans shows a tiny warning symbol in the bottom right hand corner and shows the following.
java.lang.IllegalArgumentException: Contents must be presorted - added value 42318 is less than preceding value 42320
at org.netbeans.core.output2.IntList.add(IntList.java:76)
at org.netbeans.core.output2.AbstractLines.addTabAt(AbstractLines.java:1122)
at org.netbeans.core.output2.OutWriter.doWrite(OutWriter.java:452)
at org.netbeans.core.output2.OutWriter.write(OutWriter.java:506)
at java.io.PrintWriter.write(PrintWriter.java:456)
at java.io.PrintWriter.write(PrintWriter.java:473)
at org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride$Copier.maybeFlush(ForkedJavaOverride.java:350)
at org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride$Copier.access$000(ForkedJavaOverride.java:251)
at org.apache.tools.ant.module.bridge.impl.ForkedJavaOverride$Copier$1.run(ForkedJavaOverride.java:271)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1432)
[catch] at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2044)
Is this error thrown because of the UDP Packets being received out of order by any chance? Is there a way to ignore this? I can tell the program has ceased as the string of "The End" does not appear. Does anyone know why this may be? My code for my client socket is attached below (its a bit of a long one and lots of comments for my own sanity). Many thanks in advance for any guidance.
public class UDPClient extends variable {
// static Integer portNo = 4444;
static Integer byteSize = 16;
public static void main(String[] args) throws Exception { //taken out main from here
SocketForm form = new SocketForm();
File file=null;
long startTime; // Starting time of program, in milliseconds.
long endTime; // Time when computations are done, in milliseconds.
double time;
//get server address
String serverName = "localhost";
if (args.length >= 1)
serverName = args[0];
InetAddress serverIPAddress = InetAddress.getByName(serverName);
//get server port;
int serverPort = form.cliportNo;
if (args.length >= 2)
serverPort = Integer.parseInt(args[1]);
//create socket
DatagramSocket clientSocket = new DatagramSocket();
//get input from keybaord
byte[] sendData = new byte[byteSize];
//BufferedReader inFromUser = new BufferedReader(new InputStreamReader (System.in));
//while (true){
//String sentence = inFromUser.readLine();
startTime = System.currentTimeMillis();
//sendData = sentence.getBytes();
String fileName = "/Users/Andrew/Desktop/pic.jpg";
File f = new File(fileName);
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
System.out.println("Total file size to read in bytes is : " + fis.available());
} catch (IOException e) {}
Path path = Paths.get("/Users/Andrew/Desktop/pic.jpg");
//byte[] data = Fles.readAllBytes(path);
sendData = Files.readAllBytes(path);
try {
for( int index = 0; index < sendData.length ; index += byteSize ) {
DatagramPacket packet = new DatagramPacket( sendData, index, Math.min( byteSize, sendData.length-index ), serverIPAddress, serverPort);
clientSocket.send(packet);
//DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverIPAddress, serverPort);
//receive datagram
byte[] receiveData = new byte [byteSize];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
//print output
String sentenceFromServer = new String(receivePacket.getData());
System.out.println("From Server:" + sentenceFromServer);
}
System.out.println("The End");
}
catch (Exception e) {}
//close client socket
//clientSocket.close();
endTime = System.currentTimeMillis();
time = endTime - startTime;
System.out.println("Time :" + time);
// }
} //end of main
} //end of UDPClient