Created
June 15, 2016 09:04
-
-
Save lizettepreiss/361d72288905cd835a2780e63aa36e13 to your computer and use it in GitHub Desktop.
SocketClient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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