f = new Frame("QQ");
f.setSize(500,450);
f.setLocation(100,200);
f.setLayout(new FlowLayout());
t=new TextField(20);
ta1 = new TextArea(10,60);
b = new Button("发送");
ta2 = new TextArea(6,60);
f.add(t);
f.add(ta1);
f.add(ta2);
f.add(b);
ce();
f.setVisible(true);
}
//创建事件的方法。
public void ce()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
//主函数。
public class Chat
{
public static void main(String[] args)throws Exception
{
Jf j = new Jf();
DatagramSocket sds = new DatagramSocket();
DatagramSocket rds = new DatagramSocket(10008);
new Thread(new Receive(rds,j)).start();
new Thread(new Send(sds,j)).start();
public class ChatServer {
boolean started = false;
ServerSocket ss = null;
List<Client> clients = new ArrayList<Client>();
public static void main(String[] args) {
new ChatServer().start();
}
public void start() {
try {
ss = new ServerSocket(8555);
started = true;
} catch (BindException be) {
System.out.println("The port is using...\nPlease shut down referrence project and restart the server.");
System.exit(0);
} catch (IOException e2) {
e2.printStackTrace();
}
try {
while(started){
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
clients.add(c);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bConnected = false;
public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
}