?? chatserver.java
/*
* @author: 先風劍客
* @version: chat0.8
* 實現各按鈕事件響應
* 主要是對不同按鈕作出反應
* 實現上一版本未完成的功能
* 在對發送后,顯示本機發送的內容
* 實現了網絡連接
* 對網絡上接收的數據進行簡單處理
* 但只能處理一次
* 添加時間功能
* 顯示信息定位到顯示面板
* 可以接收單個主機發來的多條消息
* 各類異常的人性化處理
* 接收多個客戶端
* 雖然只實現一點功能,但總體格局發生巨大改變
*
* 對接收到的數據,分別再發送給其它的客戶端
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class ChatServer {
public static void main(String[] args){
new ChatServerListener();
}
}
class ChatServerListener extends Frame{
ServerSocket ss = null;
Socket s = null;
TextArea ta = null;
TextField tf = null;
Date dt = null;
boolean started = false;
List<Client> clients = new ArrayList<Client>();
ChatServerListener(){
super("ChatServer");
ta = new TextArea();
tf = new TextField();
ChatServerMonitor cs = new ChatServerMonitor();
Dimension screen = getToolkit().getScreenSize();
addWindowListener(cs);
setBackground(Color.LIGHT_GRAY);
add(ta,BorderLayout.NORTH);
add(tf,BorderLayout.SOUTH);
pack();
setLocation((screen.width - getSize().width) / 2, (screen.height - getSize().height) /2);
setResizable(false);
setVisible(true);
Server();//啟動服務器
}
public void Server(){//服務器啟動
try{
ta.setText("服務器正在啟動并進行初始化.....");
ss = new ServerSocket(5678);
ta.setText(ta.getText()+"\n"+"服務器已經啟動......."+"\n"+"靜候客戶端連接.......");
started = true;
while(started){//啟動連接線程
s = ss.accept();
Client c = new Client(s);
new Thread(c).start();
clients.add(c);
}
}catch(BindException e1){
started = false;
System.out.println("端口無法使用.......");
System.exit(-1);
}catch(IOException e2){
started = false;
System.out.println("連接失敗!"+"\n"+e2.getMessage());
System.exit(-1);
}
}
class Client implements Runnable{//處理客戶端
private Socket s;
private DataInputStream dis = null;
private DataOutputStream dos = null;
private boolean bconnect = false;
Client(Socket s){
this.s = s;
try{
dt = new Date();
ta.setText(ta.getText()+"\n"+"有客戶機連接上....."+"\n"+"獲取客戶機信息......"+"\n"+"連接客戶機IP&Port: "+s.getRemoteSocketAddress()+"\n"+"連接時間: "+dt.getMonth()+"月 "+dt.getDay()+"日 "+dt.getHours()+"時 "+dt.getMinutes()+"分 "+dt.getSeconds()+"秒"+"\n");
dis = new DataInputStream(s.getInputStream());
dos = new DataOutputStream(s.getOutputStream());
bconnect = true;
}catch(EOFException e){
bconnect = false;
clients.remove(this);
ta.setText(ta.getText()+"\n"+"客戶端連接已經中斷......");
}catch(IOException e){
bconnect = false;
clients.remove(this);
ta.setText(ta.getText()+"\n"+"網絡連接中斷......");
}
}
public void send(String str){
try {
dos.writeUTF(str);
} catch (IOException e) {
clients.remove(this);
ta.setText(ta.getText()+"\n"+"客戶端斷開連接.....");
}
}
public void run() {
try{
while(bconnect){
String str = dis.readUTF();
ta.setText(ta.getText()+"\n"+"主機:"+s.getRemoteSocketAddress()+" 有消息送達:"+"\n"+" "+str+"\n"+"發送時間:"+(new Date()).getMonth()+"月 "+(new Date()).getDay()+"日 "+(new Date()).getHours()+"時 "+(new Date()).getMinutes()+"分 "+(new Date()).getSeconds()+"秒"+"\n");
for(int i=0; i<clients.size(); i++){//調用向其它客戶端發送消息的方法
Client c = clients.get(i);
c.send(str);
}
}
}catch(EOFException e){
bconnect = false;
//ta.setText(ta.getText()+"\n"+"客戶端連接已經中斷......");
}catch(IOException e){
bconnect = false;
ta.setText(ta.getText()+"\n"+"網絡連接中斷......");
}finally{
try {
if(dis != null){
dis.close();
dis = null;
}
if(dos != null){
dos.close();
dos = null;
}
if(s != null){
ta.setText(ta.getText()+"\n"+s.getRemoteSocketAddress()+"準備退出連接");
s.close();
clients.remove(this);
ta.setText(ta.getText()+"\n"+"客戶端已經安全退出連接.....");
s = null;
}
} catch (IOException e1) {
clients.remove(this);
ta.setText(ta.getText()+"\n"+"客戶端停止通話.....");
}
}
}
}
private class ChatServerMonitor extends WindowAdapter{
public void windowClosing(WindowEvent e) {
setVisible(false);
System.exit(0);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -