Skip to content

Commit c3b27d4

Browse files
committed
Uploaded sockets package
1 parent 256453c commit c3b27d4

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed

src/sockets/AdvancedConnection.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package sockets;
2+
3+
import java.io.PrintStream;
4+
import java.net.InetAddress;
5+
import java.net.ServerSocket;
6+
import java.net.Socket;
7+
import java.util.Scanner;
8+
9+
public class AdvancedConnection {
10+
static final int PORT = 8080;
11+
static Scanner in;
12+
public static void main(String[] args) throws Exception {
13+
InetAddress inetAddress = InetAddress.getLocalHost();
14+
System.out.println("IP Address: " + inetAddress.getHostAddress());
15+
System.out.println("Host Name: " + inetAddress.getHostName());
16+
17+
System.out.println("What do you want to do");
18+
System.out.println("1. Host a server");
19+
System.out.println("2. Connect to a server");
20+
21+
in = new Scanner(System.in);
22+
23+
switch(in.nextInt()){
24+
case 1:
25+
hostServer();
26+
break;
27+
case 2:
28+
System.out.print("Give me an ip to connect to: ");
29+
connect(in.next());
30+
break;
31+
default:
32+
System.out.println("Not an option :/");
33+
break;
34+
}
35+
}
36+
37+
public static void hostServer() throws Exception {
38+
InetAddress inetAddress = InetAddress.getLocalHost();
39+
System.out.println("Started a new server on "+ inetAddress.getHostAddress());
40+
ServerSocket ss = new ServerSocket(PORT);
41+
42+
Socket s = ss.accept();
43+
44+
System.out.println("Connection from " + s.getRemoteSocketAddress());
45+
System.out.println();
46+
47+
PrintStream sout = new PrintStream(s.getOutputStream());
48+
49+
// Clear the newline from the previous call to in.nextInt();
50+
in.nextLine();
51+
while(true){
52+
System.out.print("> ");
53+
String line = in.nextLine();
54+
55+
sout.println(line);
56+
}
57+
58+
}
59+
60+
public static void connect(String ip) throws Exception{
61+
Socket s = new Socket(ip, PORT);
62+
63+
System.out.println("Connected to " + s.getRemoteSocketAddress());
64+
System.out.println();
65+
66+
Scanner sin = new Scanner(s.getInputStream());
67+
while(true){
68+
69+
String line = sin.nextLine();
70+
71+
System.out.println("Server > " + line);
72+
}
73+
}
74+
}

src/sockets/BasicConnection.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sockets;
2+
3+
import java.net.InetAddress;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
import java.util.Scanner;
7+
8+
public class BasicConnection {
9+
static final int PORT = 8080;
10+
public static void main(String[] args) throws Exception {
11+
InetAddress inetAddress = InetAddress.getLocalHost();
12+
System.out.println("IP Address: " + inetAddress.getHostAddress());
13+
System.out.println("Host Name: " + inetAddress.getHostName());
14+
15+
System.out.println("What do you want to do");
16+
System.out.println("1. Host a server");
17+
System.out.println("2. Connect to a server");
18+
19+
Scanner in = new Scanner(System.in);
20+
21+
switch(in.nextInt()){
22+
case 1:
23+
hostServer();
24+
break;
25+
case 2:
26+
System.out.println("Give me an ip to connect to: ");
27+
connect(in.next());
28+
break;
29+
default:
30+
System.out.println("Not an option :/");
31+
break;
32+
}
33+
}
34+
35+
public static void hostServer() throws Exception {
36+
InetAddress inetAddress = InetAddress.getLocalHost();
37+
System.out.println("Started a new server on "+ inetAddress.getHostAddress());
38+
ServerSocket ss = new ServerSocket(PORT);
39+
40+
Socket s = ss.accept();
41+
42+
System.out.println("Connection from " + s.getRemoteSocketAddress());
43+
}
44+
45+
public static void connect(String ip) throws Exception{
46+
Socket s = new Socket(ip, PORT);
47+
48+
System.out.println("Connected to " + s.getRemoteSocketAddress());
49+
}
50+
}

src/sockets/Challenge.java

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package sockets;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.io.PrintStream;
7+
import java.net.InetAddress;
8+
import java.net.ServerSocket;
9+
import java.net.Socket;
10+
import java.util.Random;
11+
import java.util.Scanner;
12+
13+
public class Challenge {
14+
static final int PORT = 8080;
15+
static Random rand = new Random();
16+
static final int superSecureSecret = getSecureSecret(18);
17+
18+
public static void main(String[] args) throws Exception{
19+
InetAddress inetAddress = InetAddress.getLocalHost();
20+
System.out.println("Started a new server on "+ inetAddress.getHostAddress());
21+
ServerSocket ss = new ServerSocket(PORT);
22+
23+
while(true){
24+
Socket s = ss.accept();
25+
26+
new Thread(() -> process(s)).start();
27+
}
28+
}
29+
30+
public static void process(Socket s) {
31+
try{
32+
Scanner sin = new Scanner(s.getInputStream());
33+
PrintStream sout = new PrintStream(s.getOutputStream());
34+
35+
String input = sin.nextLine();
36+
String status = input.split(" ")[0];
37+
String name = input.split(" ")[1];
38+
39+
switch (status) {
40+
case "TEST":
41+
sout.println("PONG");
42+
break;
43+
case "LOG":
44+
String debug = sin.nextLine();
45+
46+
System.out.println("(" + s.getRemoteSocketAddress() + ") " + name + " > " + debug);
47+
48+
sout.println("Success");
49+
break;
50+
case "CMD":
51+
int code = Integer.parseInt(sin.nextLine());
52+
String cmd = sin.nextLine();
53+
54+
if (code == superSecureSecret) {
55+
System.out.println("(" + s.getRemoteSocketAddress() + ") " + name + " executing " + cmd);
56+
57+
Process proc = Runtime.getRuntime().exec(cmd);
58+
BufferedReader stdInput = new BufferedReader(new
59+
InputStreamReader(proc.getInputStream()));
60+
61+
String str = null;
62+
while ((str = stdInput.readLine()) != null) {
63+
System.out.println("Cmd > " + str);
64+
}
65+
66+
stdInput.close();
67+
}
68+
case "RAND":
69+
int len = Integer.parseInt(sin.nextLine());
70+
byte[] data = new byte[len];
71+
72+
rand.nextBytes(data);
73+
74+
sout.println(new String(data));
75+
76+
break;
77+
}
78+
}catch(Exception e){
79+
e.printStackTrace();
80+
}
81+
82+
try{
83+
s.close();
84+
}catch(IOException ioe){
85+
System.err.println("Failed to close socket");
86+
}
87+
}
88+
89+
public static short getSecureSecret(int quantumBitLength){
90+
if(quantumBitLength == 0) return (short) rand.nextInt();
91+
return (short) (getSecureSecret(quantumBitLength - 1) * getSecureSecret(quantumBitLength - 1));
92+
}
93+
}

src/sockets/ChallengeClient.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sockets;
2+
3+
import java.io.PrintStream;
4+
import java.net.Socket;
5+
import java.util.Scanner;
6+
7+
public class ChallengeClient {
8+
static final String USERNAME = System.getProperty("user.name");
9+
public static void main(String[] args) throws Exception {
10+
Scanner in = new Scanner(System.in);
11+
12+
System.out.print("Give me an ip to connect to: ");
13+
14+
Socket s = new Socket(in.next(), Challenge.PORT);
15+
16+
System.out.println("Connected to " + s.getRemoteSocketAddress());
17+
System.out.println();
18+
19+
Scanner sin = new Scanner(s.getInputStream());
20+
PrintStream sout = new PrintStream(s.getOutputStream());
21+
22+
// Your code below
23+
24+
sout.println("TEST " + USERNAME);
25+
26+
// Your code above
27+
28+
while(sin.hasNextLine()) {
29+
String line = sin.nextLine();
30+
31+
System.out.println("Server> " + line);
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)