Em đang cảm thấy rất khó hiểu. Ảnh dưới lúc e cho System.out
vào thì nó chạy, còn bỏ đi thì thì nó ko chạy, ai giải thích jup e với.
Đây là code cl của e:
package chat_s;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class cilient extends panel {
static Scanner sc = new Scanner(System.in);
static cilient p = new cilient();
static String s = "";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Socket socket;
textarea = new JTextArea();
textarea.setBounds(0, 0, 300, 500);
scpane = new JScrollPane(textarea);
scpane.setBounds(10, 10, 300, 500);
p.add(scpane);
button = new JButton("send");
button.setBounds(50, 510, 70, 30);
p.add(button);
textf = new JTextField();
textf.setBounds(150, 510, 150, 30);
p.add(textf);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
s = textf.getText();
}
});
p.setVisible(true);
try {
socket = new Socket("192.168.0.106", 7777);
DataOutputStream da = new DataOutputStream(socket.getOutputStream());
System.out.println("dat ten user:");
String s = sc.nextLine();
da.writeUTF(s);
da.writeUTF("room1");
new read(socket).start();
new write(socket).start();
} catch (UnknownHostException e) {
System.out.println("host da dong");
e.printStackTrace();
} catch (IOException e) {
System.out.println("loi ket noi");
e.printStackTrace();
}
}
}
class read extends Thread {
Socket socket;
public read(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
DataInputStream datainput = new DataInputStream(socket.getInputStream());
while (true) {
cilient.p.addtext(datainput.readUTF());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class write extends Thread {
Socket socket;
public write(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
DataOutputStream dataoutput = new DataOutputStream(socket.getOutputStream());
while (true) {
System.out.println(cilient.s.equals("")); // dòng này
if (cilient.s.equals("")) {} else {
cilient.p.addtext(cilient.s);
dataoutput.writeUTF(cilient.s);
cilient.s = "";
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}