是套接字发送文件
客户端代码为
private void btnchoosefile_Click(object sender, EventArgs e)//选择文件按钮
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
textsendfilename.Text = ofd.FileName;
}
}
private void btnsendfile_Click(object sender, EventArgs e)//发送文件按钮
{
using(FileStream fs=new FileStream(textsendfilename.Text,FileMode.Open ))
{
byte[] arrfile = new byte[102 * 1024 * 2];
int length = fs.Read(arrfile, 0, arrfile.Length);
byte[] newarrfile = new byte[length + 1];
newarrfile[0] = 1;
Buffer.BlockCopy(arrfile, 0, newarrfile, 1, length);
connection.Send(newarrfile);
}
}
服务端判断是否是文件
void RecMsg(object socket)
{
while (true)
{
Socket soket1 = socket as Socket;
byte[] bytes = new byte[1024 * 1024 * 2];
int length = soket1.Receive(bytes);
if (bytes[0] == 1)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string filesavepath = sfd.FileName;
using (FileStream fs = new FileStream(filesavepath, FileMode.Create))
{
fs.Write(bytes, 1, length - 1);
ShowMsg("保存成功" + "\r\n");
}
}
}
我点客户端选择好文件发送到服务端 没反应 没有报错 不弹出保存文件的选择框 各位同学帮我看看是哪出错了。 |