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;
public class Network {
public static void main(String [] args) {
String serverName = args[0];
int port = Integer.parseInt(args[1]);
try(Socket client = new Socket(serverName, port);
private static final int port = 4321;
private static final String host = "127.0.0.1" ;
Scanner socketin;
Formatter socketOut;
public Network() throws IOException{
try(Socket client = new Socket( host, port);
Scanner socketin = new Scanner(client.getInputStream());
Formatter socketOut = new Formatter(client.getOutputStream());)
{
String text;
socketOut.format(text);
socketOut.flush();
} catch (UnknownHostException e) {
e.printStackTrace();
......@@ -23,4 +26,11 @@ public class Network {
e.printStackTrace();
}
}
public void sendMessage(String text){
socketOut.format(text);
socketOut.flush();
}
}
package chatRoom.client.view;
import chatRoom.client.Network;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class MessageArea extends JPanel {
JTextField textField;
JButton btn;
ChatRoomGUI chatRoomGUI;
Network network;
public MessageArea(ChatRoomGUI chatRoomGUI){
......@@ -25,6 +29,13 @@ public class MessageArea extends JPanel {
doClickAction();
} });
add(btn, BorderLayout.EAST);
try {
network = new Network();
} catch (IOException e) {
e.printStackTrace();
}
this.setVisible(true);
}
void reset() {
......@@ -35,6 +46,7 @@ public class MessageArea extends JPanel {
void doClickAction(){
chatRoomGUI.addNewMessage(textField.getText());
network.sendMessage(textField.getText());
reset();
}
}
......@@ -3,20 +3,30 @@ import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Formatter;
import java.util.Scanner;
public class ClientHandler {
private Socket socket;
private DataOutputStream dataOutputStream;
private DataInputStream dataInputStream;
// private DataOutputStream dataOutputStream;
// private DataInputStream dataInputStream;
Scanner in;
Formatter out;
public ClientHandler(Socket client) throws Exception {
if (client == null)
throw new Exception("client can't be null");
this.socket = client;
dataOutputStream = new DataOutputStream(client.getOutputStream());
dataInputStream = new DataInputStream(client.getInputStream());
run();
// dataOutputStream = new DataOutputStream(client.getOutputStream());
// dataInputStream = new DataInputStream(client.getInputStream());
try(Scanner in = new Scanner(client.getInputStream());
Formatter out = new Formatter(client.getOutputStream());){
run();
}
}
private void handleIncomingMessages(String message){
System.out.println(message);
......@@ -24,13 +34,8 @@ public class ClientHandler {
public void run() {
while(true){
try {
String input = dataInputStream.readUTF();
handleIncomingMessages(input);
}
catch (IOException e) {
e.printStackTrace();
}
String input = in.next() ;
handleIncomingMessages(input);
}
}
}
\ 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