您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#基于Sockets類如何實現TCP通訊,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
最終效果
TCPClient
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net; using System.Net.Sockets; using System.Threading; namespace TCPClient02 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Socket socketSend; private void button1_Click(object sender, EventArgs e) { //Create socket socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(textBox1.Text); IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text)); IDInfo idinfo = new IDInfo(); //Read ID number information //Get the IP address and port number of the remote server socketSend.Connect(point); ShowMessages("Connection succeeded"); //Start a new thread and keep receiving messages sent by the server Thread th = new Thread(ReciveMessages); th.IsBackground = true; th.Start(); } private void button2_Click(object sender, EventArgs e) { string str = textBox3.Text.Trim(); byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str); socketSend.Send(buffer); } void ShowMessages(string str) { textBox4.AppendText(str + "\r\n"); } void ReciveMessages() { while (true) { byte[] buffer = new byte[1024 * 1024 * 3]; int r = socketSend.Receive(buffer); if (r == 0) { break; } string s = Encoding.UTF8.GetString(buffer, 0, r); ShowMessages(socketSend.RemoteEndPoint + ":" + s); } } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } } }
TCPserver
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Net.Sockets; using System.Net; using System.Threading; namespace TCPserver { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { //創建一個負責監聽的Socket Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //創建ip地址和端口號 //IPAddress ip = IPAddress.Parse(textBox1.Text); IPAddress ip = IPAddress.Any; IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text)); //讓負責監聽的socket綁定ip地址和端口號 socketWatch.Bind(point); ShowMsg("監聽成功"); //設置監聽隊列(某一時刻連接客戶端的最大數目) socketWatch.Listen(10); //線程執行的方法 Thread th = new Thread(Listen); //服務器開始監聽 th.IsBackground = true; th.Start(socketWatch); } catch { } } void ShowMsg(string str) { textBox3.AppendText(str + "\r\n"); } /// <summary> /// 等待客戶端的連接 并且創建與之通信的Socket /// </summary> /// Socket socketSend; void Listen(object o) { Socket socketWatch = o as Socket; //負責監聽的socket 來接收客戶端的連接 //創建跟客戶端通信的socket while (true) { try { socketSend = socketWatch.Accept(); ShowMsg(socketSend.RemoteEndPoint.ToString() + "連接成功"); //開始一個新的線程不斷接受客戶端發送過來的消息 Thread th = new Thread(Recive); th.IsBackground = true; th.Start(socketSend); } catch { } } } /// <summary> /// 服務器不斷接受客戶端發送過來的消息 /// </summary> /// <param name="o"></param> void Recive(object o) { Socket socketSend = o as Socket; while (true) { try { //客戶端連接成功后,服務器應該接收客戶端發來的消息 byte[] buffer = new byte[1024 * 1024 * 2]; //實際接收到的有效字節數 int bytelen = socketSend.Receive(buffer); if (bytelen == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, bytelen); ShowMsg(socketSend.RemoteEndPoint + ":" + str); } catch { } } } private void textBox1_TextChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } /// <summary> /// 服務器給客戶端發送消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button3_Click(object sender, EventArgs e) { string str = textBox4.Text; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str); socketSend.Send(buffer); } } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#基于Sockets類如何實現TCP通訊”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。