Java Reference
In-Depth Information
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This program creates a
ServerSocket
and repeatedly accepts connections
to it. The
Socket
obtained for each connection is queried for an input
stream and the data from that socket is read from the stream and writ-
ten to the standard output stream, printing whatever bytes it receives.
A client that shipped its standard input to the server might look like this:
import java.net.*;
import java.io.*;
public class WriteOutput {
public static void main(String[] args)
throws IOException
{
String host = args[0];
Socket sock = new Socket(host, AcceptInput.PORT);
OutputStream out = sock.getOutputStream();
int ch;
while ((ch = System.in.read()) != -1)
out.write(ch);
out.close();
}
}
Once we have the actual
Socket
connection, I/O is performed, like all
other I/O, using an appropriate input or output stream.
A
URL
object stores a
URL
, providing methods to examine and set its vari-
ous parts (protocol, host, port number, and file). A
URL
object simply