视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
C# socket传文件
2025-10-05 00:56:25 责编:小OO
文档
C#SocketSC结构文件传输 收藏

下面是服务器端的代码,里面注释都很清楚我就不解释了

Serve form 上有一个btnListen的button,一个表连接状态的:Connection State的label,还有一个表连接状态的txtBox:txtConState

view plaincopy to clipboardprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Net;

using System.Net.Sockets;

namespace FileSendServer

{

public partial class FileSendServer : Form

{

public FileSendServer()

{

InitializeComponent();

}

private void btnListen_Click(object sender, EventArgs e)

{

  object obj = new string[] { "172.25.73.158

[] buff = new Byte[256];

int result;

//new file

if (File.Exists(filename))

{

File.Delete(filename);

}

//StreamWrite

StreamWriter sr = new StreamWriter(File.Create(filename));

while (true)

{

buff = new Byte[256];

result = mySoket.Receive(buff); //接收来自绑定的 Socket 的数据。

sr.Write(Encoding.Default.GetString(buff));

//sr.Write();

if (result < 256)

break;

}

sr.Close();//close buffer write

//txtConState.Clear();

}

catch (SocketException ex)

{

Console.WriteLine(ex.ToString());

}

}

}

}

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Net;

using System.Net.Sockets;

namespace FileSendServer

{

public partial class FileSendServer : Form

{

public FileSendServer()

{

InitializeComponent();

}

private void btnListen_Click(object sender, EventArgs e)

{

  object obj = new string[] { "172.25.73.158

); //将 Socket 置于侦听状态,参数backlog为挂起连接队列的最大长度

//accept the call

Socket mySoket = listenSocket.Accept(); //为新建连接创建新的 Socket。

txtConState.Text = "已建立联接!";

//define a buff

Byte[] buff = new Byte[256];

int result;

//new file

if (File.Exists(filename))

{

File.Delete(filename);

}

//StreamWrite

StreamWriter sr = new StreamWriter(File.Create(filename));

while (true)

{

buff = new Byte[256];

result = mySoket.Receive(buff); //接收来自绑定的 Socket 的数据。

sr.Write(Encoding.Default.GetString(buff));

//sr.Write();

if (result < 256)

break;

}

sr.Close();//close buffer write

//txtConState.Clear();

}

catch (SocketException ex)

{

Console.WriteLine(ex.ToString());

}

}

}

}

下面是客户端代码,

客户端是有一个,btnBrowse的打开浏览button,加了一个OpenFileDialog控件,一个btnSendFile的文件传输button,一个用于显示地址的文本框:txtFilePath

view plaincopy to clipboardprint?

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Net;

using System.Net.Sockets;

namespace FileSendClient

{

public partial class FileSendClient : Form

{

public static string path;

public FileSendClient()

{

InitializeComponent();

}

private void btnBrowse_Click(object sender, EventArgs e)

{

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

{

this.txtFilePath.Text = openFileDialog1.FileName;

path = openFileDialog1.FileName;

}

}

private void btnSendFile_Click(object sender, EventArgs e)

{

String ServerIP = "172.25.73.158";

string filename = path;

//string filename="C:\\\est.txt";

try

{

//为 Internet 主机地址信息提供容器类。

IPHostEntry ipHost = Dns.GetHostEntry(ServerIP); //将主机名或 IP 地址解析为 IPHostEntry 实例。

IPAddress ipAddr = ipHost.AddressList[0];

IPEndPoint ipEndP = new IPEndPoint(ipAddr, 11000);

Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

//connect

client.Connect(ipEndP);

Console.WriteLine("Sending {0} to the Host下载本文

显示全文
专题