📄 chatserver.java
字号:
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class ChatServer extends Frame {
private String numName = null;
private ArrayList<Client> clients = new ArrayList<Client>();
private ServerSocket ss = null;//用于建立服务器的插口
private Button btCreate = null;
private Label lbIP = null;
private Label lbPort = null;
private TextField tfIP = null;
private TextField tfPort = null;
private TextArea taContent = null;
public static void main(String[] args) {//主函数,这个程序的起点
new ChatServer().launch();//调用launch()函数
}
public void launch() {
this.setSize(500, 300);
this.setLocation(300, 300);
btCreate = new Button("创建服务器");
lbIP = new Label("IP地址");
lbPort = new Label("端口号");
tfIP = new TextField("", 15);
//tfIP.setSize(100, 100);
tfPort = new TextField("", 15);
//tfPort.setSize(100, 100);
taContent = new TextArea();
Panel p1 = new Panel();
Panel p2 = new Panel();
/*
p1.setLayout(new GridLayout(2, 2));
p1.add(lbIP);
p1.add(tfIP);
p1.add(lbPort);
p1.add(tfPort);
p2.add(btCreate, BorderLayout.CENTER);
p2.add(p1, BorderLayout.EAST);
*/
///*
p1.add(lbIP, BorderLayout.WEST);
p1.add(tfIP, BorderLayout.CENTER);
p1.add(lbPort, BorderLayout.EAST);
p2.add(btCreate, BorderLayout.WEST);
p2.add(p1, BorderLayout.CENTER);
p2.add(tfPort, BorderLayout.EAST);
this.add(p2, BorderLayout.NORTH);
this.add(taContent, BorderLayout.SOUTH);
//*/
//pack();
this.addWindowListener(new WindowAdapter(){
/*
* 这个代码其实没有用,因为它进入一个死循环,这儿要是想用它,要用一个多线程来实现它
* 可是没这个必要,因为服务器一般是不会关闭的
*/
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
btCreate.addMouseListener(new MyButtonListener());
this.setVisible(true);
}
class MyButtonListener extends MouseAdapter {//用于鼠标点击时作的反应
Socket s = null;//服务器的插口
String bt = null;
public void mouseClicked(MouseEvent e) {
try {
bt = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e2) {
e2.printStackTrace();
}
tfIP.setText(bt);
//tfIP.setText("127.0.0.1");
//这一个地方有一点美中不足,不能计算出具体的IP,不过肯定有这样的函数,今天不做了,有空的时候再改吧
taContent.setText("服务器创建成功" + '\n');
try {
ss = new ServerSocket(Integer.valueOf(tfPort.getText()));//总的服务器插口
while(true){
s = ss.accept();//接受一个客户端的连接
System.out.println("一个客户端成功连接上服务器");
Client c = new Client(s, numName);//新建对客户端的线程
new Thread(c).start();//此客户端线程启动
clients.add(c);//把此Client存放起来,为以后作准备;
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
class Client implements Runnable {
Socket s = null;
DataInputStream dis = null;
DataOutputStream dos = null;
String numName = null;
String str = null;//用于存放在客户端收到的信息
Client(Socket s, String numName){//构造方法,在新建时启动
this.s = s;
this.numName = numName;
try {
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void send(String s){
try {
dos.writeUTF(s);
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {//实现Runnable接口必须实现的方法,是此类线程
while(true){
try {
str = dis.readUTF();//此客户端中发出的信息读入到str
System.out.println(str);
for(int i = 0; i<clients.size(); i++){
Client c = clients.get(i);
c.send(str);
}
} catch (IOException e) {
try {//当客户端关闭的时候,服务器端相应的也关闭
dis.close();
dos.close();
s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -