summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordor <dor1_b@walla.com>2013-10-18 19:41:59 +0300
committerdor <dor1_b@walla.com>2013-10-18 19:41:59 +0300
commitc687077bd3d7743b1c44cc06a724d4d41881b416 (patch)
tree0d662e2edac9f1a2ad67cda15c597954ebc77bd7
parentcc2a8b879fd189aeb9caf907838d514e3633a1af (diff)
Separate threads in client
Cleint now has a reader (reading from network, writing to screen) and a writer (writng to network, reading from screen) thread.
-rw-r--r--src/ChatClient.java45
-rw-r--r--src/ChatClientReader.java40
-rw-r--r--src/ChatClientWriter.java39
-rw-r--r--src/ChatServer.java19
4 files changed, 99 insertions, 44 deletions
diff --git a/src/ChatClient.java b/src/ChatClient.java
index ea0eeb9..53a6e81 100644
--- a/src/ChatClient.java
+++ b/src/ChatClient.java
@@ -4,51 +4,30 @@ import java.util.*;
public class ChatClient
{
- public static Scanner reader = new Scanner(System.in);
public static void main(String[] args) throws IOException
{
- String phrase;
int port = ChatServer.portNum;
String host = null;
Socket Soc;
- OutputStreamWriter outTemp;
-
- Soc = new Socket(host, port);
- outTemp = new OutputStreamWriter(Soc.getOutputStream());
- BufferedWriter output = new BufferedWriter(outTemp);
- InputStreamReader inputTemp = new InputStreamReader(Soc.getInputStream());
- BufferedReader input = new BufferedReader(inputTemp);
- while (true)
+ try
{
- System.out.print("Write here:> ");
- phrase = reader.nextLine();
- output.write(phrase, 0, phrase.length());
- output.newLine();
- output.flush();
- phrase = input.readLine(); // FIXME: more then 1 line
- phrase = "[" + phrase + "]";
- System.out.println(phrase);
-
+ Soc = new Socket(host, port);
+ } catch (IOException e)
+ {
+ System.err.println("Failed connecting to server: " + host + ":" + port + " (" + e + ")");
+ return;
}
+ ChatClientReader reader = new ChatClientReader(Soc);
+ ChatClientWriter writer = new ChatClientWriter(Soc);
+
+ reader.start();
+ writer.start();
+
//Soc.close();
}
}
-class ConsoleReader extends Thread
-{
- Socket Soc;
-
- void setSoc(Socket Soc)
- {
- this.Soc = Soc;
- }
-
- public void run()
- {
-
- }
-} \ No newline at end of file
diff --git a/src/ChatClientReader.java b/src/ChatClientReader.java
index 0dec5ee..15789f3 100644
--- a/src/ChatClientReader.java
+++ b/src/ChatClientReader.java
@@ -1,15 +1,41 @@
+import java.net.Socket;
import java.util.*;
-
-
-
+import java.io.*;
public class ChatClientReader extends Thread
{
- public static Scanner reader = new Scanner (System.in);
- public void run ()
+ public static Scanner reader = new Scanner(System.in);
+
+ private Socket Soc;
+
+ public ChatClientReader (Socket Soc)
{
-
+ this.Soc = Soc;
}
+
+ public void run()
+ {
+ try
+ {
+
+ String phrase;
+
+ InputStreamReader inputTemp = new InputStreamReader(this.Soc.getInputStream());
+ BufferedReader input = new BufferedReader(inputTemp);
+
+ do {
+ phrase = input.readLine(); // FIXME: more then 1 line
+ phrase = "[" + phrase + "]";
+ System.out.println("");
+ System.out.println(phrase);
+ } while (phrase != null);
+
+
+ } catch (IOException e) {
+ System.err.println(e);
+ return;
+ }
-}
+ }
+} \ No newline at end of file
diff --git a/src/ChatClientWriter.java b/src/ChatClientWriter.java
new file mode 100644
index 0000000..3a8283f
--- /dev/null
+++ b/src/ChatClientWriter.java
@@ -0,0 +1,39 @@
+import java.io.*;
+import java.net.Socket;
+import java.util.*;
+
+public class ChatClientWriter extends Thread
+{
+ private Socket Soc;
+
+ public static Scanner reader = new Scanner(System.in);
+
+ public ChatClientWriter(Socket Soc)
+ {
+ this.Soc = Soc;
+ }
+
+ public void run()
+ {
+ try
+ {
+ OutputStreamWriter outTemp;
+ String phrase;
+
+ outTemp = new OutputStreamWriter(this.Soc.getOutputStream());
+ BufferedWriter output = new BufferedWriter(outTemp);
+
+ while (true)
+ {
+ System.out.print("Write here:> ");
+ phrase = reader.nextLine();
+ output.write(phrase, 0, phrase.length());
+ output.newLine();
+ output.flush();
+ }
+
+ } catch (IOException e) {}
+
+ }
+
+}
diff --git a/src/ChatServer.java b/src/ChatServer.java
index 30a0811..426ff94 100644
--- a/src/ChatServer.java
+++ b/src/ChatServer.java
@@ -6,12 +6,23 @@ public class ChatServer
{
public static Scanner reader = new Scanner(System.in);
- public static int portNum = 40001;
+ public static int portNum = 50001;
public static void main(String[] args) throws IOException
{
String phrase;
- ServerSocket Service = new ServerSocket(portNum);
+ ServerSocket Service;
+
+ try
+ {
+ Service = new ServerSocket(portNum);
+ } catch (IOException e)
+ {
+ System.err.println("Failed listening on port " + portNum + " (" + e + ")");
+ return;
+ }
+
+
Socket Soc = null;
try
{
@@ -21,7 +32,7 @@ public class ChatServer
InputStreamReader inputTemp = new InputStreamReader(Soc.getInputStream());
BufferedReader input = new BufferedReader(inputTemp);
phrase = input.readLine(); // FIXME: more then 1 line
- System.out.println("Acc");
+ //System.out.println("Acc");
OutputStreamWriter outTemp = new OutputStreamWriter(Soc.getOutputStream());
BufferedWriter output = new BufferedWriter(outTemp);
phrase = "<" + phrase + ">";
@@ -30,7 +41,7 @@ public class ChatServer
output.newLine();
output.flush();
- System.out.println("Done");
+ //System.out.println("Done");
}
} catch (IOException e)
{