Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Simple socket connection

  • Loading branch information...
commit 21107c1b6982ce8bf4b66cf42c0881aacf48430d 1 parent 361d32e
Pavan Kumar Sunkara authored September 14, 2011
47  LamportClock/Agent.java
... ...
@@ -1,32 +1,41 @@
1 1
 import java.io.*;
  2
+import java.util.*;
  3
+import java.net.*;
2 4
 
3 5
 public class Agent {
4  
-
5  
-	private static String nodeName = "";
6  
-
  6
+	private static String nodeName;
7 7
 	private static FileWriter fileStream;
  8
+	private static Socket pentagonSocket;
  9
+	private static ObjectOutputStream pentagonOutput;
  10
+	private static ObjectInputStream pentagonInput;
8 11
 
9  
-	public static void main(String[] args) {
10  
-		if(args.length!=1) {
11  
-			System.out.println("Usage: Agent <codename>");
  12
+	public static void main(String[] args) throws Exception {
  13
+		if(args.length!=2) {
  14
+			System.out.println("Usage: Agent <codename> <port>");
12 15
 			System.exit(1);
13 16
 		}
14 17
 		nodeName = args[0];
15 18
 
16  
-		report("Initialised mission SuperSecret!");
17  
-		report("Hacked the super computer!");
18  
-		report("Finished mission SuperSecret!");
19  
-	}
  19
+		pentagonSocket = new Socket("localhost", 5000);
  20
+		pentagonOutput = new ObjectOutputStream(pentagonSocket.getOutputStream());
  21
+		pentagonInput = new ObjectInputStream(pentagonSocket.getInputStream());
20 22
 
21  
-	private static void report(String msg) {
22  
-		try {
23  
-			fileStream = new FileWriter("report", true);
24  
-			fileStream.write("Agent " + nodeName + " → " + msg + "\n");
25  
-			fileStream.close();
26  
-			Thread.sleep(2000);
27  
-		} catch (Exception e) {
28  
-			e.printStackTrace();
29  
-		}
  23
+		pentagonOutput.writeObject("Agent " + nodeName + " in place.");
  24
+		pentagonOutput.writeObject("Agent " + nodeName + " ready to proceed.");
  25
+
  26
+		pentagonInput.close();
  27
+		pentagonOutput.close();
  28
+		pentagonSocket.close();
  29
+
  30
+		report("Initialising mission SuperSecret!");
  31
+		report("Hacking the uber super computer!");
  32
+		report("Finishing mission SuperSecret!");
30 33
 	}
31 34
 
  35
+	private static void report(String msg) throws Exception {
  36
+		fileStream = new FileWriter("report", true);
  37
+		fileStream.write(nodeName + " → " + msg + "\n");
  38
+		fileStream.close();
  39
+		Thread.sleep(2000);
  40
+	}
32 41
 }
59  LamportClock/Pentagon.java
... ...
@@ -1,4 +1,61 @@
  1
+import java.io.*;
  2
+import java.util.*;
  3
+import java.net.*;
  4
+
1 5
 public class Pentagon {
2  
-	public static void main(String[] args) {
  6
+	private static ServerSocket serverSocket;
  7
+	private static FileWriter fileStream;
  8
+	private static Socket clientList[] = new Socket[3];
  9
+	private static clientThread clientThreadList[] = new clientThread[3];
  10
+
  11
+	public static void main(String[] args) throws Exception {
  12
+		Socket client;
  13
+
  14
+		serverSocket = new ServerSocket(5000);
  15
+		for(int i=0; i<3; i++) {
  16
+			client = serverSocket.accept();
  17
+			clientList[i] = client;
  18
+			clientThreadList[i] = new clientThread(client);
  19
+		}
  20
+
  21
+		for(int i=0; i<3; i++)
  22
+			clientThreadList[i].thread.join();
  23
+		serverSocket.close();
  24
+	}
  25
+
  26
+	private static void report(String msg) throws Exception {
  27
+		fileStream = new FileWriter("report", true);
  28
+		fileStream.write("Pentagon → " + msg + "\n");
  29
+		fileStream.close();
  30
+	}
  31
+
  32
+	private static class clientThread implements Runnable {
  33
+		private Socket client;
  34
+		public Thread thread;
  35
+		private ObjectInputStream in;
  36
+		private ObjectOutputStream out;
  37
+
  38
+		public clientThread(Socket client) throws Exception {
  39
+			this.client = client;
  40
+			in = new ObjectInputStream(client.getInputStream());
  41
+			out = new ObjectOutputStream(client.getOutputStream());
  42
+			thread = new Thread(this);
  43
+			thread.start();
  44
+		}
  45
+
  46
+		public void run() {
  47
+			try {
  48
+				System.out.println((String)in.readObject());
  49
+				System.out.println((String)in.readObject());
  50
+				closeStreams();
  51
+			} catch (Exception e) {
  52
+				e.printStackTrace();
  53
+			}
  54
+		}
  55
+
  56
+		private void closeStreams() throws Exception {
  57
+			in.close();
  58
+			out.close();
  59
+		}
3 60
 	}
4 61
 }
21  LamportClock/Ranger.java
... ...
@@ -1,16 +1,27 @@
1 1
 import java.io.*;
  2
+import java.util.*;
  3
+import java.net.*;
2 4
 
3 5
 public class Ranger {
4  
-
5  
-	private static String nodeName = "";
6  
-
  6
+	private static String nodeName;
7 7
 	private static FileWriter fileStream;
8 8
 
9 9
 	public static void main(String[] args) {
10  
-		if(args.length!=1) {
11  
-			System.out.println("Usage: Ranger <codename>");
  10
+		if(args.length!=2) {
  11
+			System.out.println("Usage: Ranger <codename> <port>");
12 12
 			System.exit(1);
13 13
 		}
14 14
 		nodeName = args[0];
  15
+
  16
+		report("Initialising mission SuperSecret!");
  17
+		report("Hacking the uber super computer!");
  18
+		report("Finishing mission SuperSecret!");
  19
+	}
  20
+
  21
+	private static void report(String msg) throws Exception {
  22
+		fileStream = new FileWriter("report", true);
  23
+		fileStream.write(nodeName + " → " + msg + "\n");
  24
+		fileStream.close();
  25
+		Thread.sleep(2000);
15 26
 	}
16 27
 }
18  LamportClock/report
... ...
@@ -1,9 +1,9 @@
1  
-Agent alpha → Initialised mission SuperSecret!
2  
-Agent beta → Initialised mission SuperSecret!
3  
-Agent charlie → Initialised mission SuperSecret!
4  
-Agent alpha → Hacked the super computer!
5  
-Agent beta → Hacked the super computer!
6  
-Agent charlie → Hacked the super computer!
7  
-Agent alpha → Finished mission SuperSecret!
8  
-Agent beta → Finished mission SuperSecret!
9  
-Agent charlie → Finished mission SuperSecret!
  1
+alpha → Initialising mission SuperSecret!
  2
+beta → Initialising mission SuperSecret!
  3
+charlie → Initialising mission SuperSecret!
  4
+alpha → Hacking the uber super computer!
  5
+beta → Hacking the uber super computer!
  6
+charlie → Hacking the uber super computer!
  7
+alpha → Finishing mission SuperSecret!
  8
+beta → Finishing mission SuperSecret!
  9
+charlie → Finishing mission SuperSecret!

0 notes on commit 21107c1

Please sign in to comment.
Something went wrong with that request. Please try again.