Socket-客戶端向伺服器端傳送訊息
阿新 • • 發佈:2020-07-02
客戶端:
介面:
程式碼:
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; namespace Test2.Client {public partial class Form1 : Form { Socket clientSocket; IPEndPoint ipEndPoint; public Form1() { InitializeComponent(); button1.Text = "連線"; button2.Text = "傳送"; button3.Text = "關閉"; } private void button1_Click(objectsender, EventArgs e) { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ipEndPoint = new IPEndPoint(IPAddress.Parse("xxxxxxxxx"), 52555); clientSocket.Connect(ipEndPoint); } private void button2_Click(objectsender, EventArgs e) { clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(textBox1.Text)); } private void button3_Click(object sender, EventArgs e) { SafeClose(clientSocket); } public void SafeClose( Socket socket) { if (socket == null) return; if (!socket.Connected) return; try { socket.Shutdown(SocketShutdown.Both); } catch { } try { socket.Close(); } catch { } } } }
伺服器端:
介面:
程式碼:
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 Test2.Server { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.FormClosed += Form1_FormClosed; } private void Form1_FormClosed(object sender, FormClosedEventArgs e) { SafeClose(socketSend); } public void SafeClose( Socket socket) { if (socket == null) return; if (!socket.Connected) return; try { socket.Shutdown(SocketShutdown.Both); } catch { } try { socket.Close(); } catch { } } Socket socketSend; private void button1_Click(object sender, EventArgs e) { try { //點選開始監聽時 在服務端建立一個負責監聽IP和埠號的Socket Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress ip = IPAddress.Any; //建立物件埠 IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(52555)); socketWatch.Bind(point);//繫結埠號 MessageBox.Show("Watching"); socketWatch.Listen(10);//設定監聽 //建立監聽執行緒 Thread thread = new Thread(Listen); thread.IsBackground = true; thread.Start(socketWatch); } catch { } } void Listen(object o) { try { Socket socketWatch = o as Socket; while (true) { socketSend = socketWatch.Accept();//等待接收客戶端連線 //ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "連線成功!"); //開啟一個新執行緒,執行接收訊息方法 Thread r_thread = new Thread(Received); r_thread.IsBackground = true; r_thread.Start(socketSend); } } catch { } } void Received(object o) { try { Socket socketSend = o as Socket; while (true) { //客戶端連線伺服器成功後,伺服器接收客戶端傳送的訊息 byte[] buffer = new byte[1024 * 1024 * 3]; //實際接收到的有效位元組數 int len = socketSend.Receive(buffer); if (len == 0) { break; } string str = Encoding.UTF8.GetString(buffer, 0, len); this.Invoke(new Action(() => { listBox1.Items.Add(str + "\r\n"); })); } } catch { } } } }