Skip to content

Instantly share code, notes, and snippets.

@colin-haber
Created April 14, 2012 20:15
Show Gist options
  • Select an option

  • Save colin-haber/2387722 to your computer and use it in GitHub Desktop.

Select an option

Save colin-haber/2387722 to your computer and use it in GitHub Desktop.
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);
}
}
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");
}
}
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