91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 28KB
    文件類型: .rar
    金幣: 2
    下載: 0 次
    發布日期: 2021-05-27
  • 語言: C#
  • 標簽: C#??Socket??

資源簡介

給剛接觸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個文件信息

評論

共有 條評論