wait for a socket

P

Phat Phuq

Hello,

I am connecting to a web server and submitting a data request. The web
server initially responds with an acknowledgment and then I have to wait for
the result from my data request. I have up to 90 seconds to wait before
closing the socket.

All data is transmitted in bytes and also is read in bytes.

My question is, after my inital read from the socket, how do I keep polling
the socket for the second chunk of data that will come from the server?

here is what I have 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://myconnectionhere");
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 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();

// dump result to file for processing
writeFile (serverresponse,baos);

socket.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
 
G

Gordon Beaton

My question is, after my inital read from the socket, how do I keep
polling the socket for the second chunk of data that will come from
the server?

[ skipping to relevant part of code ]
// this is where I would need to put some sort of 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();

As written, the above read loop will read *all* of the data - the
first and second and any additional chunks - send by the server, since
it continues to read until EOF is reached (when the server closes the
connection).

If you want to read the chunks separately, you need to examine the
data to determine when you've reached the end of the first one. Then
to read the second chunk, you simply continue reading from the stream.

The timeout doesn't really change anything. Set a short timeout (1 or
2 seconds perhaps) with Socket.setSoTimeout(). That will to prevent
read() from hanging indefinately if no data is arriving and allow you
to check after each call to read() whether your 90 seconds have
passed.

/gordon
 
P

Phat Phuq

To be more specific about the nature of my post to the server, it is a
transaction request using MIME-based Secure EDI.

The server initally responds with a standard ack (http code 200 if
successful) and then will send a Message Delivery Notification (MDN) as per
RFC 3335. This response can take up to 90 seconds to process on the server.

And yes, I do receive an eof before this second transmission from the
server.
So what I'm trying to achieve is some sort of polling loop (I'm not
concerned about efficiency right now, I just need to get it to work) so I
can read all the data and dump it to a file for processing.


Gordon Beaton said:
My question is, after my inital read from the socket, how do I keep
polling the socket for the second chunk of data that will come from
the server?

[ skipping to relevant part of code ]
// this is where I would need to put some sort of 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();

As written, the above read loop will read *all* of the data - the
first and second and any additional chunks - send by the server, since
it continues to read until EOF is reached (when the server closes the
connection).

If you want to read the chunks separately, you need to examine the
data to determine when you've reached the end of the first one. Then
to read the second chunk, you simply continue reading from the stream.

The timeout doesn't really change anything. Set a short timeout (1 or
2 seconds perhaps) with Socket.setSoTimeout(). That will to prevent
read() from hanging indefinately if no data is arriving and allow you
to check after each call to read() whether your 90 seconds have
passed.

/gordon
 
P

Phat Phuq

I'm not getting content length from the server on the initial ack.
I'm doing MIME-based secure EDI as per RFC 3335.

The server is responding with an eof after the initial ack, I need to keep
the socket open and poll the socket for up to 90 seconds until I receive the
MDN (Message Delivery Notification) from the server.

I'm just looking for a simple way to keep looping until I get the second
response.

Thanks.
 
G

Gordon Beaton

I'm not getting content length from the server on the initial ack.
I'm doing MIME-based secure EDI as per RFC 3335.

The server is responding with an eof after the initial ack, I need
to keep the socket open and poll the socket for up to 90 seconds
until I receive the MDN (Message Delivery Notification) from the
server.

I'm just looking for a simple way to keep looping until I get the
second response.

You have conflicting requirements. If you've really received EOF, the
socket *cannot* be used to receive more data.

/gordon
 
P

Phat Phuq

I think I've found my answer. It appears that I should be using an
HttpURLConnection and streaming my data through that.
It will allow me to keep querying the server until my message request has
been processed.

I'll go back into my java hole now and start crunching some code. Later...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top