C#實(shí)現(xiàn)Socket通信的解決方法
本文以實(shí)例詳述了C#實(shí)現(xiàn)Socket通信的解決方法,具體實(shí)現(xiàn)步驟如下:
1、首先打開VS新建兩個(gè)控制臺(tái)應(yīng)用程序:
ConsoleApplication_socketServer和ConsoleApplication_socketClient。
2、在ConsoleApplication_socketClient中輸入以下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace ConsoleApplication_socketClient { class Program { static Socket clientSocket; static void Main(string[] args) { //將網(wǎng)絡(luò)端點(diǎn)表示為IP地址和端口 用于socket偵聽時(shí)綁定 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("*.*.*.*"), 3001); //填寫自己電腦的IP或者其他電腦的IP,如果是其他電腦IP的話需將ConsoleApplication_socketServer工程放在對(duì)應(yīng)的電腦上。 clientSocket = new Socket(ipep.AddressFamily,SocketType.Stream,ProtocolType.Tcp); //將Socket連接到服務(wù)器 try { clientSocket.Connect(ipep); String outBufferStr; Byte[] outBuffer = new Byte[1024]; Byte[] inBuffer = new Byte[1024]; while (true) { //發(fā)送消息 outBufferStr = Console.ReadLine(); outBuffer = Encoding.ASCII.GetBytes(outBufferStr); clientSocket.Send(outBuffer, outBuffer.Length, SocketFlags.None); //接收服務(wù)器端信息 clientSocket.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息為空 阻塞 當(dāng)前循環(huán) Console.WriteLine("服務(wù)器說:"); Console.WriteLine(Encoding.ASCII.GetString(inBuffer)); } } catch { Console.WriteLine("服務(wù)未開啟!"); Console.ReadLine(); } } } }
3、在ConsoleApplication_socketServer中輸入以下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; namespace ConsoleApplication_socketServer { class Program { static Socket serverSocket; static Socket clientSocket; static Thread thread; static void Main(string[] args) { IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 3001); serverSocket = new Socket(ipep.AddressFamily, SocketType.Stream, ProtocolType.Tcp); serverSocket.Bind(ipep); serverSocket.Listen(10); while (true) { clientSocket = serverSocket.Accept(); thread = new Thread(new ThreadStart(doWork)); thread.Start(); } } private static void doWork() { Socket s = clientSocket;//客戶端信息 IPEndPoint ipEndPoint = (IPEndPoint)s.RemoteEndPoint; String address = ipEndPoint.Address.ToString(); String port = ipEndPoint.Port.ToString(); Console.WriteLine(address + ":" + port + " 連接過來了"); Byte[] inBuffer = new Byte[1024]; Byte[] outBuffer = new Byte[1024]; String inBufferStr; String outBufferStr; try { while (true) { s.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息為空 阻塞 當(dāng)前循環(huán) inBufferStr = Encoding.ASCII.GetString(inBuffer); Console.WriteLine(address + ":" + port + "說:"); Console.WriteLine(inBufferStr); outBufferStr = Console.ReadLine(); outBuffer = Encoding.ASCII.GetBytes(outBufferStr); s.Send(outBuffer, outBuffer.Length, SocketFlags.None); } } catch { Console.WriteLine("客戶端已關(guān)閉!"); } } } }
4、先運(yùn)行ConsoleApplication_socketServer,后運(yùn)行ConsoleApplication_socketClient就可以通信了。
本例給出了基本的實(shí)現(xiàn)代碼,讀者可以根據(jù)自身的需求進(jìn)一步完成個(gè)性化功能。
相關(guān)文章
C# Split函數(shù)根據(jù)特定分隔符分割字符串的操作
這篇文章主要介紹了C# Split函數(shù)根據(jù)特定分隔符分割字符串的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12WinForm實(shí)現(xiàn)為TextBox設(shè)置水印文字功能
這篇文章主要介紹了WinForm實(shí)現(xiàn)為TextBox設(shè)置水印文字功能,很實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-08-08C#類型轉(zhuǎn)換之自定義隱式轉(zhuǎn)換和顯式轉(zhuǎn)換
本文主要為大家介紹了一個(gè)新的類型轉(zhuǎn)換方法:通過自定義隱式轉(zhuǎn)換,把不一樣的數(shù)據(jù)類型反序列化為一樣的數(shù)據(jù)類型,需要的同學(xué)可以參考一下2022-03-03WPF實(shí)現(xiàn)動(dòng)畫效果(六)之路徑動(dòng)畫
這篇文章介紹了WPF實(shí)現(xiàn)動(dòng)畫效果之路徑動(dòng)畫,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Silverlight實(shí)現(xiàn)星星閃爍動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了Silverlight實(shí)現(xiàn)星星閃爍動(dòng)畫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07