diff options
author | Dor Bivas <dor1_b@walla.com> | 2013-12-03 17:23:16 +0200 |
---|---|---|
committer | Dor Bivas <dor1_b@walla.com> | 2013-12-03 17:23:16 +0200 |
commit | 0bb21a5b0aa2a835d6a8aa5f8cbe46388c428f13 (patch) | |
tree | e378da88b0c39f8a961e05dd00b328fa7ae72819 /src | |
parent | 586ddb48f38625bbea43079810ad62efa474d0ac (diff) |
Connecting between server output to client
Diffstat (limited to 'src')
-rw-r--r-- | src/Client/ChatClient.java | 39 | ||||
-rw-r--r-- | src/Client/ChatClientReader.java | 17 | ||||
-rw-r--r-- | src/Client/UserInterface.java | 15 |
3 files changed, 38 insertions, 33 deletions
diff --git a/src/Client/ChatClient.java b/src/Client/ChatClient.java index 754340f..db10bfa 100644 --- a/src/Client/ChatClient.java +++ b/src/Client/ChatClient.java @@ -3,12 +3,15 @@ import java.io.*; import java.net.Socket;
+
import Server.ChatServer;
public class ChatClient
{
String nick;
String realname;
+ BufferedWriter output;
+ UserInterface ui;
/** returning the nick name */
public String getNick ()
@@ -30,7 +33,7 @@ public class ChatClient {
this.realname = realname;
}
-
+
public boolean connect (String host, int port)
{
Socket soc;
@@ -40,6 +43,9 @@ public class ChatClient {
soc = new Socket(host, port);
ChatClientReader reader = new ChatClientReader(soc);
+
+ reader.setDisplay(client.ui);
+
ChatClientWriter writer = new ChatClientWriter(soc,client);
reader.start();
@@ -52,9 +58,6 @@ public class ChatClient System.err.println("Failed connecting to server: " + host + ":" + port + " (" + e + ")");
return false;
}
-
-
-
}
public static void main(String[] args) throws IOException
@@ -62,36 +65,14 @@ public class ChatClient ChatClient client = new ChatClient();
client.setNick("dor");
client.setRealname("Dor bivas");
+ client.ui = new UserInterface(client);
+
+
- new UserInterface(client);
//soc.close();
}
}
-// משימות לביצוע:
-
-// TODO: 1. לשנות את שם המחלקה של ממשק המשתמש למשהו יותר נורמלי מ־SwingTest.
-// לדוגמה: UI (קיצור של UserInterface).
-
-// TODO: 2. לבטל את ה־main שלה ולהריץ את ממשק המשתמש מה־main של ChatClient.
-
-// TODO: 3. השורות שמגיעות מהרשת יודפסו ל־output של ממשק המשתמש במקום למסוף.
-
-// TODO: 4. לא תורץ התחברות בצורה אוטומטית. הוסף לממשק המשתמש כפתור "Connect".
-// לחיצה עליו תיזום התחברות (ומה צריכה לעשות לחיצה עליו לאחר שכבר התחלנו
-// להתחבר? איך יודעים שכבר התחלנו להתחבר? שאלה טובה).
-
-// TODO: 5. הפעלת כפתור Send: כאשר לוחצים עליו, תישלח הודעה. לצורך כך נבטל זמנית
-// את ה־Thread הנפרד שכותב לרשת: אין טעם בלולאה הראשית הנוכחית שלו. אבל ר'
-// הערה בהמשך. מכיוון שכך אני מעדיף שהשליחה לרשת עדיין תעשה משם ולא מתוך
-// ממשק המשתמש.
-
-// TODO: 6. ההודעה צריכה להיות הודעה לערוץ #chat, כלומר הודעה מסוג PRIVMSG.
-// TODO: 7. מכיוון שאנחנו רוצים אפשרות לשלוח הודעות אחרות, צריך לממש את האפשרות
-// לשלוח אותן. לשם כך נתחיל לממש פקודות. פקודות הן שורות שהתו הראשון בהן
-// הוא /. המילה שלאחר מכן היא הפקודה. גם כאן לא משנה אם מדובר על אותיות
-// גדולות או קטנות.
-
diff --git a/src/Client/ChatClientReader.java b/src/Client/ChatClientReader.java index a8c02cb..2183d90 100644 --- a/src/Client/ChatClientReader.java +++ b/src/Client/ChatClientReader.java @@ -3,10 +3,14 @@ import java.net.Socket; import java.util.*;
import java.io.*;
+import javax.swing.JTextArea;
+
public class ChatClientReader extends Thread
{
public static Scanner reader = new Scanner(System.in);
private Socket soc;
+ private JTextArea display;
+ UserInterface ui;
/** constructor
* @param the socket */
@@ -15,13 +19,18 @@ public class ChatClientReader extends Thread this.soc = soc;
}
+ public void setDisplay (UserInterface ui)
+ {
+ this.ui = ui;
+ }
+
/** Running the object and translate the input from bytes to letters*/
public void run()
{
try
{
- String line;
+ String line ;
InputStreamReader inputTemp = new InputStreamReader(this.soc.getInputStream());
BufferedReader input = new BufferedReader(inputTemp);
@@ -35,6 +44,12 @@ public class ChatClientReader extends Thread line = "[" + line + "]";
System.out.println("");
System.out.println(line);
+
+ ui.outputWriter(line);
+ this.display.append(line + "\n");
+
+
+
} while (line != null);
diff --git a/src/Client/UserInterface.java b/src/Client/UserInterface.java index a354e97..20b235c 100644 --- a/src/Client/UserInterface.java +++ b/src/Client/UserInterface.java @@ -17,9 +17,15 @@ public class UserInterface extends JFrame implements ActionListener private JTextField message;
private JTextArea output;
private ChatClient client;
+ private ChatClientReader read;
private JTextField host = new JTextField("localhost", 15);
private JTextField port = new JTextField("6667", 4);
+ public void outputWriter (String line)
+ {
+ output.append(line + "\n");
+ }
+
/** Graphic interface recipient the client*/
public UserInterface(ChatClient client)
{
@@ -30,7 +36,7 @@ public class UserInterface extends JFrame implements ActionListener output = new JTextArea(10, 20);
output.setBackground(Color.gray);
-
+
JPanel msgPanel = new JPanel();
JPanel conPanel = new JPanel();
JPanel vertPanel = new JPanel();
@@ -53,13 +59,15 @@ public class UserInterface extends JFrame implements ActionListener vertPanel.setLayout(new BoxLayout(vertPanel, BoxLayout.PAGE_AXIS));
+
+
msgPanel.add(connect);
msgPanel.add(send);
msgPanel.add(new JLabel("Message:"));
msgPanel.add(message);
msgPanel.add(stop);
-
+
conPanel.add(new JLabel("host:"));
conPanel.add(host);
@@ -75,6 +83,8 @@ public class UserInterface extends JFrame implements ActionListener setVisible(true);
}
+
+
/** Syncing the the action that the button send to the perform
* @param name of the action */
int counterClick = 0;
@@ -91,7 +101,6 @@ public class UserInterface extends JFrame implements ActionListener if (command == "send")
{
- System.out.println(message.getText());
output.append(message.getText() + "\n");
message.setText("");
}
|