看了一下沒看出啥原因來,但是只要客戶端主動(dòng)斷開連接之后就開始屏幕輸出空字符串,并且將CPU全部占滿,請(qǐng)問一下問題出在哪里呢?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
namespace websocket
{
public class Session
{
private Socket _sockeclient;
private byte[] _buffer;
private string _ip;
private bool _isweb = false;
public Socket SockeClient
{
set { _sockeclient = value; }
get { return _sockeclient; }
}
public byte[] buffer
{
set { _buffer = value; }
get { return _buffer; }
}
public string IP
{
set { _ip = value; }
get { return _ip; }
}
public bool isWeb
{
set { _isweb = value; }
get { return _isweb; }
}
}
public class WebSocket
{
void handle(byte[] msg)
{
string str = Encoding.Default.GetString(msg);
//string str = Encoding.ASCII.GetString(msg);
//string str = Encoding.Unicode.GetString(msg);
Console.WriteLine(str.Substring(1));
//Console.WriteLine(Encoding.GetEncoding("GBK").GetString(msg));
//string[] rotation = str.Split(',');
//foreach (string item in rotation)
//{
// Console.WriteLine(item);
//}
}
private Dictionary<string, Session> SessionPool = new Dictionary<string, Session>();
private Dictionary<string, string> MsgPool = new Dictionary<string, string>();
#region 啟動(dòng)WebSocket服務(wù)
/// <summary>
/// 啟動(dòng)WebSocket服務(wù)
/// </summary>
public void start(int port)
{
Socket SockeServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
SockeServer.Bind(new IPEndPoint(IPAddress.Any, port));
SockeServer.Listen(20);
SockeServer.BeginAccept(new AsyncCallback(Accept), SockeServer);
Console.WriteLine("服務(wù)已啟動(dòng)");
Console.WriteLine("按任意鍵關(guān)閉服務(wù)");
Console.ReadLine();
}
#endregion
#region 處理客戶端連接請(qǐng)求
/// <summary>
/// 處理客戶端連接請(qǐng)求
/// </summary>
/// <param name="result"></param>
private void Accept(IAsyncResult socket)
{
// 還原傳入的原始套接字
Socket SockeServer = (Socket)socket.AsyncState;
// 在原始套接字上調(diào)用EndAccept方法,返回新的套接字
Socket SockeClient = SockeServer.EndAccept(socket);
byte[] buffer = new byte[4096];
try
{
//接收客戶端的數(shù)據(jù)
SockeClient.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), SockeClient);
//保存登錄的客戶端
Session session = new Session();
session.SockeClient = SockeClient;
session.IP = SockeClient.RemoteEndPoint.ToString();
session.buffer = buffer;
lock (SessionPool)
{
if (SessionPool.ContainsKey(session.IP))
{
this.SessionPool.Remove(session.IP);
}
this.SessionPool.Add(session.IP, session);
}
//準(zhǔn)備接受下一個(gè)客戶端
SockeServer.BeginAccept(new AsyncCallback(Accept), SockeServer);
Console.WriteLine(string.Format("Client {0} connected", SockeClient.RemoteEndPoint));
}
catch (Exception ex)
{
Console.WriteLine("Error : " + ex.ToString());
}
}
#endregion
#region 處理接收的數(shù)據(jù)
/// <summary>
/// 處理接受的數(shù)據(jù)
/// </summary>
/// <param name="socket"></param>
private void Recieve(IAsyncResult socket)
{
Socket SockeClient = (Socket)socket.AsyncState;
string IP = SockeClient.RemoteEndPoint.ToString();
if (SockeClient == null || !SessionPool.ContainsKey(IP))
{
return;
}
try
{
int length = SockeClient.EndReceive(socket);
byte[] buffer = SessionPool[IP].buffer;
SockeClient.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(Recieve), SockeClient);
string msg = Encoding.UTF8.GetString(buffer, 0, length);
// websocket建立連接的時(shí)候,除了TCP連接的三次握手,websocket協(xié)議中客戶端與服務(wù)器想建立連接需要一次額外的握手動(dòng)作
if (msg.Contains("Sec-WebSocket-Key"))
{
SockeClient.Send(PackageHandShakeData(buffer, length));
SessionPool[IP].isWeb = true;
return;
}
if (SessionPool[IP].isWeb)
{
msg = AnalyzeClientData(buffer, length);
}
byte[] msgBuffer = PackageServerData(msg);
//foreach (Session se in SessionPool.Values)
//{
// se.SockeClient.Send(msgBuffer, msgBuffer.Length, SocketFlags.None);
//}
handle(msgBuffer);
}
catch
{
SockeClient.Disconnect(true);
Console.WriteLine("客戶端 {0} 斷開連接", IP);
SessionPool.Remove(IP);
}
}
#endregion
#region 客戶端和服務(wù)端的響應(yīng)
/*
* 客戶端向服務(wù)器發(fā)送請(qǐng)求
*
* GET / HTTP/1.1
* Origin: http://localhost:1416
* Sec-WebSocket-Key: vDyPp55hT1PphRU5OAe2Wg==
* Connection: Upgrade
* Upgrade: Websocket
*Sec-WebSocket-Version: 13
* User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko
* Host: localhost:8064
* DNT: 1
* Cache-Control: no-cache
* Cookie: DTRememberName=admin
*
* 服務(wù)器給出響應(yīng)
*
* HTTP/1.1 101 Switching Protocols
* Upgrade: websocket
* Connection: Upgrade
* Sec-WebSocket-Accept: xsOSgr30aKL2GNZKNHKmeT1qYjA=
*
* 在請(qǐng)求中的“Sec-WebSocket-Key”是隨機(jī)的,服務(wù)器端會(huì)用這些數(shù)據(jù)來構(gòu)造出一個(gè)SHA-1的信息摘要。把“Sec-WebSocket-Key”加上一個(gè)魔幻字符串
* “258EAFA5-E914-47DA-95CA-C5AB0DC85B11”。使用 SHA-1 加密,之后進(jìn)行 BASE-64編碼,將結(jié)果做為 “Sec-WebSocket-Accept” 頭的值,返回給客戶端
*/
#endregion
#region 打包請(qǐng)求連接數(shù)據(jù)
/// <summary>
/// 打包請(qǐng)求連接數(shù)據(jù)
/// </summary>
/// <param name="handShakeBytes"></param>
/// <param name="length"></param>
/// <returns></returns>
private byte[] PackageHandShakeData(byte[] handShakeBytes, int length)
{
string handShakeText = Encoding.UTF8.GetString(handShakeBytes, 0, length);
string key = string.Empty;
Regex reg = new Regex(@"Sec\-WebSocket\-Key:(.*?)\r\n");
Match m = reg.Match(handShakeText);
if (m.Value != "")
{
key = Regex.Replace(m.Value, @"Sec\-WebSocket\-Key:(.*?)\r\n", "$1").Trim();
}
byte[] secKeyBytes = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
string secKey = Convert.ToBase64String(secKeyBytes);
var responseBuilder = new StringBuilder();
responseBuilder.Append("HTTP/1.1 101 Switching Protocols" + "\r\n");
responseBuilder.Append("Upgrade: websocket" + "\r\n");
responseBuilder.Append("Connection: Upgrade" + "\r\n");
responseBuilder.Append("Sec-WebSocket-Accept: " + secKey + "\r\n\r\n");
return Encoding.UTF8.GetBytes(responseBuilder.ToString());
}
#endregion
#region 處理接收的數(shù)據(jù)
/// <summary>
/// 處理接收的數(shù)據(jù)
/// 參考 http://www.cnblogs.com/smark/archive/2012/11/26/2789812.html
/// </summary>
/// <param name="recBytes"></param>
/// <param name="length"></param>
/// <returns></returns>
private string AnalyzeClientData(byte[] recBytes, int length)
{
int start = 0;
// 如果有數(shù)據(jù)則至少包括3位
if (length < 2) return "";
// 判斷是否為結(jié)束針
bool IsEof = (recBytes[start] >> 7) > 0;
// 暫不處理超過一幀的數(shù)據(jù)
if (!IsEof) return "";
start++;
// 是否包含掩碼
bool hasMask = (recBytes[start] >> 7) > 0;
// 不包含掩碼的暫不處理
if (!hasMask) return "";
// 獲取數(shù)據(jù)長度
UInt64 mPackageLength = (UInt64)recBytes[start] & 0x7F;
start++;
// 存儲(chǔ)4位掩碼值
byte[] Masking_key = new byte[4];
// 存儲(chǔ)數(shù)據(jù)
byte[] mDataPackage;
if (mPackageLength == 126)
{
// 等于126 隨后的兩個(gè)字節(jié)16位表示數(shù)據(jù)長度
mPackageLength = (UInt64)(recBytes[start] << 8 | recBytes[start + 1]);
start += 2;
}
if (mPackageLength == 127)
{
// 等于127 隨后的八個(gè)字節(jié)64位表示數(shù)據(jù)長度
mPackageLength = (UInt64)(recBytes[start] << (8 * 7) | recBytes[start] << (8 * 6) | recBytes[start] << (8 * 5) | recBytes[start] << (8 * 4) | recBytes[start] << (8 * 3) | recBytes[start] << (8 * 2) | recBytes[start] << 8 | recBytes[start + 1]);
start += 8;
}
mDataPackage = new byte[mPackageLength];
for (UInt64 i = 0; i < mPackageLength; i++)
{
mDataPackage[i] = recBytes[i + (UInt64)start + 4];
}
Buffer.BlockCopy(recBytes, start, Masking_key, 0, 4);
for (UInt64 i = 0; i < mPackageLength; i++)
{
mDataPackage[i] = (byte)(mDataPackage[i] ^ Masking_key[i % 4]);
}
return Encoding.UTF8.GetString(mDataPackage);
}
#endregion
#region 發(fā)送數(shù)據(jù)
/// <summary>
/// 把發(fā)送給客戶端消息打包處理(拼接上誰什么時(shí)候發(fā)的什么消息)
/// </summary>
/// <returns>The data.</returns>
/// <param name="message">Message.</param>
private byte[] PackageServerData(string msg)
{
byte[] content = null;
byte[] temp = Encoding.UTF8.GetBytes(msg);
if (temp.Length < 126)
{
content = new byte[temp.Length + 2];
content[0] = 0x81;
content[1] = (byte)temp.Length;
Buffer.BlockCopy(temp, 0, content, 2, temp.Length);
}
else if (temp.Length < 0xFFFF)
{
content = new byte[temp.Length + 4];
content[0] = 0x81;
content[1] = 126;
content[2] = (byte)(temp.Length & 0xFF);
content[3] = (byte)(temp.Length >> 8 & 0xFF);
Buffer.BlockCopy(temp, 0, content, 4, temp.Length);
}
return content;
}
#endregion
}
class Program
{
static void Main(string[] args)
{
WebSocket socket = new WebSocket();
socket.start(8080);
}
}
}
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。