Communications between two or more machines

M

Matthew

Hello, I have a problem here. I have 2 machines. 1 is a webserver and
the other is the client machine. The client machine calls the webserver
to execute a particular application. Now the problem is my client
machine (using Java's HTTP.... classes) keeps on waiting until the
process in the web server finish executing. The code below calls the
webserver:

public void caller()
{

String host = "www.google.com";
String content = "hello...";

try
{
URL url = new URL(host);

URLConnection uc = url.openConnection ();

if (!(uc instanceof HttpURLConnection))
{
System.err.println ("Wrong connection type");
return;
}

uc.setDoOutput (true);
uc.setUseCaches (true);
HttpURLConnection hc = (HttpURLConnection) uc;
hc.setRequestMethod ("POST");
OutputStream os = uc.getOutputStream ();
DataOutputStream dos = new DataOutputStream (os);
dos.writeBytes (content);

dos.flush ();
dos.close ();

uc.connect();

// -- HERE (Get the reply of the webserver)
InputStream is = uc.getInputStream ();
int ch;
while ((ch = is.read ()) != -1)
System.out.print ((char) ch);

is.close ();
// -- HERE (End)
}
catch (Exception e)
{
System.out.println(ipAddr+" "+e.getMessage());
}
}

Once, I erased the portion bet HERE but the apps in the webserver did
not work. Please do reply. thanks.
 
M

Martin Gregorie

Matthew said:
Hello, I have a problem here. I have 2 machines. 1 is a webserver and
the other is the client machine. The client machine calls the webserver
to execute a particular application. Now the problem is my client
machine (using Java's HTTP.... classes) keeps on waiting until the
process in the web server finish executing. The code below calls the
webserver:

public void caller()
{

String host = "www.google.com";
String content = "hello...";

try
{
URL url = new URL(host);

URLConnection uc = url.openConnection ();

if (!(uc instanceof HttpURLConnection))
{
System.err.println ("Wrong connection type");
return;
}

uc.setDoOutput (true);
uc.setUseCaches (true);
HttpURLConnection hc = (HttpURLConnection) uc;
hc.setRequestMethod ("POST");
OutputStream os = uc.getOutputStream ();
DataOutputStream dos = new DataOutputStream (os);
dos.writeBytes (content);

dos.flush ();
dos.close ();

uc.connect();

// -- HERE (Get the reply of the webserver)
InputStream is = uc.getInputStream ();
int ch;
while ((ch = is.read ()) != -1)
System.out.print ((char) ch);

is.close ();
// -- HERE (End)
}
catch (Exception e)
{
System.out.println(ipAddr+" "+e.getMessage());
}
}

Once, I erased the portion bet HERE but the apps in the webserver did
not work. Please do reply. thanks.
Try connecting before sending the message to the server.
 

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

Forum statistics

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

Latest Threads

Top