Skip to content

Instantly share code, notes, and snippets.

@lizettepreiss
Created June 15, 2016 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizettepreiss/361d72288905cd835a2780e63aa36e13 to your computer and use it in GitHub Desktop.
Save lizettepreiss/361d72288905cd835a2780e63aa36e13 to your computer and use it in GitHub Desktop.
SocketClient
import java.io.*;
import java.net.*;
public class Client {
public static void main(String args[]) {
new Client(args[0]);
}
public Client(String host) {
Socket socket;
try {
socket = new Socket(host, Server.PORT_NUMBER);
}
catch(UnknownHostException ex) {
System.out.println(host + " is not a valid host name.");
return;
}
catch(IOException ex) {
System.out.println(“Error communicating with ” + host);
return;
}
// … initialize model, GUI, etc. ...
InputStream in = null;
OutputStream out = null;
try {
in = socket.getInputStream();
out = socket.getOutputStream();
// ... do useful stuff ...
}
finally {
try {
in.close();
out.close();
socket.close();
}
catch(IOException ex) {
// not much can be done ...
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment