Sending some lines of string to server (UDP) in java

2020-02-15 java sockets udp datagram udpclient

I try to send a string consist of some lines in one datagram packet from my client to server ,my problem is when I send the string only first line will send to the server ,I want to send one line then after 10 second send another line and another, how can I solve it??? this is my client code for sending string :

    String msge="null";
    String atCurrentLine = null;
     try (BufferedReader cl = new BufferedReader(
     new FileReader("Client1.txt"))) { 
     while ((atCurrentLine = cl.readLine()) != null) {
            msge=atCurrentLine;
    System.out.println(msge);

        } 


    } catch (IOException e) {
     e.printStackTrace();
  } 


    DatagramSocket skt = null;
    try {
      skt = new DatagramSocket();
      byte[] b = msge.getBytes();
      InetAddress host = InetAddress.getByName("localhost");   
      int cl = 6700;                       
      DatagramPacket request = new DatagramPacket(b,b.length,host,cl);  
      skt.send(request);                   

and this code for server to receive:

     DatagramSocket skt = null;
    try {
       skt = new DatagramSocket(6700);
       byte [] buffer = new byte[1000];
        while (true) {                
          DatagramPacket request = new DatagramPacket(buffer,buffer.length);
          skt.receive(request);

          String [] arrayMsg = (new String(request.getData())).split("       ");
          sms=arrayMsg[0];
          System.out.println("received from client :"+arrayMsg[0]);

Answers

Related