Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2006

Tip: Looking for answers? Try searching our database.

wait for a socket

Thread view: 
Phat Phuq - 08 Mar 2006 14:59 GMT
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();
    }
   }
Gordon Beaton - 08 Mar 2006 15:26 GMT
> 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
[quoted text clipped - 5 lines]
>    // ******* 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

jcc - 08 Mar 2006 15:38 GMT
In most cases, the response from server has header Content-Length, so
you know the size of response data to read.

In other cases, please refer

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6

http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.4.6
So even your code can read the response data correctly, you still need
to delete the number which identifies the size of each chunk.
Phat Phuq - 08 Mar 2006 16:13 GMT
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.

> In most cases, the response from server has header Content-Length, so
> you know the size of response data to read.
[quoted text clipped - 6 lines]
> So even your code can read the response data correctly, you still need
> to delete the number which identifies the size of each chunk.
Gordon Beaton - 08 Mar 2006 16:18 GMT
> I'm not getting content length from the server on the initial ack.
> I'm doing MIME-based secure EDI as per RFC 3335.
[quoted text clipped - 6 lines]
> 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

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Phat Phuq - 08 Mar 2006 19:59 GMT
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...

>> I'm not getting content length from the server on the initial ack.
>> I'm doing MIME-based secure EDI as per RFC 3335.
[quoted text clipped - 11 lines]
>
> /gordon
Phat Phuq - 08 Mar 2006 16:10 GMT
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.

>> 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
[quoted text clipped - 29 lines]
>
> /gordon


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.