網上有大量socket相關文章,茫茫多,大多交代不清,最近自我整理了一下socket相關知識,附加了大量代碼註釋,先看效果。數組
上代碼,客戶端:服務器
1 Socket socket1 = null;//一個全局的socket對象 2 3 private void btnConnect_Click(object sender, EventArgs e) 4 { 5 //得到服務器ip和端口號 6 IPAddress ip = IPAddress.Parse(textBox1.Text); 7 int port =Convert.ToInt32( textBox2.Text); 8 9 //構造客戶端socket,設置爲tcp協議模式 10 socket1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 11 12 //鏈接 13 socket1.Connect(ip, port); 14 if (socket1.Connected)//若是鏈接成功 15 { 16 textBox3.Text += "鏈接成功\r\n"; 17 SendMsg(socket1, "發成功"); 18 19 string recMsg= ""; 20 //接收 21 ReceiveMsg(socket1, ref recMsg); 22 textBox3.Text +=recMsg;//文本顯示 23 } 24 else 25 { 26 textBox3.Text += "鏈接不成功\r\n"; 27 } 28 } 29 30 private void btnSendMessage_Click(object sender, EventArgs e) 31 { 32 if (socket1.Connected) 33 { 34 string sendMsg = textBox4.Text; 35 36 //依據長度判斷是否發送 37 if (sendMsg.Length <= 0) 38 { 39 return; 40 } 41 42 //拿到發送是否成功的標記 43 bool isSendSuccess = SendMsg(socket1, textBox4.Text); 44 45 string recMsg = ""; 46 bool isReceiveSuccess = ReceiveMsg(socket1, ref recMsg); 47 textBox3.Text += recMsg; 48 49 if (!(isSendSuccess&&isReceiveSuccess)) 50 { 51 textBox3.Text += "鏈接已斷開,請重連"; 52 } 53 } 54 } 55 56 /// <summary> 57 /// 發送內容,並顯示 58 /// </summary> 59 /// <param name="socket">發送內容的socket</param> 60 /// <param name="msg">輸入字符串</param> 61 /// <returns>發送是否成功</returns> 62 private bool SendMsg(Socket socket, string msg) 63 { 64 try { 65 socket.Send(Encoding.UTF8.GetBytes(msg)); 66 return true; 67 } 68 catch (Exception e) 69 { 70 socket.Close(); 71 return false; 72 } 73 } 74 75 /// <summary> 76 /// 接收內容,並顯示 77 /// </summary> 78 /// <param name="socket">接受內容的socket</param> 79 /// <param name="msg">輸出字符串</param> 80 /// <returns>接收是否成功</returns> 81 private static bool ReceiveMsg(Socket socket ,ref string msg) 82 { 83 try 84 { 85 int recNumber;//接收到的字節數量 86 byte[] recBytes = new byte[1024];//緩衝字節數組 87 recNumber = socket.Receive(recBytes);//接收行爲 88 byte[] recBytesForShort = new byte[recNumber];//byteArrayForDestination 89 Array.Copy(recBytes, recBytesForShort, recNumber);//拷貝 90 91 //顯示文本構造 92 msg += (Encoding.UTF8.GetString(recBytesForShort) + "\r\n"); 93 94 return true; 95 } 96 catch (Exception e) 97 { 98 socket.Close(); 99 100 return false; 101 } 102 103 }
服務端:多線程
1 static void Main(string[] args) 2 { 3 //設置監聽的ip和端口號 4 IPAddress ip = IPAddress.Parse("192.168.120.56"); 5 IPEndPoint ipEP = new IPEndPoint(ip, 8088); 6 7 //迎賓socket 8 Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 9 //綁定 10 serverSocket.Bind(ipEP); 11 //監聽 12 serverSocket.Listen(10); 13 Console.WriteLine("監聽已開\r\n"); 14 15 while (true) 16 { 17 //這裏會阻塞當前線程,以等待鏈接 18 Socket socketTemp = serverSocket.Accept(); 19 20 //開啓其餘線程處理數據 21 ParameterizedThreadStart pts = new ParameterizedThreadStart(ReceiveAndSend);//這裏傳入的方法必須是靜態 22 new Thread(pts).Start(socketTemp); 23 } 24 25 } 26 27 /// <summary> 28 /// socket循環讀取和發送的方法,建議用於多線程異步處理 29 /// </summary> 30 /// <param name="socketTemp">單用戶Socket對象</param> 31 private static void ReceiveAndSend(object socketTemp) 32 { 33 Socket socket = (Socket)socketTemp; 34 IPEndPoint ipep = ((IPEndPoint)(socket.RemoteEndPoint)); 35 string ip = ipep.Address.ToString();//得到對方ip 36 string port = ipep.Port.ToString();//得到對方端口 37 byte[] buffer = new byte[1024]; 38 39 while (true)//循環接收和發送 40 { 41 //接收並顯示 42 string result=""; 43 bool isReceiveMsgSuccess = ReceiveMsg(socket, ref result); 44 if (isReceiveMsgSuccess) 45 { 46 Console.WriteLine(DateTime.Now.ToLongDateString()+" "+ DateTime.Now.ToLongTimeString()); 47 Console.WriteLine(ip + " _ " + port + ":" + result); 48 } 49 50 //回發 51 string sendBackStr = "服務端已接收:" + result; 52 SendMsg(socket, sendBackStr); 53 54 //socket.Shutdown(SocketShutdown.Both); 55 //socket.Close(); 56 //socket.Dispose(); 57 } 58 } 59 60 /// <summary> 61 /// 接收內容(若是失敗,會釋放socket) 62 /// </summary> 63 /// <param name="socket">接受內容的socket</param> 64 /// <param name="msg">輸出字符串</param> 65 /// <returns>接收是否成功</returns> 66 private static bool ReceiveMsg(Socket socket,ref string msg) 67 { 68 try 69 { 70 int recNumber;//接收到的字節數量 71 byte[] recBytes = new byte[1024];//緩衝字節數組 72 recNumber = socket.Receive(recBytes);//接收行爲 73 byte[] recBytesForShort = new byte[recNumber]; 74 Array.Copy(recBytes, recBytesForShort, recNumber);//拷貝 75 76 //顯示 77 msg += Encoding.UTF8.GetString(recBytesForShort) + "\r\n"; 78 79 return true; 80 } 81 catch (Exception e) 82 { 83 socket.Close(); 84 85 return false; 86 } 87 88 } 89 90 /// <summary> 91 /// 發送內容(若是失敗,會釋放socket) 92 /// </summary> 93 /// <param name="socket">發送內容的socket</param> 94 /// <param name="msg">輸入字符串</param> 95 /// <returns>接收是否成功</returns> 96 private static bool SendMsg(Socket socket, string msg) 97 { 98 try 99 { 100 socket.Send(Encoding.UTF8.GetBytes(msg)); 101 return true; 102 } 103 catch (Exception e) 104 { 105 socket.Close(); 106 return false; 107 } 108 }