Created
April 14, 2012 20:15
-
-
Save colin-haber/2387722 to your computer and use it in GitHub Desktop.
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.net.*; | |
| import java.io.*; | |
| public class Client { | |
| public static void main(String[] args) { | |
| new Client(); | |
| } | |
| private final InetAddress host; | |
| private final Socket port; | |
| private final PrintStream out; | |
| private final BufferedReader in; | |
| private Client() { | |
| /* | |
| * Get IP address and set port | |
| */ | |
| InetAddress host = null; | |
| Socket port = null; | |
| try { | |
| host = InetAddress.getByName("localhost"); | |
| port = new Socket(host, 1337); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| this.host = host; | |
| this.port = port; | |
| /* | |
| * Get input and output streams | |
| */ | |
| PrintStream out = null; | |
| BufferedReader in = null; | |
| try { | |
| out = new PrintStream(port.getOutputStream()); | |
| in = new BufferedReader(new InputStreamReader(port.getInputStream())); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| this.out = out; | |
| this.in = in; | |
| int read = 0; | |
| do { | |
| try { | |
| read = in.read(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| System.out.print((char) read); | |
| } while (read != -1); | |
| } | |
| } |
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 Connection { | |
| private Server server; | |
| private Socket port; | |
| private PrintStream out; | |
| private BufferedReader in; | |
| protected Connection(Server server, Socket port) { | |
| System.out.println("Connection established at " + port.getInetAddress().getCanonicalHostName()); | |
| this.server = server; | |
| this.port = port; | |
| OutputStream out = null; | |
| InputStream in = null; | |
| try { | |
| out = this.port.getOutputStream(); | |
| in = this.port.getInputStream(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| this.out = new PrintStream(out); | |
| this.in = new BufferedReader(new InputStreamReader(in)); | |
| this.out.println("Tittysprinkles"); | |
| } | |
| } |
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.net.*; | |
| import java.util.ArrayList; | |
| import java.io.*; | |
| public class Server { | |
| public static void main(String[] args) { | |
| new Server(); | |
| } | |
| private final InetAddress localhost; | |
| private final ServerSocket port; | |
| private ArrayList<Connection> clients; | |
| private Server server; | |
| private Server() { | |
| clients = new ArrayList<Connection>(); | |
| InetAddress localhost = null; | |
| ServerSocket port = null; | |
| try { | |
| localhost = InetAddress.getLocalHost(); | |
| port = new ServerSocket(1337); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| this.localhost = localhost; | |
| this.port = port; | |
| while (!this.port.isClosed()) { | |
| try { | |
| final Socket s = this.port.accept(); | |
| new Thread( | |
| new Runnable() { | |
| @Override | |
| public void run() { | |
| clients.add(new Connection(server, s)); | |
| } | |
| } | |
| ).start(); | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment