Results 1 to 17 of 17
  1. #1
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default how to receive all data with UDP client

    Hallo ,
    am new in Java Programming and need some help .
    I wan to read all data sending from a UDP server but i have many problem in this way. The fact is , when i send some command (String text) to the server, he send a big string as response.
    My question are :

    1- how can i read all this data sending from server before closing the socket connection. ( i think the client have to stay in receive mode until he received all data, but how to do this ? )

    2- when a read a answer how can i know the end of the message sending from the server

    3- If somebody has some example, they will help me

    Thank you for you answer and sorry for my bad english

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Does the server send any indication or details on how long its response is?

  3. #3
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Yes the server send a character (\n) at the end each response . so when this character come the Client know that the message end . how to implement this ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Keep reading packets until you get the \n character.
    However the packets could be out of order so the packet with the \n could come before some other parts of the response.

  5. #5
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    thanks for you answer but the real problem is :
    the server answer by sending many packets with different byte length how can i read each packet until the end of the message and print this for the user each time i receive a part of the message.

    thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    What does the API doc say about reading/receiving packets?
    Is there a problem with continuing to read and display the packets until finding the ending \n?

  7. #7
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Yes this is my problem now , to continuing read each packets until the character \n come.
    how can i implements this ?
    now i writed some code but it still block on the receive method and nothing appear on the screen.
    here is my code now :



    public class UDPClient {



    public static void main(String args[]) throws Exception {

    DatagramSocket clientSocket = new DatagramSocket();
    InetAddress IPAddress = InetAddress.getByName("192.168.1.9");

    byte[] receiveData = new byte[1024];
    String end = String.valueOf((char)Integer.parseInt("0A",16));
    String sentence = "hallo"+end;
    byte[] sendData = new byte[sentence.getBytes().length];
    sendData = sentence.getBytes();
    DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 33456);
    clientSocket.send(sendPacket);
    System.out.println("sendig message :"+sentence +" in bytes : "+sendData);

    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    System.out.println("prog need now to receive....");
    do {
    clientSocket.setSoTimeout(5000);
    clientSocket.receive(receivePacket);
    String modifiedSentence = new String(receivePacket.getData());
    System.out.println("FROM SERVER response :" + modifiedSentence);

    }

    while (new String(receivePacket.getData()).contains(end)== true) ;

    System.out.println("receive is executed....");

    clientSocket.close();
    }

    }


    when the Programm reach the red marked Line , nothing more occur
    I also trie without do and while but its the same result
    Last edited by simplo; 08-18-2010 at 04:20 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Does the timeout throw an exception after 5 seconds? Use a try{}catch to handle it in your code instead of throwing it away at the main method level.

    Have you tried it with other sites or with your own server to validate your code?
    Perhaps the server you are sending to is not responding.

  9. #9
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    yes the exception come after 5 s . i also use try {} and catch but i have the same result.
    the server send the response i can see this with wireshark . but my problem is : i can´t read the response and print it out. the response a coming in many udp packets with different size

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Have you ever read any data from the server using any parts of this code?
    If you have read data before with this code, what has changed that you are now unable to read ANY data?

  11. #11
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    no i never read data with my code . I see in wireshark that the server send the response but i cant´t read it with my code

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    the server send the response
    Is the response because of your sending a message to the server via the DatagramSocket send method?

  13. #13
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    Yes. the server only response when i send my message via DatagramSocket

  14. #14
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    I solved my problem , it was a problem in a port assigning. thank you

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Could you explain your solution a bit more please?

  16. #16
    simplo is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default

    The problem was the following code :
    DatagramSocket clientSocket = new DatagramSocket();
    it schould be :
    DatagramSocket clientSocket = new DatagramSocket(port);

    I send my Datagramm on a specified port on the server and this have to be also set by creating the socket at Client side , then the server response on the port he pick from DatagrammPacket . in my case the server sent the response on port 33310 but this would be not setting by creating the socket.

    hope i was a bit more precise

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

Similar Threads

  1. Client cannot receive inputstream from server
    By chyrl in forum Networking
    Replies: 2
    Last Post: 08-29-2010, 05:13 PM
  2. Sending array of data to client
    By k80sg in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 07:48 AM
  3. Replies: 0
    Last Post: 12-02-2008, 10:55 AM
  4. Replies: 1
    Last Post: 01-28-2008, 10:49 AM
  5. PC receive data via Bluetooth using JAVA
    By toncoolx in forum New To Java
    Replies: 0
    Last Post: 11-27-2007, 04:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •