资源描述:
《c# socket编程完成控制台服务器端和web客户端》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C#Socket编程完成控制台服务器端和web客户端(调试通过)服务器端程序usingSystem;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Text;namespaceSocketServer{ /// ///Class1的摘要说明。 /// classClass1 { /// ///应用程序的主入口点。 /// //定义端口号 privateconstintporNum=81; [S
2、TAThread] staticvoidMain(string[]args) { // //TODO:在此处添加代码以启动应用程序 // booldone=false; TcpListenerlistener=newotherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretary.Startingin1939served
3、asrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoftheCountypartyCommittee
4、Secretary,MinistersandTcpListener(porNum); listener.Start(); while(!done){ Console.Write("正在侦听81端口..."); TcpClientclient=listener.AcceptTcpClient(); Console.WriteLine("处理连接请求..."); NetworkStreamns=client.GetStream(); //ns.Read(,0,); byte[]bytes=ne
5、wbyte[1024]; intbytesRead=ns.Read(bytes,0,bytes.Length); Console.WriteLine(Encoding.BigEndianUnicode.GetString(bytes,0,bytesRead)); byte[]byteTime=Encoding.ASCII.GetBytes(DateTime.Now.ToString()); try { ns.Write(byteTime,0,byteTime.Length); ns.Clo
6、se(); client.Close(); } catch(Exceptionex){ Console.WriteLine(ex.ToString()); } } listener.Stop(); } }}Web客户端添加命名空间:usingotherstaffoftheCentre.Duringthewar,ZhuwastransferredbacktoJiangxi,andDirectorofthenewOfficeinJingdezhen,JiangxiCommitteeSecretar
7、y.Startingin1939servedasrecorderoftheWestNorthOrganization,SecretaryoftheSpecialCommitteeAfterthevictoryofthelongMarch,hehasbeentheNorthwestOfficeoftheFederationofStateenterprisesMinister,ShenmufuguSARmissions,DirectorofNingxiaCountypartyCommitteeSecretaryandrecorderoft
8、heCountypartyCommitteeSecretary,MinistersandSystem.Net;usingSystem.Net.Sockets;usingSystem.Text;连接代码:privatevo