Java Reference
In-Depth Information
Tip To create a connection to a local program running on the client machine, set the
server-name equal to 127.0.0.1 . This is done within the source listing for this
recipe. Usually local connections such as this are used only for testing purposes.
public class SocketClient {
public static Socket socket = null;
public static PrintWriter out;
public static BufferedReader in;
public static void main(String[] args) {
createConnection("127.0.0.1", 1234);
}
public static void createConnection(String host, int
port) {
try {
//Create socket connection
socket = new Socket(host, port);
// Obtain a handle on the socket output
out = new
PrintWriter(socket.getOutputStream(),
true);
// Obtain a handle on the socket input
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
testConnection();
System.out.println("Closing the
connection...");
out.flush();
out.close();
in.close();
socket.close();
System.exit(0);
} catch (UnknownHostException e) {
System.out.println(e);
Search WWH ::




Custom Search