package chat;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestServer extends JFrame implements ActionListener {
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;
public TestServer() {
this.setTitle("�����������");
JScrollPane jp = new JScrollPane();
ta = new JTextArea(10, 10);
Panel p = new Panel();
tf = new JTextField(20);
JButton b = new JButton("����");
b.addActionListener(this);
tf.addActionListener(this);
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South", p);
this.setSize(350, 250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
tf.requestFocus();
this.connect(); // ��bl��
this.createReadThread(); // �������Ϣ�߳�
}
public void connect() {
try {
ServerSocket ss = new ServerSocket(911); // ��ͨ�Ŷ˿�
Socket s2 = ss.accept();
InputStream is = s2.getInputStream();
dis = new DataInputStream(is); // ��������
OutputStream os = s2.getOutputStream();
dos = new DataOutputStream(os); // �������
} catch (IOException e) {
System.out.println("l�ӷ�����ʧ�ܣ�");
}
}
public void createReadThread() {
ReadThread rt = new ReadThread(this.ta, this.dis);
rt.start();
}
public void actionPerformed(ActionEvent e) {
try {
String s = tf.getText();
dos.writeUTF(s); // ����������Ϣ
ta.append("�Լ�˵:" + s);
ta.append("\n");
tf.setText("");
tf.requestFocus();
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new TestServer();
}
}
阅读(2105) | 评论(0) | 转发(0) |