em có viết 1 chương trình demo chat client và server, nhưng không hoạt động được , đã thử trên màn hình console kết nối được nhưng khi gắn giao diện thì không ạ, m.n giúp em với
chatserver
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InvocationEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.xml.ws.spi.Invoker;
public class ChatServer extends JFrame {
static ServerSocket serversocket;
static Socket socket;
static DataInputStream datain;
static DataOutputStream dataout;
static JPanel pnmain;
static TextField testfield;
static JTextArea testarer;
static JButton btsend;
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ChatServer().setVisible(true);
}
});
String str="";
try {
serversocket=new ServerSocket(1111);
socket=serversocket.accept();
datain=new DataInputStream(socket.getInputStream());
dataout=new DataOutputStream(socket.getOutputStream());
while(str.equals("q")) {
str=datain.readUTF();
testarer.setText(testarer.getText().trim()+"\n"+str);
testarer.append(str);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public ChatServer() {
super("chatserver");
JPanel pnmain=new JPanel();
pnmain.setBackground(Color.black);
pnmain.setLayout(null);
TextField testfield=new TextField();
testfield.setBounds(30, 350, 250, 30);
JTextArea testarer=new JTextArea();
testarer.setForeground(Color.white);
testarer.setBounds(30, 30, 320, 300);
JButton btsend=new JButton(">>");
btsend.setBounds(300, 350, 50, 30);
pnmain.add(testarer);
pnmain.add(testfield);
pnmain.add(btsend);
this.add(pnmain);
this.setSize(400, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
btsend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String str2="";
str2=testfield.getText().trim();
dataout.writeUTF(str2);
dataout.flush();
} catch (Exception e2) {
// TODO: handle exception
}
}
});
}
}
chatclient:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;
public class ChatClient extends JFrame {
static JPanel pnmain;
static TextField testfield;
static JTextArea testarer;
static JButton btsend;
static Socket socket;
static DataInputStream datain;
static DataOutputStream dataout;
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ChatClient().setVisible(true);
}
});
String str="";
try {
socket=new Socket("127.0.0.1",1111);
datain=new DataInputStream(socket.getInputStream());
dataout=new DataOutputStream(socket.getOutputStream());
while(str.equals("exit")) {
str=datain.readUTF();
testarer.setText(testarer.getText().trim()+"\n"+str);
testarer.append(str);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public ChatClient() {
super("chatclient");
JPanel pnmain=new JPanel();
pnmain.setBackground(Color.black);
pnmain.setLayout(null);
TextField testfield=new TextField();
testfield.setBounds(30, 350, 250, 30);
JTextArea testarer=new JTextArea();
testarer.setForeground(Color.white);
testarer.setBounds(30, 30, 320, 300);
JButton btsend=new JButton(">>");
btsend.setBounds(300, 350, 50, 30);
pnmain.add(testarer);
pnmain.add(testfield);
pnmain.add(btsend);
this.add(pnmain);
this.setSize(400, 500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
btsend.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
String str2="";
str2=testfield.getText().trim();
dataout.writeUTF(str2);
dataout.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
}
}