在看到本文之前睡腿,如果讀者沒(méi)看過(guò)筆者的上一個(gè)系列 Java實(shí)現(xiàn)Socket網(wǎng)絡(luò)編程熬拒,建議先翻閱爷光。
筆者將在上期Demo的基礎(chǔ)上,進(jìn)一步修改和擴(kuò)展澎粟,達(dá)到本次Demo的運(yùn)行效果蛀序。
首先展示Demo的演示效果:
初始狀態(tài):1個(gè)服務(wù)器欢瞪,2個(gè)客戶端
檢測(cè)通信正常:
斷開(kāi)服務(wù)器,再次檢測(cè)通信正常:
服務(wù)器重新啟動(dòng)徐裸,自動(dòng)刷新:
添加客戶端:
關(guān)于 C(客戶端)和 S(服務(wù)器)之間的TCP通信遣鼓,以及 C 檢測(cè) S 狀態(tài),自動(dòng)重連等機(jī)制重贺,筆者在上期Demo的實(shí)現(xiàn)過(guò)程中已詳細(xì)闡述骑祟,此處就不再贅述。
我們來(lái)看看本次案例的實(shí)現(xiàn)需求:
1檬姥、服務(wù)器支持多客戶端訪問(wèn)
2曾我、C和S之間使用TCP連接
3粉怕、C和C之間使用UDP直接通信
由于案例需求的步驟1健民、2已實(shí)現(xiàn),我們對(duì)步驟3作如下設(shè)計(jì)思路:
1贫贝、客戶端創(chuàng)建監(jiān)聽(tīng)線程秉犹,建立UDP監(jiān)聽(tīng)端口,并發(fā)消息告訴服務(wù)器稚晚,指定自己的服務(wù)端口崇堵。
2、服務(wù)器得知客戶端的服務(wù)端口后客燕,廣播通知其他客戶端鸳劳,現(xiàn)已登錄的客戶端服務(wù)端口列表。
3也搓、客戶端之間直接通過(guò)UDP赏廓,向指定服務(wù)端口發(fā)送消息。
值得注意的是傍妒,C與C之間要求直接通信幔摸,所以必須滿足“在服務(wù)器關(guān)閉的情況下,C與C之間仍能通信”的情況颤练,而不是借助服務(wù)器完成間接通信
首先既忆,我們創(chuàng)建客戶端監(jiān)聽(tīng)線程,并發(fā)消息告訴服務(wù)器
public void run() {
try {
DatagramSocket server = new DatagramSocket(0);// 隨機(jī)分配一個(gè)端口號(hào)
// 向服務(wù)器發(fā)送接收客戶端的DatagramSocket的端口號(hào)
String message = Common.SPECIAL;
String t = "" + server.getLocalPort();
ClientMain.frame.setTitle("client " + t);
String c = "" + t.length();
if (c.length() < 2) {
c = "000" + c;
} else if (c.length() < 3) {
c = "00" + c;
} else if (c.length() < 4) {
c = "0" + c;
}
message += c + t;
OutputStreamWriter outstream = null;
// 將信息發(fā)送給服務(wù)器
try {
outstream = new OutputStreamWriter(mSocket.getOutputStream(),
"GBK");
outstream.write(message);
outstream.flush();
} catch (IOException e1) {
ClientMain.jlConnect.setText("Out Of Connect.");
ClientMain.isConnected = false;
if (outstream != null)
try {
outstream.close();
} catch (IOException e) {
e.printStackTrace();
}
e1.printStackTrace();
}
while (true) {
byte[] recvBuf = new byte[1024];// 定義接收消息的緩沖區(qū)
DatagramPacket recvPacket = new DatagramPacket(recvBuf,
recvBuf.length);// 數(shù)據(jù)包
server.receive(recvPacket);
// 接收到的消息
String recvStr = new String(recvPacket.getData(), 0,
recvPacket.getLength());
ClientMain.jtaReceivedMessage.append(recvStr + "\n");
// 滾動(dòng)到底端
ClientMain.jtaReceivedMessage
.setCaretPosition(ClientMain.jtaReceivedMessage
.getText().length());
}
} catch (Exception e) {
e.printStackTrace();
}
}
服務(wù)器得知客戶端的服務(wù)端口后嗦玖,廣播通知其他客戶端
else if (s.startsWith(Common.SPECIAL) && s.length() > 10
&& count == Integer.parseInt((s.substring(6, 10)))) {
// 存儲(chǔ)客戶端監(jiān)聽(tīng)端口
/**
* 一定要注意使用前初始化患雇,否則在IDE在這里檢測(cè)不到空指針錯(cuò)誤
*/
HashMap<Socket, String> map = new HashMap<Socket, String>();
map.put(mSocket, s.substring(10));
ServerMain.clientMonitorPortList.add(map);
// 發(fā)送更新列表信息給客戶端
sendUpdateToClient();
count = -10;
s = "";
}
sendUpdateToClient方法如下:
// 發(fā)送更新列表信息給所有客戶端
private void sendUpdateToClient() {
String message = Common.SEND_TO_CLIENT;
String t = "";
for (int i = 0; i < ServerMain.clientMonitorPortList.size(); i++) {
HashMap<Socket, String> map = ServerMain.clientMonitorPortList
.get(i);
Iterator iter1 = map.entrySet().iterator();
Map.Entry entry = (Map.Entry) iter1.next();
Socket key = (Socket) entry.getKey();
int localPort = key.getPort();
String port = (String) entry.getValue();
if (i != ServerMain.clientMonitorPortList.size() - 1)
t += localPort + " " + port + " ";
else
t += localPort + " " + port;
}
String c = "" + t.length();
if (c.length() < 2) {
c = "000" + c;
} else if (c.length() < 3) {
c = "00" + c;
} else if (c.length() < 4) {
c = "0" + c;
}
message += c + t;
OutputStreamWriter outstream = null;
// 將信息發(fā)送給每個(gè)客戶端
for (int i = 0; i < ListenThread.clientSockets.size(); i++) {
try {
HashMap<Socket, Boolean> map = ListenThread.clientSockets
.get(i);
// 用迭代器獲取HashMap的Key,即所選中的Socket
Iterator iter = map.entrySet().iterator();
Map.Entry<Socket, Boolean> entry = (Entry<Socket, Boolean>) iter
.next();
Socket key = (Socket) entry.getKey();
outstream = new OutputStreamWriter(key.getOutputStream(), "GBK");
outstream.write(message);
outstream.flush();
} catch (IOException e1) {
if (outstream != null)
try {
outstream.close();
} catch (IOException e) {
e.printStackTrace();
}
e1.printStackTrace();
}
}
}
最后宇挫,客戶端通過(guò)UDP向指定服務(wù)端口發(fā)送消息
當(dāng)選中JList的項(xiàng)時(shí)庆亡,向選中的項(xiàng)發(fā)送消息,如果沒(méi)有選中項(xiàng)捞稿,則向服務(wù)器發(fā)送消息
// 設(shè)置監(jiān)聽(tīng)
jbSendMessage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (jtaSendMessage.getText().equals("")) {
JOptionPane.showMessageDialog(null, "發(fā)送內(nèi)容不能為空!");
return;
}
// 取得要發(fā)送的消息
String message = Common.SIMPLE;
String t = "client " + Common.IP + ":" + mSocket.getLocalPort()
+ " " + jtaSendMessage.getText();
String c = "" + t.length();
if (c.length() < 2) {
c = "000" + c;
} else if (c.length() < 3) {
c = "00" + c;
} else if (c.length() < 4) {
c = "0" + c;
}
message += c + t;
OutputStreamWriter outstream = null;
// 如果沒(méi)有選中又谋,則向服務(wù)器發(fā)送消息
if (selecteds == null || selecteds.length == 0) {
try {
outstream = new OutputStreamWriter(mSocket
.getOutputStream(), "GBK");
outstream.write(message);
outstream.flush();
} catch (IOException e1) {
if (outstream != null)
try {
outstream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
e1.printStackTrace();
}
} else {
String sendPort = "";
// 檢測(cè)現(xiàn)在進(jìn)行發(fā)送行為的是哪個(gè)客戶端
for (int i = 0; i < clientPortList.size(); i++) {
HashMap<String, String> map = (HashMap<String, String>) clientPortList
.get(i);
Iterator iter1 = map.entrySet().iterator();
Map.Entry entry = (Map.Entry) iter1.next();
String sendSocketPort = (String) entry.getKey();
// mSocket.getLocalPort()是int類型拼缝,要注意加""
if (sendSocketPort.equals(mSocket.getLocalPort() + "")) {
sendPort = (String) entry.getValue();
}
}
// 向選中的客戶端發(fā)送消息
for (int i = 0; i < selecteds.length; i++) {
// 獲取選中的端口
HashMap<String, String> map = (HashMap<String, String>) clientPortList
.get(selecteds[i]);
Iterator iter1 = map.entrySet().iterator();
Map.Entry entry = (Map.Entry) iter1.next();
String port = (String) entry.getValue();
try {
// 生成一個(gè)臨時(shí)發(fā)送端口
DatagramSocket client = new DatagramSocket(0);
// 要發(fā)送的數(shù)據(jù)
String sendMessage = "client " + Common.IP + ":"
+ sendPort + " " + jtaSendMessage.getText();
byte[] buf = sendMessage.getBytes();
// 定義發(fā)送信息的目的地
InetAddress destination = InetAddress
.getByName(Common.IP);
// 生成數(shù)據(jù)包
DatagramPacket dp = new DatagramPacket(buf,
buf.length, destination, Integer
.valueOf(port));
client.send(dp);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
// 清空文本
jtaSendMessage.setText(null);
}
});
本次實(shí)驗(yàn)步驟看似簡(jiǎn)單,但也有幾個(gè)不得不注意的地方:
1彰亥、在讀寫(xiě)數(shù)據(jù)的循環(huán)里咧七,是檢測(cè)不到空指針錯(cuò)誤的,只會(huì)檢測(cè)到讀寫(xiě)錯(cuò)誤后不斷嘗試重連任斋。讀者在開(kāi)發(fā)過(guò)程中一定要注意把相應(yīng)的控件初始化继阻,而發(fā)現(xiàn)不斷重連,重復(fù)讀寫(xiě)時(shí)废酷,應(yīng)首先考慮是否在讀寫(xiě)循環(huán)里引用了未初始化的控件瘟檩。
2、mSocket.getLocalPort()方法返回的是int類型澈蟆,使用equals比較時(shí)要注意加雙引號(hào)""墨辛,以轉(zhuǎn)換成String類型,否則IDE不會(huì)編譯報(bào)錯(cuò)趴俘,但結(jié)果并未如意睹簇。
3、使用UDP端口容易混亂:讀者在開(kāi)發(fā)過(guò)程中應(yīng)盡量避免更新UI時(shí)整體刪除再添加剩余項(xiàng)寥闪,而改用“只刪除關(guān)閉項(xiàng)太惠,只增加新增項(xiàng)”,前種方法在開(kāi)發(fā)過(guò)程中容易造成端口混亂疲憋。同時(shí)凿渊,筆者建議讀者在涉及JList操作時(shí),多用ArrayList替代HashMap存儲(chǔ)缚柳,因?yàn)锳rrayList是插入有序的埃脏,能減少混亂的發(fā)生。
4喂击、注意在視圖model中刪除了項(xiàng)剂癌,也要同時(shí)在列表List中刪除對(duì)應(yīng)項(xiàng),以做到真正的刪除翰绊,而不是假刪除佩谷。
5、刪除List中的所有項(xiàng):
for(int i=0;i<list.size();)list.remove(i);
注意监嗜!這里不能添加i++谐檀,因?yàn)槊看蝦emove后,list.size()會(huì)自動(dòng)減小裁奇,如果添加了i++桐猬,則不能完全刪除List中的元素,從而導(dǎo)致二次混亂
最后刽肠,筆者在github上給出了兩次實(shí)驗(yàn)的Demo源碼溃肪,供讀者學(xué)習(xí)和思考免胃,感謝關(guān)注!