C#Socket文件传输

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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. (12)
0.......130.......140. (150)
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ;
using .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", "D:\\test.txt" };
//to parse the obj to IP and filename
string[] str = (string[])(obj);
string clientIP = str[0];
string filename = str[1];
try
{
//initialize a Socket Instance;
Socket listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress hostIP = Dns.GetHostEntry(clientIP).AddressList[0]; //将主机名或IP 地址解析为IPHostEntry 实例。

//将网络端点表示为IP 地址和端口号。

IPEndPoint ep = new IPEndPoint(hostIP, 11000); //用指定的地址和端口号初始化IPEndPoint 类的新实例。

////取得主机IP
//IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddr = ipHost.AddressList[0];
//IPEndPoint ep1 = new IPEndPoint(ipAddr, 11000); //用指定的地址和端口号初始化IPEndPoint 类的新实例。

//listenSocket
listenSocket.Bind(ep); //要与Socket 关联的本地EndPoint。

listenSocket.Listen(5); //将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());
}
}
}
}
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ;
using .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", "D:\\test.txt" };
//to parse the obj to IP and filename
string[] str = (string[])(obj);
string clientIP = str[0];
string filename = str[1];
try
{
//initialize a Socket Instance;
Socket listenSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp);
IPAddress hostIP = Dns.GetHostEntry(clientIP).AddressList[0]; //将主机名或IP 地址解析为IPHostEntry 实例。

//将网络端点表示为IP 地址和端口号。

IPEndPoint ep = new IPEndPoint(hostIP, 11000); //用指定的地址和端口号初始化IPEndPoint 类的新实例。

////取得主机IP
//IPHostEntry ipHost = Dns.Resolve(Dns.GetHostName());
//IPAddress ipAddr = ipHost.AddressList[0];
//IPEndPoint ep1 = new IPEndPoint(ipAddr, 11000); //用指定的地址和端口号初始化IPEndPoint 类的新实例。

//listenSocket
listenSocket.Bind(ep); //要与Socket 关联的本地EndPoint。

listenSocket.Listen(5); //将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. (12)
0.......130.......140. (150)
using System;
using System.Collections.Generic;
using ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ;
using .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:\\test.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", filename);
client.SendFile(filename);
//to shutdown the Server'receive and Client'send
client.Shutdown(SocketShutdown.Both);
client.Close();
}
catch (SocketException ex)
{
Console.WriteLine(ex.ToString());
}
}
} }。

相关文档
最新文档