Commit 7f22e073 authored by Amirhosein Rajabpour's avatar Amirhosein Rajabpour

network class modified but there are still some errors

parent ac02d338
...@@ -7,15 +7,18 @@ import java.util.Scanner; ...@@ -7,15 +7,18 @@ import java.util.Scanner;
public class Network { public class Network {
public static void main(String [] args) { private static final int port = 4321;
String serverName = args[0]; private static final String host = "127.0.0.1" ;
int port = Integer.parseInt(args[1]);
try(Socket client = new Socket(serverName, port); Scanner socketin;
Formatter socketOut;
public Network() throws IOException{
try(Socket client = new Socket( host, port);
Scanner socketin = new Scanner(client.getInputStream()); Scanner socketin = new Scanner(client.getInputStream());
Formatter socketOut = new Formatter(client.getOutputStream());) Formatter socketOut = new Formatter(client.getOutputStream());)
{ {
String text;
socketOut.format(text);
socketOut.flush(); socketOut.flush();
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
e.printStackTrace(); e.printStackTrace();
...@@ -23,4 +26,11 @@ public class Network { ...@@ -23,4 +26,11 @@ public class Network {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void sendMessage(String text){
socketOut.format(text);
socketOut.flush();
}
} }
package chatRoom.client.view; package chatRoom.client.view;
import chatRoom.client.Network;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.IOException;
public class MessageArea extends JPanel { public class MessageArea extends JPanel {
JTextField textField; JTextField textField;
JButton btn; JButton btn;
ChatRoomGUI chatRoomGUI; ChatRoomGUI chatRoomGUI;
Network network;
public MessageArea(ChatRoomGUI chatRoomGUI){ public MessageArea(ChatRoomGUI chatRoomGUI){
...@@ -25,6 +29,13 @@ public class MessageArea extends JPanel { ...@@ -25,6 +29,13 @@ public class MessageArea extends JPanel {
doClickAction(); doClickAction();
} }); } });
add(btn, BorderLayout.EAST); add(btn, BorderLayout.EAST);
try {
network = new Network();
} catch (IOException e) {
e.printStackTrace();
}
this.setVisible(true); this.setVisible(true);
} }
void reset() { void reset() {
...@@ -35,6 +46,7 @@ public class MessageArea extends JPanel { ...@@ -35,6 +46,7 @@ public class MessageArea extends JPanel {
void doClickAction(){ void doClickAction(){
chatRoomGUI.addNewMessage(textField.getText()); chatRoomGUI.addNewMessage(textField.getText());
network.sendMessage(textField.getText());
reset(); reset();
} }
} }
...@@ -3,20 +3,30 @@ import java.io.DataInputStream; ...@@ -3,20 +3,30 @@ import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.net.Socket; import java.net.Socket;
import java.util.Formatter;
import java.util.Scanner;
public class ClientHandler { public class ClientHandler {
private Socket socket; private Socket socket;
private DataOutputStream dataOutputStream; // private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream; // private DataInputStream dataInputStream;
Scanner in;
Formatter out;
public ClientHandler(Socket client) throws Exception { public ClientHandler(Socket client) throws Exception {
if (client == null) if (client == null)
throw new Exception("client can't be null"); throw new Exception("client can't be null");
this.socket = client; this.socket = client;
dataOutputStream = new DataOutputStream(client.getOutputStream()); // dataOutputStream = new DataOutputStream(client.getOutputStream());
dataInputStream = new DataInputStream(client.getInputStream()); // dataInputStream = new DataInputStream(client.getInputStream());
try(Scanner in = new Scanner(client.getInputStream());
Formatter out = new Formatter(client.getOutputStream());){
run(); run();
}
} }
private void handleIncomingMessages(String message){ private void handleIncomingMessages(String message){
System.out.println(message); System.out.println(message);
...@@ -24,13 +34,8 @@ public class ClientHandler { ...@@ -24,13 +34,8 @@ public class ClientHandler {
public void run() { public void run() {
while(true){ while(true){
try { String input = in.next() ;
String input = dataInputStream.readUTF();
handleIncomingMessages(input); handleIncomingMessages(input);
} }
catch (IOException e) {
e.printStackTrace();
}
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment