aboutsummaryrefslogtreecommitdiff
path: root/CS2452/chat
diff options
context:
space:
mode:
Diffstat (limited to 'CS2452/chat')
-rw-r--r--CS2452/chat/ChatClient.java47
-rw-r--r--CS2452/chat/ChatServer.java44
2 files changed, 91 insertions, 0 deletions
diff --git a/CS2452/chat/ChatClient.java b/CS2452/chat/ChatClient.java
new file mode 100644
index 0000000..816d097
--- /dev/null
+++ b/CS2452/chat/ChatClient.java
@@ -0,0 +1,47 @@
+import java.util.*;
+import java.io.*;
+import java.net.*;
+
+class ChatClient
+{
+ public static void main(String[] args)
+ {
+ Socket s;
+ BufferedReader br, tr;
+ BufferedWriter bw;
+ InetAddress address = null;
+ int port = 0;
+
+ tr = new BufferedReader(new InputStreamReader(System.in));
+
+ try{
+ address = InetAddress.getByName(args[0]);
+ port = Integer.parseInt(args[1]);
+ }catch (UnknownHostException ex)
+ {
+ ex.printStackTrace();
+ }
+
+ try
+ {
+ System.out.printf("Connecting...\n");
+ s = new Socket(address, port);
+
+ br = new BufferedReader(new InputStreamReader(s.getInputStream()));
+
+ bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
+ System.out.printf("Client>");
+
+ while(true)
+ {
+ bw.write(tr.readLine());
+ bw.newLine();
+ bw.flush();
+ System.out.printf("%s: %s\n%s>", "Server", br.readLine(), "Client");
+ }
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/CS2452/chat/ChatServer.java b/CS2452/chat/ChatServer.java
new file mode 100644
index 0000000..3259611
--- /dev/null
+++ b/CS2452/chat/ChatServer.java
@@ -0,0 +1,44 @@
+import java.io.*;
+import java.util.*;
+import java.net.*;
+
+class ChatServer
+{
+ public static void main(String[] args)
+ {
+ ServerSocket ss;
+ Socket s;
+ BufferedReader br, tr;
+ BufferedWriter bw;
+
+ tr = new BufferedReader(new InputStreamReader(System.in));
+
+ try
+ {
+ ss = new ServerSocket(2452);
+
+ s = ss.accept();
+ System.out.printf("Client connected...\n");
+
+
+ br = new BufferedReader(new InputStreamReader(s.getInputStream()));
+ bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
+
+ //String[] address = new String[2];
+ //address[0] = InetAddress.toString(s.getAddress());
+ //address[1] = InetAddress.toString(s.getLocalAddress());
+
+ while(true)
+ {
+ System.out.printf("%s: %s\n%s>", "Client", br.readLine(), "Server");
+ bw.write(tr.readLine());
+ bw.newLine();
+ bw.flush();
+ }
+
+ }catch (Exception e)
+ {
+ e.printStackTrace();
+ }
+ }
+}