-
大小: 62KB文件類(lèi)型: .zip金幣: 1下載: 1 次發(fā)布日期: 2021-06-14
- 語(yǔ)言: C#
- 標(biāo)簽:
資源簡(jiǎn)介
本代碼包括了TCP和UDP的客戶端和服務(wù)端,適合C#初學(xué)者學(xué)習(xí)、參考
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.Threading;
using?System.Threading.Tasks;
using?System.Windows.Forms;
namespace?NetWork
{
????public?partial?class?Form1?:?Form
????{
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????IPAddress?ip;
????????IPEndPoint?point?=?new?IPEndPoint(IPAddress.Any0);
????????string?protocol;
????????Socket?aimSocket;
????????Socket?mySocket;
????????UdpClient?RecUdpClient;
????????//IPEndPoint?remote;
????????private?void?Form1_Load(object?sender?EventArgs?e)//初始化為UDP?Server模式
????????{
????????????cobProtocol.SelectedIndex?=?0;
????????????txtIP.Text?=?GetAddressIP();
????????????Control.CheckForIllegalCrossThreadCalls?=?false;
????????}
????????string?GetAddressIP()
????????{
????????????string?AddressIP?=?““;
????????????foreach?(IPAddress?_IPAddress?in?Dns.GetHostEntry(Dns.GetHostName()).AddressList)
????????????{
????????????????if?(_IPAddress.AddressFamily.ToString()?==?“InterNetwork“)
????????????????{
????????????????????AddressIP?=?_IPAddress.ToString();
????????????????????ip?=?_IPAddress;//設(shè)定全局的IP
????????????????}
????????????}
????????????return?AddressIP;
????????}
????????private?void?btnStart_Click(object?sender?EventArgs?e)//設(shè)置目標(biāo)IP(Client),本地IP(Server)
????????{
????????????try
????????????{
????????????????if?(protocol?==?“UDP?Server“)
????????????????{
????????????????????aimSocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Dgram?ProtocolType.Udp);
????????????????????RecUdpClient?=?new?UdpClient(new?IPEndPoint(ip?Convert.ToInt32(txtPort.Text)));
????????????????????MessageBox.Show(“UDP?Server創(chuàng)建成功“);
????????????????????Thread?thReceive?=?new?Thread(USReceive);
????????????????????thReceive.IsBackground?=?true;
????????????????????thReceive.Start();
????????????????}
????????????????else?if?(protocol?==?“UDP?Client“)//UPD客戶端,目標(biāo)IP和端口??調(diào)用Socket.SendTo
????????????????{
????????????????????aimSocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Dgram?ProtocolType.Udp);//建立通信的Socket
????????????????????ip?=?IPAddress.Parse(txtIP.Text);
????????????????????point?=?new?IPEndPoint(ip?Convert.ToInt32(txtPort.Text));
????????????????????MessageBox.Show(“UDP?Client設(shè)置成功“);
????????????????????Thread?thReceive?=?new?Thread(UCReceive);
????????????????????thReceive.IsBackground?=?true;
????????????????????thReceive.Start();
????????????????}
????????????????else?if?(protocol?==?“TCP?Server“)
????????????????{
????????????????????mySocket?=?new?Socket(AddressFamily.InterNetwork?SocketType.Stream?ProtocolType.Tcp);
????????????????????point?=?new?IPEndPoint(ip?Convert.ToInt32(txtPort.Text));//綁定端口號(hào)
????????????????????mySocket.Bind(point);//綁定監(jiān)聽(tīng)端口
????????????????????MessageBox.Show(“TCP?Server綁定成功“);
??????
?屬性????????????大小?????日期????時(shí)間???名稱(chēng)
-----------?---------??----------?-----??----
?????目錄???????????0??2015-09-18?10:57??NetWork\
?????目錄???????????0??2015-09-18?17:36??NetWork\NetWork\
?????文件?????????990??2015-09-18?10:57??NetWork\NetWork.sln
?????文件???????27648??2015-09-18?17:36??NetWork\NetWork.v12.suo
?????文件?????????187??2015-09-18?10:57??NetWork\NetWork\App.config
?????目錄???????????0??2015-09-18?10:57??NetWork\NetWork\bin\
?????目錄???????????0??2015-09-18?11:31??NetWork\NetWork\bin\Debug\
?????文件???????13824??2015-09-18?17:36??NetWork\NetWork\bin\Debug\NetWork.exe
?????文件?????????187??2015-09-18?10:57??NetWork\NetWork\bin\Debug\NetWork.exe.config
?????文件???????30208??2015-09-18?17:36??NetWork\NetWork\bin\Debug\NetWork.pdb
?????文件???????24224??2015-09-18?17:36??NetWork\NetWork\bin\Debug\NetWork.vshost.exe
?????文件?????????187??2015-09-18?10:57??NetWork\NetWork\bin\Debug\NetWork.vshost.exe.config
?????文件?????????490??2014-01-13?21:31??NetWork\NetWork\bin\Debug\NetWork.vshost.exe.manifest
?????文件????????8151??2015-09-18?17:36??NetWork\NetWork\Form1.cs
?????文件????????8055??2015-09-18?11:30??NetWork\NetWork\Form1.Designer.cs
?????文件????????5817??2015-09-18?11:31??NetWork\NetWork\Form1.resx
?????文件????????3794??2015-09-18?11:31??NetWork\NetWork\NetWork.csproj
?????目錄???????????0??2015-09-18?10:57??NetWork\NetWork\obj\
?????目錄???????????0??2015-09-18?17:36??NetWork\NetWork\obj\Debug\
?????文件?????????865??2015-09-18?10:57??NetWork\NetWork\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件????????6987??2015-09-18?10:57??NetWork\NetWork\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????726??2015-09-18?17:36??NetWork\NetWork\obj\Debug\NetWork.csproj.FileListAbsolute.txt
?????文件?????????977??2015-09-18?11:31??NetWork\NetWork\obj\Debug\NetWork.csproj.GenerateResource.Cache
?????文件????????2211??2015-09-18?11:31??NetWork\NetWork\obj\Debug\NetWork.csprojResolveAssemblyReference.cache
?????文件???????13824??2015-09-18?17:36??NetWork\NetWork\obj\Debug\NetWork.exe
?????文件?????????180??2015-09-18?11:31??NetWork\NetWork\obj\Debug\NetWork.Form1.resources
?????文件???????30208??2015-09-18?17:36??NetWork\NetWork\obj\Debug\NetWork.pdb
?????文件?????????180??2015-09-18?11:31??NetWork\NetWork\obj\Debug\NetWork.Properties.Resources.resources
?????文件???????????0??2015-09-18?10:57??NetWork\NetWork\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件???????????0??2015-09-18?10:57??NetWork\NetWork\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件???????????0??2015-09-18?10:57??NetWork\NetWork\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此處省略8個(gè)文件信息
評(píng)論
共有 條評(píng)論