diff options
author | dor <dor1_b@walla.com> | 2013-10-18 19:41:59 +0300 |
---|---|---|
committer | dor <dor1_b@walla.com> | 2013-10-18 19:41:59 +0300 |
commit | c687077bd3d7743b1c44cc06a724d4d41881b416 (patch) | |
tree | 0d662e2edac9f1a2ad67cda15c597954ebc77bd7 /src/ChatClient.java | |
parent | cc2a8b879fd189aeb9caf907838d514e3633a1af (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.
Diffstat (limited to 'src/ChatClient.java')
-rw-r--r-- | src/ChatClient.java | 45 |
1 files changed, 12 insertions, 33 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 |