import java.io.*;
import java.net.*;
public class SocketClient
{
public static void main(String[] args) throws IOException
{
Socket theSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try
{
theSocket = new Socket("www.yahoo.com", 80);
out = new PrintWriter(theSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(theSocket.getInputStream()));
}
catch (UnknownHostException e)
{
System.err.println("Don't know about host: www.yahoo.com.");
System.exit(1);
}
catch (IOException e)
{
System.err.println("Couldn't get I/O for the connection to: www.yahoo.com.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null)
{
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
theSocket.close();
}
}
|