asp.net use socket.send to send information and Socket.sendfile Transfer file Method _ practical skills

Source: Internet
Author: User
Tags error code sendfile

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.