简体   繁体   English

套接字无法完成发送4字节数据包

[英]Socket Failing to finish sending 4Byte Packets

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. 这是对先前提出的上一个问题的补充,但是我现在修改了原始代码,以便能够通过具有特定数据包大小的UDP套接字发送文件(而不是文本)。 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. 如果指定1、2或4字节数据包,则程序可以完美运行,但是,如果尝试使用更大的数据包(例如8、16等),则程序将停止运行。 No exception is thrown in the console, however Netbeans shows a tiny warning symbol in the bottom right hand corner and shows the following. 控制台中不会引发异常,但是Netbeans在右下角显示一个小的警告符号,并显示以下内容。

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? 是否由于偶然接收到UDP数据包而引发此错误? Is there a way to ignore this? 有没有办法忽略这一点? I can tell the program has ceased as the string of "The End" does not appear. 我可以看出程序已经停止,因为没有出现“ The End”字符串。 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

Solved. 解决了。 Was running 7.3 of Netbeans. 正在运行7.3的Netbeans。 It is a known issue in 7.3 and has been addressed in the latest revision of 7.4. 这是7.3中的一个已知问题,并且在7.4的最新修订版中已解决。 Solution : Update to Netbeans 7.4. 解决方案:更新到Netbeans 7.4。 Now purring like a cat. 现在像猫一样发出呼pur声。 Thanks to all those who tried to help with this! 感谢所有尝试提供帮助的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM