Two Laptop Bag*
The moose likes Sockets and Internet Protocols and the fly likes polling a socket 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 "polling a socket" Watch "polling a socket" New topic
Author

polling a socket

Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Hi,

I have an application that connects to a server, transmits a message and then receives a response from the server. I also need to stay connected to that server to receive a return message from the server for up to 90 seconds. Does anyone have an idea of a simple way to accomplish this?

Thanks.
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Hi Leroy,

Can you explain your problem in more detail? What sort of server are you connecting to?

If you've got to wait for 90 seconds for a response I would recommend creating the connection in a separate Thread, thereby you will not be blocking the client interface whilst waiting for the response.

Jason.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Without knowing any of the circumstances, I'd say a standard TCP Socket might auffice. You can even send serialized Java objects over the connection.

A separate tread is definitely a good idea.


Ping & DNS - my free Android networking tools app
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Read through this post for some ideas.

http://www.coderanch.com/t/206963/sockets/java/Client-Server
Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Here's a little more background on what I'm doing.

I'm connecting to a web server on port 80 and submitting a post.
All data is in bytes, I must read the response in byte form as well.
I then read the immediate response from the server to see if all is ok and then I need to poll the socket for 90 seconds to recieve the response to my post. I then dump all my data to a file to be read in at another point in the program for further processing. Here is what I have for code so far...

private void TransmitData(byte[] header, byte[] data)
{
try
{
// combine header and data for transmission
ByteArrayOutputStream dataout = new ByteArrayOutputStream();
dataout.write(header);
dataout.write(data);
byte [] transdata = dataout.toByteArray();

// set up socket
URL url = new URL("http://myurlgoeshere");
Socket socket = new Socket(url.getHost(), 80);

// send data to server
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
out.write(transdata);
out.flush();

// set up file to capture server response
File serverresponse = new File(testFilePath + "response-" + timeStamp + ".txt");

int buffer_size = 1024;
InputStream my_stream =
new DataInputStream( new BufferedInputStream( socket.getInputStream(), buffer_size ) );

// read data into array
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] tmp = new byte[1024];
int numBytes = 0;

// this is where I would need to put some sort of outer loop in (I think?) to keep polling for 90 seconds
while ( (numBytes = my_stream.read(tmp)) != -1 )
{
baos.write( tmp, 0, numBytes);
}

// ******* end loop?
baos.close();

writeFile (serverresponse,baos);

socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42635
    
  65
Is there a particular reason you can't use HttpURLConnection, which would give you access to HTTP headers and error/status codes? Or do you need to store the HTTP headers along with the actual data?

I'm not sure what you mean by "reading the immediate response and then poll for a further response". The server will close the socket when it is done sending the response, whether that is after 1 second or after 300 seconds.
Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
I can try the httpurlconnection. The only thing I want from the header is the HTTP response code. However, I've been told that I need to keep the socket open and read from it for up to another 90 seconds in order to receive a MDN from the server.

This is the initial response from the server

HTTP/1.1 200 ok
Date: Wed, 08 Mar 2006 16:37:23 GMT
Server: IBM_HTTP_Server/1.3.6.2 Apache/1.3.7-dev (Unix)
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Connection: close
Content-Type: text/plain

<HTML><BODY><H1>200 OK</H1>Your transaction <<1141835830437@myplace.com>> has been accepted.</BODY></HTML>

I'm supposed to keep polling the socket until my MDN comes down the pipe. Do I need to create a second inputstream for this?
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
HTTP is a stateless protocol so as soon as the web server sends a response the socket connection will be closed.

I think from what you are describing is that the appliction on the web server will receive a initial request which will fire of some process in a separate thread which may take 90 secs and then immediately returns a response whilst the process is still running.

You then need to make further requests to the web server to see if the process has completed.

If this is the case you would need some sort of loop that would make a request, check the response, if the web server responds that the process hasn't completed you could sleep for a period and then try again.

The only problem you have is that you will need to provide some kind of session id (i.e cookie) may be your transaction id? to enable the web server to link your subsequent requests with your initial request.

So you would need to do something like....





Jason.

[ March 08, 2006: Message edited by: Jason Moors ]

[ March 08, 2006: Message edited by: Jason Moors ]
[ March 08, 2006: Message edited by: Jason Moors ]
Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Thanks Jason...

This appears to be something along the line that I will need as opposed to a plain old socket connection. Can you point me in the direction of a decent sample I can pillage? I've been googling all day with little success.
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
I don't have any examples but I don't think it should take you long to pull something together. Just search google for examples of the HttpUrlConnection and apply the pseudocode above.

You do however need to determine what error codes can be returned by the server (to determine if the process has completed) and also what you use as a sessionid. i.e does the web server provide a sessionid with the response header which you need to add to your additional requests.

Let me know if you get stuck!

Jason.
Leroy Lemon
Greenhorn

Joined: Dec 15, 2005
Posts: 21
Thanks to everyone for their help.

Being new to the java internet scene, I was a little confused when my supplier (the guy with the app server) told me to connect, transmit my transaction and then wait 90 seconds before reading a MDN from the server.
It turns out that I needed to disconnect and then post another request for a download of messages which in turn would contain my MDN and my transaction summary.

I was able to achieve what I needed to do with a socket and manually constructing my header and body of the message.

However when I used the HttpURLConnection, I had and issue when I tried to request a download of transactions. I was getting a "Bogus Chunk Size" error when reading from the stream. Does anyone have any idea why I would be getting this?

Like I said, I'm already able to do what I need to do with a socket, but I would like to take advantage of the HttpURLConnection because of the extra features. If someone could provide some insite on how to properly set the chunk size, I'd appreciate it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polling a socket