The example in this article describes how ASP.net uses socket.send to send information and socket.sendfile transfer files. Share to everyone for your reference, specific as follows:
Displays sending with a connected socket
//using the overload that takes a buffer.
public static int SendReceiveTest1 (Socket server)
{
byte[] msg = Encoding.UTF8.GetBytes (' is a test ');
byte[] bytes = new byte[256];
Try
{
//Blocks until send returns.
int i = server. Send (msg);
Console.WriteLine ("Sent {0} bytes.", i);
Get reply from the server.
i = server. Receive (bytes);
Console.WriteLine (Encoding.UTF8.GetString (bytes));
}
catch (SocketException e)
{
Console.WriteLine ("{0} Error code: {1}.", E.message, E.errorcode);
return (E.errorcode);
}
return 0;
}
Establish the local endpoint for the socket.
Iphostentry iphost = Dns.gethostentry (Dns.gethostname ());
IPAddress ipaddr = iphost.addresslist[0];
IPEndPoint IPEndPoint = new IPEndPoint (ipaddr, 11000);
Create a TCP socket.
Socket client = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);
Connect the socket to the remote endpoint.
Client. Connect (IPEndPoint);
There is a-text file Test.txt located in the root directory.
String fileName = "C:\\Test.txt";
Send file filename to remote device
Console.WriteLine ("Sending {0} to the host.", fileName);
Client. SendFile (fileName);
Release the socket.
Client. Shutdown (Socketshutdown.both);
Client. Close ();
More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."
I hope this article will help you to ASP.net program design.