欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net使用Socket.Send发送信息及Socket.SendFile传输文件的方法

程序员文章站 2023-11-29 14:35:52
本文实例讲述了asp.net使用socket.send发送信息及socket.sendfile传输文件的方法。分享给大家供大家参考,具体如下: // displa...

本文实例讲述了asp.net使用socket.send发送信息及socket.sendfile传输文件的方法。分享给大家供大家参考,具体如下:

// displays sending with a connected socket
// using the overload that takes a buffer.
public static int sendreceivetest1(socket server)
{
  byte[] msg = encoding.utf8.getbytes("this 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();

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作xml技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。