aboutsummaryrefslogtreecommitdiff
path: root/CS2452/chatThreaded
diff options
context:
space:
mode:
Diffstat (limited to 'CS2452/chatThreaded')
-rw-r--r--CS2452/chatThreaded/ChatClient.java40
-rw-r--r--CS2452/chatThreaded/ChatServer.java27
-rw-r--r--CS2452/chatThreaded/T1.java33
-rw-r--r--CS2452/chatThreaded/T2.java32
4 files changed, 132 insertions, 0 deletions
diff --git a/CS2452/chatThreaded/ChatClient.java b/CS2452/chatThreaded/ChatClient.java
new file mode 100644
index 0000000..1822240
--- /dev/null
+++ b/CS2452/chatThreaded/ChatClient.java
@@ -0,0 +1,40 @@
+import java.util.*;
+import java.io.*;
+import java.net.*;
+
+
+
+class ChatClient
+{
+ public static void main(String[] args)
+ {
+ Socket s;
+ InetAddress address = null;
+ int port = 0;
+
+
+
+ try
+ {
+ address = InetAddress.getByName(args[0]);
+ port = Integer.parseInt(args[1]);
+ }catch (UnknownHostException ex){
+ ex.printStackTrace();
+ }
+
+ try
+ {
+ s = new Socket(address, port);
+
+ T1 screenWriter = new T1(s);
+ T2 socketWriter = new T2(s);
+ screenWriter.start();
+ socketWriter.start();
+
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/CS2452/chatThreaded/ChatServer.java b/CS2452/chatThreaded/ChatServer.java
new file mode 100644
index 0000000..67608d1
--- /dev/null
+++ b/CS2452/chatThreaded/ChatServer.java
@@ -0,0 +1,27 @@
+import java.util.*;
+import java.io.*;
+import java.net.*;
+
+class ChatServer
+{
+ public static void main(String[] args)
+ {
+ ServerSocket ss;
+ Socket s;
+
+ try
+ {
+ ss = new ServerSocket(2452);
+ s = ss.accept();
+
+ T1 screenWriter = new T1(s);
+ T2 socketWriter = new T2(s);
+ screenWriter.start();
+ socketWriter.start();
+ }
+ catch(Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/CS2452/chatThreaded/T1.java b/CS2452/chatThreaded/T1.java
new file mode 100644
index 0000000..6fd8e06
--- /dev/null
+++ b/CS2452/chatThreaded/T1.java
@@ -0,0 +1,33 @@
+import java.util.*;
+import java.io.*;
+import java.net.*;
+
+
+class T1 extends Thread
+{
+ Socket s;
+
+ public T1(Socket socket)
+ {
+ s = socket;
+ }
+ public void run()
+ {
+ BufferedReader tr = new BufferedReader(new InputStreamReader(System.in));
+
+ try
+ {
+ BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
+
+ while(true)
+ {
+ bw.write(tr.readLine());
+ bw.newLine();
+ bw.flush();
+ }
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+ }
+}
+
diff --git a/CS2452/chatThreaded/T2.java b/CS2452/chatThreaded/T2.java
new file mode 100644
index 0000000..470c917
--- /dev/null
+++ b/CS2452/chatThreaded/T2.java
@@ -0,0 +1,32 @@
+
+import java.util.*;
+import java.io.*;
+import java.net.*;
+
+
+class T2 extends Thread
+{
+ Socket s;
+ public T2(Socket socket)
+ {
+ s = socket;
+ }
+ public void run()
+ {
+
+ try
+ {
+
+ BufferedReader bw = new BufferedReader( new InputStreamReader(s.getInputStream()));
+ String str = "Connected";
+ do
+ {
+ System.out.printf("%s\n", str);
+ str = bw.readLine();
+ }while (str != null);
+ }catch(Exception e){
+ e.printStackTrace();
+ }
+ }
+}
+