I'm doing a simple chat application with TCP
in java.
In my code i have add a GUI but with some reasons it doesn't work even i have connected to server.
I can't figure out what is the problem.
I run the GUI and it works.
Hope that someone can help.
Server:
public class Server {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(3);
Socket client = server.accept();
System.out.println("Connected");
String str;
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
while(true){
str = "Connected to server";
bw.write(str);
bw.newLine();
str = br.readLine();
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client:
public class Client1 implements MouseListener {
private JFrame jF;
private JTextArea area;
private JTextField field;
private JButton send;
String str;
BufferedReader br;
BufferedWriter bw;
private Socket client;
public Client1(){
try {
client = new Socket("localhost", 3);
br = new BufferedReader(new InputStreamReader(client.getInputStream()));
bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
str = br.readLine();
jF = new JFrame("Simple Chat");
jF.setSize(500, 500);
jF.setLayout(null);
jF.setResizable(true);
jF.setLocationRelativeTo(null);
area = new JTextArea();
area.append(str);
area.setLocation(0, 0);
area.setSize(500,400);
jF.add(area);
field = new JTextField();
field.setLocation(0,400);
field.setSize(400,100);
jF.add(field);
send = new JButton("Send");
send.setLocation(400, 400);
send.setSize(100,100);
send.addMouseListener(this);
jF.add(send);
jF.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client1();
}
}