客户端上传记事本到服务器端的指定的文件夹

下面的代码示例创建并连接一个套接字。文件“test.txt”位于本地计算机的根目录中。在此示例中,我们将创建数据的预缓冲区和后缓冲区,并将它们随该文件发送到远程主机。在此,使用了默认的   TransmitFileOptions。 C#     复制代码   //   Establish   the   local   endpoint   for   the   socket. IPHostEntry   ipHost   =   Dns.Resolve(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); //   Send   file   fileName   to   the   remote   host   with   preBuffer   and   postBuffer   data. //   There   is   a   text   file   test.txt   located   in   the   root   directory. string   fileName   =   "C://test.txt ";               //   Create   the   preBuffer   data. string   string1   =   String.Format( "This   is   text   data   that   precedes   the   file.{0} ",   Environment.NewLine); byte[]   preBuf   =   Encoding.ASCII.GetBytes(string1); //   Create   the   postBuffer   data. string   string2   =   String.Format( "This   is   text   data   that   will   follow   the   file.{0} ",   Environment.NewLine); byte[]   postBuf   =   Encoding.ASCII.GetBytes(string2); //Send   file   fileName   with   buffers   and   default   flags   to   the   remote   device. Console.WriteLine( "Sending   {0}   with   buffers   to   the   host.{1} ",   fileName,   Environment.NewLine); client.SendFile(fileName,   preBuf,   postBuf,   TransmitFileOptions.UseDefaultWorkerThread); //   Release   the   socket. client.Shutdown(SocketShutdown.Both); client.Close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值