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();
}
}
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();
}
}