winform上传文件php,C# Winform局域网传送文件

//发送文件

private void btn_sendFile_Click(object sender, EventArgs e)

{

//打开文件

OpenFileDialog dlg = new OpenFileDialog();

if (dlg.ShowDialog() == DialogResult.OK)

{

//初始化接受套接字:寻址方案,以字符流方式和Tcp通信

socketSent = new Socket(AddressFamily.InterNetwork,

SocketType.Stream,

ProtocolType.Tcp);

//设置服务器IP地址和端口

ipSent = new IPEndPoint(IPAddress.Parse(ip), 8001);

//与服务器进行连接

ClassSocket socketConnet = new ClassSocket(socketSent, ipSent);

Thread tConnection = new Thread(new ThreadStart(socketConnet.SocketConnect));

tConnection.Start();

Thread.Sleep(100);

//将要发送的文件加上"DAT"标识符

ClassSentFile sentFile = new ClassSentFile(dlg, socketSent);

Thread tSentFile = new Thread(new ThreadStart(sentFile.SentFile));

tSentFile.Start();

}

}

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Net;

using System.Net.Sockets;

using System.Windows.Forms;

namespace Message

{

class ClassSentFile

{

private OpenFileDialog dlg;

private Socket socketSent;

public ClassSentFile(OpenFileDialog dlg, Socket socketSent)

{

this.dlg = dlg;

this.socketSent = socketSent;

}

public void SentFile()

{

string msg = "DAT " + dlg.FileName;

//将 "msg" 转化为字节流的形式进行传送

socketSent.Send(Encoding.Default.GetBytes(msg));

//定义一个读文件流

FileStream read = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read);

//设置缓冲区为1024byte

byte[] buff = new byte[1024];

int len = 0;

while ((len = read.Read(buff, 0, 1024)) != 0)

{

//按实际的字节总量发送信息

socketSent.Send(buff, 0, len, SocketFlags.None);

}

//将要发送信息的最后加上"END"标识符

msg = "END";

//将 "msg" 发送

socketSent.Send(Encoding.Default.GetBytes(msg));

socketSent.Close();

read.Close();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值