⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 如何写一个稳定,高效的socket服务器程序。 _net技术 - c# - csdn社区 community_csdn_net.htm

📁 主要是我最近两月在公司通过百度学习的内容,有AJAX,DWR,JAVA实现文件的上传和下载.主要目的在与告诉大家一个成功程序员(JAVA)是如何学习的,需要学习什么内容.以及学习的态度.
💻 HTM
📖 第 1 页 / 共 4 页
字号:
&nbsp; byte[] &nbsp; bCheck; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; protected 
&nbsp; AsyncCallback &nbsp; pfnWorkerCallBack &nbsp; ; &nbsp; <BR>&nbsp; 
protected &nbsp; AsyncCallback &nbsp; proOnAccept &nbsp; ; &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; <BR>&nbsp; /// &nbsp; 上传文件包 
&nbsp; <BR>&nbsp; /// &nbsp; &lt;/summary&gt; &nbsp; <BR>&nbsp; public &nbsp; 
class &nbsp; TSocketPacket &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; public &nbsp; 
System.Net.Sockets.Socket &nbsp; thisSocket; &nbsp; <BR>&nbsp; public &nbsp; 
byte[] &nbsp; dataBuffer = &nbsp; new &nbsp; byte[ONCE_COUNT]; &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; public &nbsp; FileStream &nbsp; m_file ; &nbsp; <BR>&nbsp; 
public &nbsp; int nTimes ; &nbsp; <BR>&nbsp; public &nbsp; int nCurTime ; &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; public &nbsp; TSocketPacket() &nbsp; <BR>&nbsp; { 
&nbsp; <BR>&nbsp; Reset(); &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; <BR>&nbsp; /// &nbsp; 清空 &nbsp; 
<BR>&nbsp; /// &nbsp; &lt;/summary&gt; &nbsp; <BR>&nbsp; public &nbsp; void 
&nbsp; Reset() &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; nTimes = 
0; &nbsp; <BR>&nbsp; nCurTime = 0; &nbsp; <BR>&nbsp; if(m_file &nbsp; != &nbsp; 
null) &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; try &nbsp; <BR>&nbsp; { &nbsp; 
<BR>&nbsp; m_file.Flush(); &nbsp; <BR>&nbsp; m_file.Close(); &nbsp; <BR>&nbsp; } 
&nbsp; <BR>&nbsp; catch &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; } &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; finally &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 
m_file = null; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; if( &nbsp; thisSocket != null) &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 
try &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; thisSocket.Close(); &nbsp; <BR>&nbsp; 
} &nbsp; <BR>&nbsp; catch &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; } &nbsp; 
<BR>&nbsp; finally &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; thisSocket = null; 
&nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; // public &nbsp; bool 
bCmd = true; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; /// &nbsp; 
&lt;summary&gt; &nbsp; <BR>&nbsp; /// &nbsp; 应用程序的主入口点。 &nbsp; <BR>&nbsp; /// 
&nbsp; &lt;/summary&gt; &nbsp; <BR>&nbsp; [STAThread] &nbsp; <BR>&nbsp; static 
&nbsp; void &nbsp; Main(string[] &nbsp; args) &nbsp; <BR>&nbsp; { &nbsp; 
<BR>&nbsp; // TcpListener &nbsp; myList=new &nbsp; TcpListener(ipAd,PORT); 
&nbsp; <BR>&nbsp; // &nbsp; <BR>&nbsp; // myList.Start(); &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; TUploadServer &nbsp; app &nbsp; = &nbsp; new &nbsp; 
TUploadServer(); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; if(args.Length &nbsp; 
&nbsp; != &nbsp; 0) &nbsp; <BR>&nbsp; app.CreateWait(Convert.ToInt16(args[0])); 
&nbsp; <BR>&nbsp; else &nbsp; <BR>&nbsp; app.CreateWait(); &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; // &nbsp; <BR>&nbsp; // &nbsp; TODO: &nbsp; 在此处添加代码以启动应用程序 
&nbsp; <BR>&nbsp; // &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; <BR>&nbsp; /// &nbsp; 构造 
&nbsp; <BR>&nbsp; /// &nbsp; &lt;/summary&gt; &nbsp; <BR>&nbsp; public &nbsp; 
TUploadServer() &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; //Socket &nbsp; Packet 
&nbsp; Array &nbsp; <BR>&nbsp; aSock = new &nbsp; TSocketPacket[MAX_CONNECT]; 
&nbsp; <BR>&nbsp; for(int &nbsp; i &nbsp; = &nbsp; 0; &nbsp; i&lt; &nbsp; 
MAX_CONNECT; &nbsp; i++) &nbsp; <BR>&nbsp; aSock[i] &nbsp; = &nbsp; new &nbsp; 
TSocketPacket(); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; // &nbsp; byte &nbsp; 
process &nbsp; <BR>&nbsp; m_byte = new &nbsp; EIIByte(); &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; nSocketNum = &nbsp; 0; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
bSend = &nbsp; new &nbsp; byte[ONCE_COUNT]; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
m_ascn &nbsp; = &nbsp; new &nbsp; ASCIIEncoding(); &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; pfnWorkerCallBack = &nbsp; new &nbsp; AsyncCallback(OnDataReceived); 
&nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; proOnAccept = new &nbsp; 
AsyncCallback(OnAccept); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; bCheck = new &nbsp; 
Byte[2]; &nbsp; <BR>&nbsp; int &nbsp; pos = 0; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; m_byte.SetWord(bCheck,CMD_S_CHECK,ref &nbsp; pos); &nbsp; <BR>&nbsp; 
// pfnWorkerCallBack = &nbsp; new &nbsp; AsyncCallback(OnReceive); &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; // proOnSend = &nbsp; new &nbsp; 
AsyncCallback(OnSend); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; 
&nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; <BR>&nbsp; 
/// &nbsp; 创建等待连接 &nbsp; <BR>&nbsp; /// &nbsp; &lt;/summary&gt; &nbsp; 
<BR>&nbsp; public &nbsp; void &nbsp; &nbsp; CreateWait(int &nbsp; port) &nbsp; 
<BR>&nbsp; { &nbsp; <BR>&nbsp; int nCount = &nbsp; 0; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; IPAddress &nbsp; ip = 
Dns.GetHostByName(Dns.GetHostName()).AddressList[0]; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; // IPAddress &nbsp; ipAddress &nbsp; = &nbsp; IPAddress.Parse(IP); 
&nbsp; &nbsp; <BR>&nbsp; IPEndPoint &nbsp; ipe &nbsp; = &nbsp; new &nbsp; 
IPEndPoint(ip,port); &nbsp; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; sWait = new 
&nbsp; Socket(AddressFamily.InterNetwork, &nbsp; SocketType.Stream, &nbsp; 
ProtocolType.Tcp); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; sWait.Bind(ipe); &nbsp; 
<BR>&nbsp; sWait.Listen(MAX_CONNECT); &nbsp; <BR>&nbsp; 
sWait.BeginAccept(proOnAccept,sWait); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
Console.WriteLine("The &nbsp; server &nbsp; is &nbsp; running &nbsp; at &nbsp; 
port &nbsp; {0}...",port); &nbsp; <BR>&nbsp; Console.WriteLine("The &nbsp; local 
&nbsp; End &nbsp; point &nbsp; is &nbsp; &nbsp; :" &nbsp; + &nbsp; ip.ToString() 
&nbsp; ); &nbsp; <BR>&nbsp; Console.WriteLine("Waiting &nbsp; for &nbsp; a 
&nbsp; connection....."); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; while(true) &nbsp; 
<BR>&nbsp; { &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
System.Threading.Thread.Sleep(50); &nbsp; <BR>&nbsp; nCount &nbsp; ++; &nbsp; 
<BR>&nbsp; //每60秒检查一次 &nbsp; <BR>&nbsp; if(nCount &nbsp; &gt;= &nbsp; 1200) 
&nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; ActiveCheck(); &nbsp; <BR>&nbsp; nCount = 
0; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; } 
&nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; <BR>&nbsp; 
/// &nbsp; 创建等待连接 &nbsp; <BR>&nbsp; /// &nbsp; &lt;/summary&gt; &nbsp; 
<BR>&nbsp; public &nbsp; void &nbsp; &nbsp; CreateWait() &nbsp; <BR>&nbsp; { 
&nbsp; <BR>&nbsp; CreateWait(PORT); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; } &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; 
<BR>&nbsp; /// &nbsp; 等待接收数据 &nbsp; <BR>&nbsp; /// &nbsp; &lt;/summary&gt; 
&nbsp; <BR>&nbsp; /// &nbsp; &lt;param &nbsp; name="nIndex"&gt;&lt;/param&gt; 
&nbsp; <BR>&nbsp; private &nbsp; void &nbsp; WaitForData(int &nbsp; nIndex) 
&nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; // Console.WriteLine("WaitForData &nbsp; 
{0}",soc.RemoteEndPoint.ToString()); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; try 
&nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; TSocketPacket &nbsp; theSocPkt &nbsp; = 
&nbsp; aSock[nIndex]; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; if( &nbsp; theSocPkt 
== &nbsp; null) &nbsp; <BR>&nbsp; return &nbsp; ; &nbsp; <BR>&nbsp; // &nbsp; 
now &nbsp; start &nbsp; to &nbsp; listen &nbsp; for &nbsp; any &nbsp; data... 
&nbsp; <BR>&nbsp; theSocPkt.thisSocket &nbsp; .BeginReceive &nbsp; 
(theSocPkt.dataBuffer &nbsp; ,0,theSocPkt.dataBuffer.Length &nbsp; 
,SocketFlags.None,pfnWorkerCallBack,nIndex); &nbsp; <BR>&nbsp; } &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; catch(SocketException &nbsp; se) &nbsp; <BR>&nbsp; 
{ &nbsp; <BR>&nbsp; ProcessSocketException(se,nIndex); &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; <A 
href="http://topic.csdn.net/t/20030327/22/1586528.html#">Top</A></P>
<H3><STRONG><A class=anchor name=r_10507990>9 楼</A>gujianxin(木头象)<INPUT class=user5 title="五级用户 该版得分小于等于5000分,大于2000分" type=button></STRONG><SPAN>回复于 
2003-03-28 11:35:45 得分 0 </SPAN></H3>
<P>//////////////////////////////// &nbsp; <BR>&nbsp; ////刚用C#时写的,莫笑 &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; /// &nbsp; &lt;summary&gt; &nbsp; <BR>&nbsp; /// 
&nbsp; 接收数据,并处理 &nbsp; <BR>&nbsp; /// &nbsp; &lt;/summary&gt; &nbsp; <BR>&nbsp; 
/// &nbsp; &lt;param &nbsp; name="asyn"&gt;&lt;/param&gt; &nbsp; <BR>&nbsp; 
public &nbsp; &nbsp; void &nbsp; OnDataReceived(IAsyncResult &nbsp; asyn) &nbsp; 
<BR>&nbsp; { &nbsp; <BR>&nbsp; //send &nbsp; : CMD(WORD &nbsp; = &nbsp; 
0x0301)+filename's &nbsp; length(WORD)+filename(target)+size(DWORD)+times(WORD), 
&nbsp; <BR>&nbsp; //Receive: CMD(WORD &nbsp; = &nbsp; 0x0302)+reday &nbsp; 
status(BYTE &nbsp; 1= &nbsp; OK,2 &nbsp; = &nbsp; FAIL) &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; //check &nbsp; <BR>&nbsp; if( &nbsp; !((asyn.AsyncState) &nbsp; is 
&nbsp; int)) &nbsp; <BR>&nbsp; return; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
string sFileName = null; &nbsp; <BR>&nbsp; short nNameLength = 0; &nbsp; 
<BR>&nbsp; int nSize = 0; &nbsp; <BR>&nbsp; byte byOper = ALL_RIGHT; 
//打开文件操作结果代码 &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; int nIndex = 
(int)asyn.AsyncState; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; if(nIndex &nbsp; &gt;= 
&nbsp; MAX_CONNECT &nbsp; || &nbsp; nIndex &nbsp; &lt;0 &nbsp; ) &nbsp; 
<BR>&nbsp; return &nbsp; ; &nbsp; <BR>&nbsp; TSocketPacket &nbsp; theSockId 
&nbsp; = &nbsp; aSock[nIndex] &nbsp; ; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
if(theSockId &nbsp; == &nbsp; null) &nbsp; <BR>&nbsp; return &nbsp; ; &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; try &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; //end 
&nbsp; receive... &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; int &nbsp; iRx = &nbsp; 
theSockId.thisSocket.EndReceive &nbsp; (asyn); &nbsp; <BR>&nbsp; // 
Console.WriteLine("Received &nbsp; Data &nbsp; From &nbsp; {0},length &nbsp; = 
&nbsp; {1}",theSockId.thisSocket.RemoteEndPoint.ToString(),iRx); &nbsp; 
<BR>&nbsp; int pos = &nbsp; 0; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; int &nbsp; 
nCmd = &nbsp; m_byte.GetWord(theSockId.dataBuffer,ref &nbsp; pos); &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; if(nCmd == &nbsp; CMD_C_REQUERY) //requery &nbsp; 
<BR>&nbsp; { &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; nNameLength = 
m_byte.GetWord(theSockId.dataBuffer,ref &nbsp; pos); &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; sFileName = m_ascn.GetString(theSockId.dataBuffer,pos,nNameLength); 
&nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; pos += nNameLength; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; nSize = m_byte.GetDword(theSockId.dataBuffer,ref &nbsp; pos); &nbsp; 
<BR>&nbsp; &nbsp; <BR>&nbsp; theSockId.nTimes= 
m_byte.GetDword(theSockId.dataBuffer,ref &nbsp; pos); &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; Console.WriteLine("Get &nbsp; Requery &nbsp; file &nbsp; = &nbsp; 
'{0}',size &nbsp; = &nbsp; {1} &nbsp; ,times= &nbsp; 
{2}",sFileName,nSize,theSockId.nTimes); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; // 
&nbsp; assert &nbsp; m_file &nbsp; not &nbsp; opened &nbsp; &nbsp; <BR>&nbsp; if 
&nbsp; (theSockId.m_file &nbsp; != &nbsp; null) &nbsp; <BR>&nbsp; //return 
&nbsp; ; &nbsp; <BR>&nbsp; byOper = FILE_ALWAYS_OPENED; &nbsp; <BR>&nbsp; &nbsp; 
<BR>&nbsp; try &nbsp; &nbsp; //open &nbsp; the &nbsp; file &nbsp; <BR>&nbsp; { 
&nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; theSockId.m_file = 
File.Open(sFileName,FileMode.OpenOrCreate,FileAccess.Write,FileShare.None &nbsp; 
); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; Console.WriteLine("Open &nbsp; or &nbsp; 
Create &nbsp; file &nbsp; '{0}'",sFileName); &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
byOper = ALL_RIGHT; &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; theSockId.nCurTime = 0; 
&nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; } &nbsp; <BR>&nbsp; &nbsp; <BR>&nbsp; 
catch(Exception &nbsp; e) &nbsp; <BR>&nbsp; { &nbsp; <BR>&nbsp; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -