Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a simple socket program

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello, how can i make this program run when a user inputs a website, so that it responds with html code. right now when i run it, it just responds with html code from Id like to make it the output like this:
"Please type in a web address" ->(user types in address, and the website html is returned).

Thanks for any help, here is the code I have:

import java.io.*;
import java.net.*;
/**
* will open up a webserver
*
*/
public class SimpleWebClient {
public static void main(String args[])
{
try
{
// open a client socket connection
Socket clientSocket1 = new Socket(" 80);
System.out.println("Client1: " + clientSocket1);
// web page
getPage(clientSocket1);
}
catch (UnknownHostException uhe)
{
System.out.println("UnknownHostException: " + uhe);
}
catch (IOException ioe)
{
System.err.println("IOException: " + ioe);
}
}
/**
* request a Web page
*
*/
public static void getPage(Socket clientSocket)
{
try
{
DataOutputStream outbound = new DataOutputStream(
clientSocket.getOutputStream() );
BufferedReader inbound = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()) );

outbound.writeBytes("GET / HTTP/1.0\r\n\r\n");

String responseLine;
while ((responseLine = inbound.readLine()) != null)
{
// display to the console
System.out.println(responseLine);
}
outbound.close();
inbound.close();
clientSocket.close();
}
catch (IOException ioe)
{
System.out.println("IOException: " + ioe);
}
}
}
 
Try this as your main method instead of what you have:

public static void main( String args[] )
{
String url = null;
do
{
BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
System.out.print( "Please type in a web address -> " );
try
{
url = in.readLine();
} catch( IOException e )
{
System.err.println( "IOException: " + e );
}

if( url != null && url.equals( "quit" ) )
System.exit( 0 );

try
{
// open a client socket connection Socket clientSocket1 = new Socket( url, 80);
System.out.println("Client1: " + clientSocket1);
// web page getPage(clientSocket1);
} catch (UnknownHostException uhe)
{
System.out.println("UnknownHostException: " + uhe);
}
catch (IOException ioe)
{
System.err.println("IOException: " + ioe);
}
} while( url != null );
}

-gc "I don't look busy because I did it right the first time."
 
sorry if the format of that came out a little messed up.

-gc "I don't look busy because I did it right the first time."
 
Thanks a lot. I tried it and it worked. Only problem is now, that my socket will not close after i get the html code from the url. It asks me again to enter a web address. How can I make it close after I receive the html code?
 
Sorry about that, I didn't look at the code carefully. I got it now (I had to type in "quit" to close the connection) Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top