C#基于Sockets類實(shí)現(xiàn)TCP通訊
本文實(shí)例為大家分享了C#基于Sockets類實(shí)現(xiàn)TCP通訊的具體代碼,供大家參考,具體內(nèi)容如下
最終效果
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 ? ? ? ? ? ? { ? ? ? ? ? ? ? ? //創(chuàng)建一個(gè)負(fù)責(zé)監(jiān)聽的Socket ? ? ? ? ? ? ? ? Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ? ? ? ? ? ? ? ? //創(chuàng)建ip地址和端口號(hào) ? ? ? ? ? ? ? ? //IPAddress ip = IPAddress.Parse(textBox1.Text); ? ? ? ? ? ? ? ? IPAddress ip = IPAddress.Any; ? ? ? ? ? ? ? ? IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(textBox2.Text)); ? ? ? ? ? ? ? ? //讓負(fù)責(zé)監(jiān)聽的socket綁定ip地址和端口號(hào) ? ? ? ? ? ? ? ? socketWatch.Bind(point); ? ? ? ? ? ? ? ? ShowMsg("監(jiān)聽成功"); ? ? ? ? ? ? ? ? //設(shè)置監(jiān)聽隊(duì)列(某一時(shí)刻連接客戶端的最大數(shù)目) ? ? ? ? ? ? ? ? socketWatch.Listen(10); ? ? ? ? ? ? ? ? //線程執(zhí)行的方法 ? ? ? ? ? ? ? ? Thread th = new Thread(Listen); ? //服務(wù)器開始監(jiān)聽 ? ? ? ? ? ? ? ? th.IsBackground = true; ? ? ? ? ? ? ? ? th.Start(socketWatch); ? ? ? ? ? ? } ? ? ? ? ? ? catch ? ? ? ? ? ? { ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? void ShowMsg(string str) ? ? ? ? { ? ? ? ? ? ? textBox3.AppendText(str + "\r\n"); ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 等待客戶端的連接 并且創(chuàng)建與之通信的Socket ? ? ? ? /// </summary> ? ? ? ? ///? ? ? ? ? Socket socketSend; ? ? ? ? void Listen(object o) ? ? ? ? { ? ? ? ? ? ? Socket socketWatch = o as Socket; ? ? ? ? ? ? //負(fù)責(zé)監(jiān)聽的socket 來接收客戶端的連接 ? ? ? ? ? ? ? //創(chuàng)建跟客戶端通信的socket ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? try ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? socketSend = socketWatch.Accept(); ? ? ? ? ? ? ? ? ? ? ShowMsg(socketSend.RemoteEndPoint.ToString() + "連接成功"); ? ? ? ? ? ? ? ? ? ? //開始一個(gè)新的線程不斷接受客戶端發(fā)送過來的消息 ? ? ? ? ? ? ? ? ? ? Thread th = new Thread(Recive); ? ? ? ? ? ? ? ? ? ? th.IsBackground = true; ? ? ? ? ? ? ? ? ? ? th.Start(socketSend); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? catch ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } ? ? ? ? /// <summary> ? ? ? ? /// 服務(wù)器不斷接受客戶端發(fā)送過來的消息 ? ? ? ? /// </summary> ? ? ? ? /// <param name="o"></param> ? ? ? ? void Recive(object o) ? ? ? ? { ? ? ? ? ? ?? ? ? ? ? ? ? Socket socketSend = o as Socket; ? ? ? ? ? ? while (true) ? ? ? ? ? ? { ? ? ? ? ? ? ? ?try ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? //客戶端連接成功后,服務(wù)器應(yīng)該接收客戶端發(fā)來的消息 ? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[1024 * 1024 * 2]; ? ? ? ? ? ? ? ? ? ? //實(shí)際接收到的有效字節(jié)數(shù) ? ? ? ? ? ? ? ? ? ? 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> ? ? ? ?/// 服務(wù)器給客戶端發(fā)送消息 ? ? ? ?/// </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); ? ? ? ? } ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 動(dòng)畫窗體(AnimateWindow)的小例子
C# 動(dòng)畫窗體(AnimateWindow)的小例子,需要的朋友可以參考一下2013-03-03C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程
本文主要介紹了C#用Parallel.Invoke方法盡可能并行執(zhí)行提供的每個(gè)線程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實(shí)現(xiàn)
本文主要介紹了C#創(chuàng)建及訪問網(wǎng)絡(luò)硬盤的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03基于WPF實(shí)現(xiàn)簡(jiǎn)單的文件夾比較工具
文件比較平常都是用Beyond?Compare,可以說離不開的神器,不過Beyond?Compare平常拿它主要是用來做代碼比較,用來做一些大批量的二進(jìn)制文件比較,其實(shí)有點(diǎn)不是很方便,所以本文來用WPF做一個(gè)簡(jiǎn)單的文件夾比較的小工具2023-05-05C# 讀取ttf字體文件里的Unicode實(shí)現(xiàn)
這篇文章主要介紹了C# 讀取 ttf字體文件里的 Unicode實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法
這篇文章主要介紹了WPF中的ListBox實(shí)現(xiàn)按塊顯示元素的方法,涉及ListBox屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2016-09-09