无法在java中使用UDP发送大量数据

时间:2017-11-02 11:54:07

标签: java sockets udp

我有一个小项目,我需要从客户端的文件中读取一行并将其发送到服务器。以下是我的文件的详细信息。

Jeevans-MacBook-Pro:udp jeevansrikar$ ls -ll number.txt 
-rw-r--r--@ 1 jeevansrikar  staff  5197498 Oct 23 00:04 number.txt
Jeevans-MacBook-Pro:udp jeevansrikar$ head number.txt 
64434
84511
80655
95690
41954
82049
62288
52560
37556
47627

服务器

    import java.io.*;
import java.net.*;

class UDPServer
{
   public static void main(String args[]) throws Exception
      {
         DatagramSocket serverSocket = new DatagramSocket(9883);
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData(), 0, receivePacket.getLength());
                  System.out.println("RECEIVED: " + sentence);
                  InetAddress IPAddress = receivePacket.getAddress();
                  int port = receivePacket.getPort();
                  sendData = sentence.getBytes();
                  DatagramPacket sendPacket =
                  new DatagramPacket(sendData, sendData.length, IPAddress, port);
                  System.out.println(serverSocket.getSendBufferSize());
                  serverSocket.send(sendPacket);
               }

      }
}

客户端:

    import java.io.*;
import java.net.*;

class UDPClient
{
   public static void main(String args[]) throws Exception
   {
      String fileName = "number.txt";
      String line = null;
      DatagramSocket clientSocket = new DatagramSocket();
      InetAddress IPAddress = InetAddress.getByName("localhost");
      byte[] sendData = new byte[1024];
      byte[] receiveData = new byte[1024];
      String modifiedSentence = null;

      try{
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        while((line = bufferedReader.readLine()) != null) {

            sendData = line.getBytes();
            System.out.println("To Server :"+line);
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9883);
            clientSocket.send(sendPacket);
            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);
            modifiedSentence = new String(receivePacket.getData());
            System.out.println("From Server :" + modifiedSentence);


        }


      }
      catch(FileNotFoundException ex) {
          System.out.println(
              "Unable to open file '" + 
              fileName + "'");                
      }
      catch(IOException ex) {
          System.out.println(
              "Error reading file '" 
              + fileName + "'");                  
          // Or we could just do this: 
          // ex.printStackTrace();
      }

      clientSocket.close();
   }
}

我必须使用UDP传输协议将每一行从客户端传输到服务器。发送某些100行的服务器不接受新线路后,数据传输停止。是否有任何UDP缓冲区大小需要增加才能接受大数据传输?

0 个答案:

没有答案