Last active
August 29, 2015 14:06
-
-
Save Korilakkuma/1e5c84d779c70b4c0def to your computer and use it in GitHub Desktop.
Socket Client
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 final class SocketClient { | |
| private static final String SERVER_HOST = "localhost"; | |
| private static final int SERVER_PORT = 9999; | |
| public static void main(String[] args) { | |
| if (args.length < 1) { | |
| System.err.println("Require at least 1 argument !!"); | |
| System.exit(1); | |
| } | |
| Socket socket = null; | |
| try { | |
| socket = new Socket(SocketClient.SERVER_HOST, SocketClient.SERVER_PORT); | |
| Reader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); | |
| Writer output = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); | |
| for (String arg : args) { | |
| output.write(arg); | |
| } | |
| output.write('.'); | |
| output.flush(); | |
| char[] buf = new char[4096]; | |
| while (input.read(buf) != -1) { | |
| System.out.println(buf); | |
| } | |
| } catch (UnknownHostException error) { | |
| error.printStackTrace(); | |
| } catch (IOException error) { | |
| error.printStackTrace(); | |
| } finally { | |
| try { | |
| if (socket != null) { | |
| socket.close(); | |
| } | |
| } catch (IOException error) { | |
| error.printStackTrace(); | |
| } | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment