資源簡介
瘋狂HTML5CSS3JavaScript講義_源碼
代碼片段和文件信息
import?java.io.*;
import?java.net.*;
import?java.nio.charset.Charset;
import?java.security.MessageDigest;
import?java.util.regex.*;
import?java.util.*;
import?sun.misc.base64Encoder;
/**
?*?Description:
?*?
網站:?瘋狂Java聯盟
?*?
Copyright?(C)?2001-2012?Yeeku.H.Lee
?*?
This?program?is?protected?by?copyright?laws.
?*?
Program?Name:
?*?
Date:
?*?@author?Yeeku.H.Lee?kongyeeku@163.com
?*?@version?1.0
?*/
public?class?ChatServer
{
//?記錄所有的客戶端Soccket
public?static?List?clientSockets
=?new?ArrayList();
public?ChatServer()throws?IOException
{
//?創建ServerSocket,準備接受客戶端連接
ServerSocket?ss?=?new?ServerSocket(30000);
while(true)
{
//?接收到客戶端連接
Socket?socket?=?ss.accept();
//?將客戶端Socket添加到clientSockets集合中
clientSockets.add(socket);
//?啟動線程
new?ServerThread(socket).start();
}
}
public?static?void?main(String[]?args)
throws?Exception
{
new?ChatServer();
}
}
class?ServerThread?extends?Thread
{
private?Socket?socket;
public?ServerThread(Socket?socket)
{
this.socket?=?socket;
}
public?void?run()
{
try
{
//?得到Socket對應的輸入流
InputStream?in?=?socket.getInputStream();
//?得到Socket對應的輸出流
OutputStream?out?=?socket.getOutputStream();
byte[]?buff?=?new?byte[1024];
String?req?=?““;
//?讀取數據,此時建立與WebSocket的“握手“。
int?count?=?in.read(buff);
//?如果讀取的數據長度大于0
if(count?>?0)
{
//?將讀取的數據轉化為字符串
req?=?new?String(buff??0??count);
System.out.println(“握手請求:“?+?req);
//?獲取WebSocket的key
String?secKey?=?getSecWebSocketKey(req);
System.out.println(“secKey?=?“?+?secKey);
String?response?=?“HTTP/1.1?101?Switching?Protocols\r\nUpgrade:?“
+?“websocket\r\nConnection:?Upgrade\r\nSec-WebSocket-Accept:?“
+?getSecWebSocketAccept(secKey)?+?“\r\n\r\n“;
System.out.println(“secAccept?=?“?+?getSecWebSocketAccept(secKey));
out.write(response.getBytes());
}
int?hasRead?=?0;
//?不斷讀取WebSocket發送過來的數據
while((hasRead?=?in.read(buff))?>?0)
{
System.out.println(“接收的字節數:“?+?hasRead);
/*
因為WebSocket發送過來的數據遵循了一定的協議格式,
其中第3個~第6個字節是數據掩碼。
從第7個字節開始才是真正的有效數據。
因此程序使用第3個~第6個字節對后面的數據進行了處理
*/
for?(int?i?=?0?;?i? {
buff[i?+?6]?=?(byte)?(buff[i?%?4?+?2]?^?buff[i?+?6]);
}
//?獲得從瀏覽器發送過來的數據
String?pushMsg?=?new?String(buff
?6??hasRead?-?6??“UTF-8“);
//?遍歷Socket集合,依次向每個Socket發送數據
for?(Iterator?it?=?ChatServer.clientSockets.iterator()
;?it.hasNext()?;)
{
try
{
Socket?s?=?it.next();
//?發送數據時,第一個字節必須與讀到的第一個字節相同
byte[]?pushHead?=?new?byte[2];
pushHead[0]?=?buff[0];
//?發送數據時,第二個字節記錄發送數據的長度
pushHead[1]?=?(byte)?pushMsg.getBytes(“UTF-8“).length;
//?發送前兩個字節
s.getOutputStream().write(pushHead);
//?發送有效數據
s.getOutputStream().write(pushMsg.get
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2013-08-17?20:51??01\
?????目錄???????????0??2013-08-17?20:51??01\1.1\
?????文件??????????75??2012-01-10?16:36??01\1.1\bad.html
?????文件??????????86??2012-01-10?16:22??01\1.1\qs.html
?????目錄???????????0??2013-08-17?20:51??01\1.2\
?????文件?????????318??2012-01-12?18:28??01\1.2\autofocus.html
?????文件?????????616??2012-01-12?18:33??01\1.2\focus.html
?????目錄???????????0??2013-08-17?20:51??01\1.3\
?????文件?????????113??2012-01-12?00:16??01\1.3\missTag.html
?????目錄???????????0??2013-08-17?20:51??01\1.3\my?images\
?????文件???????22620??2011-11-02?22:52??01\1.3\my?images\android.png
?????文件????????5632??2012-04-27?14:23??01\1.3\my?images\Thumbs.db
?????文件?????????252??2012-01-11?23:45??01\1.3\noCase.html
?????文件?????????394??2012-01-12?16:59??01\1.3\noQuote.html
?????文件?????????276??2012-01-13?12:56??01\1.3\noQuoteError.html
?????文件?????????338??2012-01-12?16:29??01\1.3\proptest.html
?????目錄???????????0??2013-08-17?20:51??02\
?????目錄???????????0??2013-08-17?20:51??02\2.1\
?????文件?????????643??2012-02-23?09:32??02\2.1\anchor.html
?????文件?????????821??2012-01-13?13:10??02\2.1\basic.html
?????文件?????????297??2012-01-13?16:17??02\2.1\if
?????目錄???????????0??2013-08-17?20:51??02\2.1\images\
?????文件???????35767??2008-06-17?12:15??02\2.1\images\logo.jpg
?????文件?????????865??2012-01-13?13:47??02\2.1\img.html
?????文件?????????808??2012-01-13?16:23??02\2.1\list.html
?????文件????????1948??2012-02-23?09:10??02\2.1\semantic.html
?????文件?????????497??2012-01-13?15:01??02\2.1\simpleTable.html
?????文件?????????512??2012-01-13?15:05??02\2.1\tablespan.html
?????文件?????????645??2012-01-13?15:17??02\2.1\tablewithbody.html
?????文件????????1075??2012-01-13?15:58??02\2.1\tablewithcol.html
?????文件?????????762??2012-01-13?13:24??02\2.1\text.html
............此處省略526個文件信息
評論
共有 條評論