Results 1 to 17 of 17
- 08-17-2010, 11:08 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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
- 08-18-2010, 12:01 AM #2
Does the server send any indication or details on how long its response is?
- 08-18-2010, 07:03 AM #3
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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 ?
- 08-18-2010, 12:57 PM #4
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.
- 08-18-2010, 01:48 PM #5
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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
- 08-18-2010, 03:02 PM #6
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?
- 08-18-2010, 03:25 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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 resultLast edited by simplo; 08-18-2010 at 04:20 PM.
- 08-18-2010, 03:50 PM #8
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.
- 08-18-2010, 04:18 PM #9
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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
- 08-18-2010, 09:38 PM #10
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?
- 08-18-2010, 11:04 PM #11
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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
- 08-18-2010, 11:43 PM #12
Is the response because of your sending a message to the server via the DatagramSocket send method?the server send the response
- 08-19-2010, 07:53 AM #13
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
Yes. the server only response when i send my message via DatagramSocket
- 08-19-2010, 09:23 AM #14
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
I solved my problem , it was a problem in a port assigning. thank you
- 08-19-2010, 01:30 PM #15
Could you explain your solution a bit more please?
- 08-19-2010, 02:04 PM #16
Member
- Join Date
- Aug 2010
- Posts
- 14
- Rep Power
- 0
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
- 08-19-2010, 02:14 PM #17
Similar Threads
-
Client cannot receive inputstream from server
By chyrl in forum NetworkingReplies: 2Last Post: 08-29-2010, 05:13 PM -
Sending array of data to client
By k80sg in forum New To JavaReplies: 0Last Post: 03-16-2010, 07:48 AM -
Java code that allows me to make and receive calls, send and receive sms
By nareshbabu@live.in in forum NetworkingReplies: 0Last Post: 12-02-2008, 10:55 AM -
how to send and receive data from servlet to applet Continuously ??
By jega_ms in forum Java ServletReplies: 1Last Post: 01-28-2008, 10:49 AM -
PC receive data via Bluetooth using JAVA
By toncoolx in forum New To JavaReplies: 0Last Post: 11-27-2007, 04:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks