asp.net使用Socket.Send发送信息及Socket.SendFile传输文件的方法
本文实例讲述了asp.net使用Socket.Send发送信息及Socket.SendFile传输文件的方法。分享给大家供大家参考,具体如下:
//Displayssendingwithaconnectedsocket
//usingtheoverloadthattakesabuffer.
publicstaticintSendReceiveTest1(Socketserver)
{
byte[]msg=Encoding.UTF8.GetBytes("Thisisatest");
byte[]bytes=newbyte[256];
try
{
//Blocksuntilsendreturns.
inti=server.Send(msg);
Console.WriteLine("Sent{0}bytes.",i);
//Getreplyfromtheserver.
i=server.Receive(bytes);
Console.WriteLine(Encoding.UTF8.GetString(bytes));
}
catch(SocketExceptione)
{
Console.WriteLine("{0}Errorcode:{1}.",e.Message,e.ErrorCode);
return(e.ErrorCode);
}
return0;
}
//Establishthelocalendpointforthesocket.
IPHostEntryipHost=Dns.GetHostEntry(Dns.GetHostName());
IPAddressipAddr=ipHost.AddressList[0];
IPEndPointipEndPoint=newIPEndPoint(ipAddr,11000);
//CreateaTCPsocket.
Socketclient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//Connectthesockettotheremoteendpoint.
client.Connect(ipEndPoint);
//Thereisatextfiletest.txtlocatedintherootdirectory.
stringfileName="C:\\test.txt";
//SendfilefileNametoremotedevice
Console.WriteLine("Sending{0}tothehost.",fileName);
client.SendFile(fileName);
//Releasethesocket.
client.Shutdown(SocketShutdown.Both);
client.Close();
更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.netajax技巧总结专题》及《asp.net缓存操作技巧总结》。
希望本文所述对大家asp.net程序设计有所帮助。