可以看到,總的工作流程就是:
- 服務端監(jiān)聽
- 客戶端(可以有多個)請求連接
- 確認連接進行通信
針對服務器端的工作流程是:
- 建立Socket對象
- Bind(ip, port)綁定端口。比如說本地的(127.0.0.1, 6666)
- Listen()開始監(jiān)聽
- Accept()接受客戶端連接
- Send()和Receive()進行通信
- Close()關閉套接字
而針對客戶端的工作流程是:
- 建立Socket對象
- Connet(ip, port)向某個IP下的PORT端口發(fā)起連接請求,接受后開始通信
- Send()和Receive()進行通信
- Close() 關閉套接字
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net.Sockets; using System.Net; using System.Threading;
namespace socket { class Program {
static Socket serverSocket;
static void Main(string[] args) { SetListen(); //開始監(jiān)聽
}
static void SetListen() { serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try { //這里最好改成IPAddress.Any, 這樣放到服務器上就會同時監(jiān)聽所有網(wǎng)卡上的端口,比如有內外雙網(wǎng)的服務器 serverSocket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888)); serverSocket.Listen(2);
Thread thread = new Thread(Listen); thread.Start(serverSocket);
} catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Listen(object so) { Socket serverSocket = so as Socket;
while (true) { try { Socket clientSocket = serverSocket.Accept(); //接受客戶端接入 // 獲取鏈接IP地址 string clientPoint = clientSocket.RemoteEndPoint.ToString();
//開啟新線程來不停接受信息 Thread rec = new Thread(Receive); rec.Start(clientSocket);
//開啟新線程來不停發(fā)送信息 Thread sen = new Thread(Send); sen.Start(clientSocket); } catch(Exception ex) { Console.WriteLine(ex.Message, ex); break; } } serverSocket.Close(); }
static void Receive(object so) { Socket clientSocket = so as Socket;
string clientPoint = clientSocket.RemoteEndPoint.ToString(); while (true) { try { byte[] buffer = new byte[1024]; int len = clientSocket.Receive(buffer); //Console.WriteLine(len.ToString(), buffer); if (len == 0) break;
string msg = Encoding.UTF8.GetString(buffer, 0, len); Console.WriteLine("客戶端說:"+ msg); } catch(Exception ex) { Console.WriteLine(ex); } } }
static void Send(object so) { Socket clientSocket = so as Socket;
while (true) { //獲取控制臺輸入 string input = Console.ReadLine();
byte[] msg = Encoding.UTF8.GetBytes(input); clientSocket.Send(msg); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Net; using System.Net.Sockets; using System.Threading;
namespace socket_client { class Program { static Socket clientSocket;
static void Main(string[] args) { Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //連接 Connect(); }
//連接客戶端,連接后開始監(jiān)聽發(fā)送過來的消息并且把輸入的信息發(fā)給服務端 static void Connect() { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { //連接 clientSocket.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
//獨立線程來接受來自服務端的數(shù)據(jù) Thread receive = new Thread(Receive); receive.Start(clientSocket);
//獨立線程來發(fā)送數(shù)據(jù)給服務端 Thread send = new Thread(Send); send.Start(clientSocket); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
static void Receive(object so) { Socket clientSocket = so as Socket;
while (true) { Console.ForegroundColor = ConsoleColor.Green; // 修改文字顏色為綠色 try { byte[] buffer = new byte[1024]; int len = clientSocket.Receive(buffer); if (len > 0) { string msg = Encoding.UTF8.GetString(buffer); Console.WriteLine("服務端說:"+ msg); } } catch (Exception ex) { Console.WriteLine(ex); break; } } clientSocket.Close(); }
static void Send(object so) { Socket clientSocket = so as Socket;
while (true) // 監(jiān)聽鍵盤輸入 { Console.ForegroundColor = ConsoleColor.Red; //獲取鍵盤輸入 string input = Console.ReadLine(); //編碼 byte[] buffer = Encoding.UTF8.GetBytes(input); //發(fā)送 clientSocket.Send(buffer); } } } }
|