C#實現(xiàn)Socket通信的解決方法
本文以實例詳述了C#實現(xiàn)Socket通信的解決方法,具體實現(xiàn)步驟如下:
1、首先打開VS新建兩個控制臺應用程序:
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)絡端點表示為IP地址和端口 用于socket偵聽時綁定 IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("*.*.*.*"), 3001); //填寫自己電腦的IP或者其他電腦的IP,如果是其他電腦IP的話需將ConsoleApplication_socketServer工程放在對應的電腦上。 clientSocket = new Socket(ipep.AddressFamily,SocketType.Stream,ProtocolType.Tcp); //將Socket連接到服務器 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); //接收服務器端信息 clientSocket.Receive(inBuffer, 1024, SocketFlags.None);//如果接收的消息為空 阻塞 當前循環(huán) Console.WriteLine("服務器說:"); Console.WriteLine(Encoding.ASCII.GetString(inBuffer)); } } catch { Console.WriteLine("服務未開啟!"); 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);//如果接收的消息為空 阻塞 當前循環(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、先運行ConsoleApplication_socketServer,后運行ConsoleApplication_socketClient就可以通信了。
本例給出了基本的實現(xiàn)代碼,讀者可以根據(jù)自身的需求進一步完成個性化功能。
相關(guān)文章
C# Split函數(shù)根據(jù)特定分隔符分割字符串的操作
這篇文章主要介紹了C# Split函數(shù)根據(jù)特定分隔符分割字符串的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12WinForm實現(xiàn)為TextBox設置水印文字功能
這篇文章主要介紹了WinForm實現(xiàn)為TextBox設置水印文字功能,很實用的一個技巧,需要的朋友可以參考下2014-08-08C#類型轉(zhuǎn)換之自定義隱式轉(zhuǎn)換和顯式轉(zhuǎn)換
本文主要為大家介紹了一個新的類型轉(zhuǎn)換方法:通過自定義隱式轉(zhuǎn)換,把不一樣的數(shù)據(jù)類型反序列化為一樣的數(shù)據(jù)類型,需要的同學可以參考一下2022-03-03