您好,登錄后才能下訂單哦!
這篇文章主要介紹了C#如何實現套接字發送接收數據,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體內容如下
namespace TestServer { public partial class ServerForm : Form { Socket socketSend; // 將遠程連接的客戶端的IP地址和socket存入集合 Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>(); public ServerForm() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { try { // 當點擊開始監聽后,在服務器創建一個負責監聽IP地址和端口號的socket Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Any;//;IPAddress.Parse(txtServer.Text) IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(txtPort.Text)); // 監聽 socketWatch.Bind(point); ShowMsg("監聽成功"); socketWatch.Listen(10); Thread th = new Thread(Listen); th.IsBackground = true; th.Start(socketWatch); } catch { } } void Listen(object o) { // 等待客戶端連接 并創建一個負責通信的Socket Socket socketWatch = o as Socket; while (true) { socketSend = socketWatch.Accept(); dicSocket.Add(socketSend.RemoteEndPoint.ToString(),socketSend); cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString()); ShowMsg(socketSend.RemoteEndPoint.ToString() + " : " + "連接成功"); Thread th = new Thread(Recive); th.IsBackground = true; th.Start(socketSend); } } void Recive(object o) { socketSend = o as Socket; while (true) { // 客戶端連接成功后,服務器應接受客戶端發來的消息 byte[] buffer = new byte[1024 * 1024 * 2]; // 實際接收到的有效字節數 int r = socketSend.Receive(buffer); if (r == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, r); ShowMsg(socketSend.RemoteEndPoint + " : " + str); } } void ShowMsg(string str) { txtLog.AppendText(str + "\r\n"); } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } private void btnSend_Click(object sender, EventArgs e) { string str = txtMsg.Text; byte[] buffer = Encoding.UTF8.GetBytes(str); string ip = cboUsers.SelectedItem.ToString(); dicSocket[ip].Send(buffer); //socketSend.Send(buffer); txtMsg.Text = ""; } private void btnSendUser_Click(object sender, EventArgs e) { string json = JsonConvert.SerializeObject(UserList()); byte[] buffer = Encoding.UTF8.GetBytes(json); socketSend.Send(buffer); } private List<User> UserList() { List<User> userList = new List<User>(); User user = null; string sql = "select * from user_table"; SqlCommand cmd = SqlUtils.ConnectSql(sql); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { user = new User(sdr.GetInt32(0), sdr.GetString(1), sdr.GetString(2), sdr.GetInt32(3)); userList.Add(user); } return userList; } } }
namespace TestClient { public partial class ClientForm : Form { Socket socketSend; public ClientForm() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { try { // 創建負責通信的socket socketSend = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); IPAddress ip = IPAddress.Parse(txtServer.Text); IPEndPoint point = new IPEndPoint(ip,Convert.ToInt32(txtPort.Text)); socketSend.Connect(point); ShowMsg("連接成功"); Thread th = new Thread(Recive); th.IsBackground = true; th.Start(); } catch { } } void Recive() { while (true) { // 客戶端連接成功后,服務器應接受客戶端發來的消息 byte[] buffer = new byte[1024 * 1024 * 2]; // 實際接收到的有效字節數 int r = socketSend.Receive(buffer); if (r == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, r); ShowMsg(socketSend.RemoteEndPoint + " : " + str); } } void ShowMsg(string str) { txtLog.AppendText(str + "\r\n"); } private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; } private void btnSend_Click(object sender, EventArgs e) { string str = txtMsg.Text.Trim(); byte[] buffer = Encoding.UTF8.GetBytes(str); socketSend.Send(buffer); txtMsg.Text = ""; } } }
效果
發送json數據
感謝你能夠認真閱讀完這篇文章,希望小編分享的“C#如何實現套接字發送接收數據”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。