資源簡介
例子中演示了udp和tcp通訊,可以循環(huán)發(fā)送消息。 分為服務(wù)器端和客戶端兩個控制臺。 udp比較簡單,無連接狀態(tài),可以指定ip也可以廣播; tcp比較復(fù)雜,不能廣播,每次發(fā)消息都需要重新建立連接 手動修改一下Main方法中調(diào)用的方法udp()/tcp()即可完成兩種方式的切換。
代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Net;
using?System.Net.Sockets;
using?System.Text;
using?System.Threading.Tasks;
namespace?SocketClient
{
????class?Program
????{
????????static?void?Main(string[]?args)
????????{
???????????udp();//可以手動切換tcp/udp代碼
????????????
????????}
????????///?
????????///?udp不需要建立面向連接的通信(通過廣播發(fā)送數(shù)據(jù))
????????///?
????????private?static?void?udp()
????????{
????????????Socket?udp?=?new?Socket(AddressFamily.InterNetwork?SocketType.Dgram?ProtocolType.Udp);
????????????udp.SetSocketOption(SocketOptionLevel.Socket?SocketOptionName.Broadcast?1);//允許?發(fā)送廣播
????????????IPEndPoint?iepA?=?new?IPEndPoint(IPAddress.Broadcast?9095);
????????????//IPEndPoint?iepA?=?new?IPEndPoint(IPAddress.Parse(“192.168.2.160“)?5566);
????????????Console.WriteLine(“udp已經(jīng)建立,請輸入您要發(fā)送的內(nèi)容“);
????????????while?(true)
????????????{
????????????????string?sss?=?Console.ReadLine();
????????????????int?len?=?udp.SendTo(System.Text.Encoding.Unicode.GetBytes(sss)?iepA);
????????????}
????????}
?????
????????///?
????????///?tcp需要建立面向連接的通訊,并且需要三次握手.也就是說,每次通信后,tcp都需要關(guān)閉,下次關(guān)閉前重新建立連接。(不能通過廣播發(fā)送數(shù)據(jù))
????????///?
????????private?static?void?tcp()
????????{
????????????Socket?newclient;
????????????while?(true)
????????????{
????????????????Console.WriteLine(“請輸入您要發(fā)送的內(nèi)容“);
????????????????string?content?=?Console.ReadLine();
????????????????using?(newclient?=?new?System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork?System.Net.Sockets.SocketType.Stream?System.Net.Sockets.ProtocolType.Tcp))
????????????????{
????????????????????byte[]?data?=?new?byte[1024];
????????????????????IPEndPoint?ie?=?new?System.Net.IPEndPoint(IPAddress.Parse(“192.168.1.10“)?9050);//服務(wù)器的IP和端口
????????????????????try
????????????????????{
????????????????????????//因為客戶端只是用來向特定的服務(wù)器發(fā)送信息,所以不需要綁定本機的IP和端口。不需要監(jiān)聽。
????????????????????????newclient.Connect(ie);//第一次交互
????????????????????????int?recv?=?newclient.Receive(data);//第二次交互
????????????????????????//連接服務(wù)器成功
????????????????????????string?stringdata?=?System.Text.Encoding.Unicode.GetString(data?0?recv);
????????????????????????if?(stringdata?==?“連接服務(wù)器成功“)
????????????????????????{
????????????????????????????newclient.Send(System.Text.Encoding.Unicode.GetBytes(content));//第三次交互
????????????????????????????newclient.Shutdown(System.Net.Sockets.SocketShutdown.Send);
????????????????????????????data?=?new?byte[1024];
????????????????????????????recv?=?newclient.Receive(data);
????????????????????????????string?result?=?System.Text.Encoding.Unicode.GetString(data?0?recv);
????????????????????????????newclient.Shutdown(System.Net.Sockets.SocketShutdown.Receive);
????????????????????????????newclient.Close();
????????????????????????????Console.WriteLine(result);
????????????????????????}
????????????????????????else
????????????????????????{
????????????????????????????C
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2017-04-20?22:37??SocketClient\
?????目錄???????????0??2017-07-18?14:24??SocketClient\SocketClient\
?????文件?????????187??2017-03-29?17:10??SocketClient\SocketClient\App.config
?????文件????????4402??2017-07-18?14:24??SocketClient\SocketClient\Program.cs
?????目錄???????????0??2017-04-20?22:37??SocketClient\SocketClient\Properties\
?????文件????????1348??2017-03-29?17:10??SocketClient\SocketClient\Properties\AssemblyInfo.cs
?????文件????????2566??2017-03-29?17:10??SocketClient\SocketClient\SocketClient.csproj
?????目錄???????????0??2017-04-20?22:37??SocketClient\SocketClient\bin\
?????目錄???????????0??2017-04-20?22:37??SocketClient\SocketClient\bin\Debug\
?????文件????????6656??2017-07-18?14:24??SocketClient\SocketClient\bin\Debug\SocketClient.exe
?????文件?????????187??2017-03-29?17:10??SocketClient\SocketClient\bin\Debug\SocketClient.exe.config
?????文件???????15872??2017-07-18?14:24??SocketClient\SocketClient\bin\Debug\SocketClient.pdb
?????文件???????24224??2017-07-18?14:25??SocketClient\SocketClient\bin\Debug\SocketClient.vshost.exe
?????文件?????????187??2017-03-29?17:10??SocketClient\SocketClient\bin\Debug\SocketClient.vshost.exe.config
?????文件?????????490??2013-03-18?17:00??SocketClient\SocketClient\bin\Debug\SocketClient.vshost.exe.manifest
?????目錄???????????0??2017-04-20?22:37??SocketClient\SocketClient\obj\
?????目錄???????????0??2017-07-18?14:24??SocketClient\SocketClient\obj\Debug\
?????文件????????6613??2017-07-14?15:10??SocketClient\SocketClient\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件?????????556??2017-07-18?14:25??SocketClient\SocketClient\obj\Debug\SocketClient.csproj.FileListAbsolute.txt
?????文件????????1755??2017-07-14?15:11??SocketClient\SocketClient\obj\Debug\SocketClient.csprojResolveAssemblyReference.cache
?????文件????????6656??2017-07-18?14:24??SocketClient\SocketClient\obj\Debug\SocketClient.exe
?????文件???????15872??2017-07-18?14:24??SocketClient\SocketClient\obj\Debug\SocketClient.pdb
?????目錄???????????0??2017-07-18?14:33??SocketClient\SocketClient\obj\Debug\TempPE\
?????文件???????????0??2017-03-29?17:10??SocketClient\SocketClient\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
?????文件???????????0??2017-03-29?17:10??SocketClient\SocketClient\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
?????文件???????????0??2017-03-29?17:10??SocketClient\SocketClient\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
?????文件????????1005??2017-03-29?17:10??SocketClient\SocketClient.sln
?????文件???????30208??2017-07-18?14:25??SocketClient\SocketClient.v12.suo
?????目錄???????????0??2017-04-20?22:37??SocketServer\
?????目錄???????????0??2017-07-18?14:24??SocketServer\SocketServer\
?????文件?????????187??2017-03-29?16:50??SocketServer\SocketServer\App.config
............此處省略25個文件信息
- 上一篇:c# http接口設(shè)計及調(diào)用demo
- 下一篇:C#車牌識別
評論
共有 條評論