下面是服务器端的代码,里面注释都很清楚我就不解释了
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下载本文