File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Socket Problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Socket Problems" Watch "Socket Problems" New topic

Socket Problems

Frank hogan

Joined: May 01, 2006
Posts: 3
I got following code to send a text message to a smalltalk server connected over TCP/Socket. Its a simple request-response action, Java engine send message ( text , actually a method and parameters) and Smalltalk engine receives and converts text into a method with parameters and excutes and sends back the XML response.

The problems I am having is I recieve Junk response sometimes thats not related, or sometimes I get incorrect response, or a delayed response after
some time.
Any thoughts?

private synchronized Element send(User user, String message) throws Exception {

new java.util.Date(System.currentTimeMillis()).toString()
+ " Engine sending "
+ user.getUserId() + " " + message);
if(DESTINATION == null) throw new Exception("EEngine has not been properly configured ");
[ May 09, 2006: Message edited by: Frank hogan ]
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Well you seem to be using DatagramSockets and that implies many things that you have to take into account.

First, you must know that the maximum amount of data transmitted in a DatagramPacket is 65,507 bytes.

Hence, you must be sure the XML file you are expecting to receive is smaller than that.

If not (which might be be your problem) you will have to send it in chunks of data. As DatagramSocket uses UDP (not TCP) protocol, you cannot ensure that the subsequent packages are going to arrive in order, worst even, if you are using multithreading programming to read the packages, one of you threads may perfectly be reading packages of another's thread request.

What I mean is that DatagramSocket can receive data from different senders connected to the same host and port. It is not like TCP sockets, which by means of Streams the comunication channel is controlled between client and server.

Finally, UDP sockets are not secure, which means that your data may perfectly never get to the other side and your DatagramSocket will never even get acquainted of that.

Maybe you should use TCP sockets instead.

At least that's what I think it is happening. What do you guys think?

[ May 09, 2006: Message edited by: Edwin Dalorzo ]
[ May 10, 2006: Message edited by: Edwin Dalorzo ]
Frank hogan

Joined: May 01, 2006
Posts: 3
Hi Edwin,
Thanks for response. But I am using TCP sockets and I/O streams and BufferedReaders to read data.
I agree. Here's the link:
subject: Socket Problems
It's not a secret anymore!