资源简介
给刚接触Socket的人学习用的,技术含量不高。为了让新人可以看得懂,代码量很小,力求简单。由于使用了C#3.0的匿名委托,不是VS2008的用户需要自己将匿名委托部分修改后才可以执行,VS2008用户可以直接打开并测试效果。

代码片段和文件信息
using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace Client
{
public partial class Form1 : Form
{
TcpClient tcp;
IPEndPoint remoteEP;
FileStream fs;
bool isstart = false;
Action showMessage;
public Form1()
{
InitializeComponent();
listView1.Columns.Add(“信息“).Width = listView1.Width / 2;
listView1.Columns.Add(“时间“).Width = listView1.Width / 2;
showMessage = delegate(string msg) { listView1.Items.Add(new ListViewItem(new string[] { msg DateTime.Now.ToString() })); };
button2.Enabled = false;
}
private void button1_Click(object sender EventArgs e)
{
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox3.Enabled = false;
button1.Enabled = false;
button2.Enabled = true;
button3.Enabled = false;
try
{
remoteEP = new IPEndPoint(IPAddress.Parse(textBox1.Text) 12345);
tcp = new TcpClient();
tcp.Client.ReceiveTimeout = 2000;
isstart = true;
tcp.Client.BeginConnect(remoteEP callback tcp.Client);
}
catch (Exception ex)
{
ShowMessage(ex.Message);
}
}
void callback(IAsyncResult ar)
{
Socket handler = (Socket)ar.AsyncState;
try
{
handler.EndConnect(ar);
//发送请求下载的文件名。
byte[] bytes = Encoding.UTF8.GetBytes(textBox2.Text);
handler.Send(bytes);
//接收下载的文件。
byte[] receive = new byte[1024];
int count = 0;
using (fs = new FileStream(textBox3.Text FileMode.OpenOrCreate))
{
//fs.Position = 0;
while (isstart)
{
count = handler.Receive(receive);
if (count > 0)
{
fs.Write(receive 0 count);
}
else
{
//注意,多数情况下收到字节数为0就意味着对方关闭了连接,
//但是否是数据接收完整应该进行文件长度比较,我这里只是简单模拟下,未作任何验证。
break;
}
}
}
handler.Close();
Stop();
ShowMessage(“传输完毕。(若看不到下载的文件,说明服务器端不存在该文件)“);
}
catch (Exception ex)
{
handler.Close();
Stop();
ShowMessage(ex.Message);
}
}
void Stop()
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 490 2007-07-21 01:33 Client\bin\Release\Client.vshost.exe.manifest
文件 3718 2009-10-30 16:01 Client\Client.csproj
文件 4288 2009-10-30 16:11 Client\Form1.cs
文件 7263 2009-10-30 15:58 Client\Form1.Designer.cs
文件 6017 2009-10-30 15:58 Client\Form1.resx
文件 498 2009-10-30 14:43 Client\Program.cs
文件 1424 2009-10-30 14:43 Client\Properties\AssemblyInfo.cs
文件 2841 2009-10-30 14:43 Client\Properties\Resources.Designer.cs
文件 5612 2009-10-30 14:43 Client\Properties\Resources.resx
文件 1091 2009-10-30 14:43 Client\Properties\Settings.Designer.cs
文件 249 2009-10-30 14:43 Client\Properties\Settings.settings
文件 204 2009-10-30 16:12 Service\obj\Release\Service.csproj.FileListAbsolute.txt
文件 500 2009-10-30 14:27 Service\Program.cs
文件 1426 2009-09-08 11:07 Service\Properties\AssemblyInfo.cs
文件 2843 2009-09-08 11:07 Service\Properties\Resources.Designer.cs
文件 5612 2009-09-08 11:07 Service\Properties\Resources.resx
文件 1092 2009-09-08 11:07 Service\Properties\Settings.Designer.cs
文件 249 2009-09-08 11:07 Service\Properties\Settings.settings
文件 4312 2009-10-30 15:51 Service\Server.cs
文件 5203 2009-10-30 15:03 Service\Server.Designer.cs
文件 6022 2009-10-30 15:03 Service\Server.resx
文件 3841 2009-10-30 16:04 Service\Service.csproj
文件 168 2009-10-30 16:12 Service\Service.csproj.user
文件 1383 2009-10-30 16:04 WindowsFormsApplication2.sln
..A..H. 59392 2009-10-30 16:12 WindowsFormsApplication2.suo
目录 0 2009-10-30 14:43 Client\obj\Debug\TempPE
目录 0 2009-10-30 15:58 Client\obj\Release\Refactor
目录 0 2009-10-30 14:43 Client\obj\Release\TempPE
目录 0 2009-09-08 11:07 Service\obj\Debug\TempPE
目录 0 2009-09-15 09:51 Service\obj\Release\Refactor
............此处省略19个文件信息
相关资源
- C# OCR数字识别实例,采用TessnetOcr,对
- 考试管理系统 - C#源码
- asp.net C#购物车源代码
- C#实时网络流量监听源码
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
评论
共有 条评论