C#以太網(wǎng)Sockets客戶端設(shè)計(jì)實(shí)現(xiàn)
【1】客戶端對(duì)象
using System.Net;// DNS_靜態(tài)對(duì)象 using System.Net.Sockets; // 字段位置 private Socket socket業(yè)務(wù); //對(duì)象既可以當(dāng)服務(wù)器,又可以當(dāng)客戶端 TcpListener tcpListener; //服務(wù)器對(duì)象 TcpClient tcpClient; //客戶端對(duì)象
【2】初始化
Socket tcpClient客戶端;
tcpClient客戶端 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
【3】連接
#region 連接
/// <summary>
/// 建立連接
/// </summary>
/// <param name="ip">IP地址</param>
/// <param name="port">端口號(hào)</param>
public void Connect(string ip, string port)
{
try
{
//實(shí)例化Socket
tcpClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//設(shè)置Socket屬性
tcpClient.SendTimeout = this.SendTimeOut;
tcpClient.ReceiveTimeout = this.ReceiveTimeOut;
//封裝一個(gè)EndPoint對(duì)象
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(ip), int.Parse(port));
//建立連接
tcpClient.Connect(endPoint);
}
catch (Exception ex) { MessageBox.Show(ex.Message); }//顯示錯(cuò)誤
}
#endregion【4】收發(fā)
#region 讀取并接受
/// <summary>
/// 發(fā)送并接受
/// </summary>
/// <param name="SendByte"></param>
/// <returns></returns>
public byte[] SendAndReceive(byte[] SendByte)
{
try
{
if (tcpClient.Available >= 1)// 丟棄字節(jié)
{
#region 加載
byte[] buffer = new byte[tcpClient.Available];//準(zhǔn)備加載
tcpClient.Receive(buffer, buffer.Length, SocketFlags.None);//開(kāi)始加載
#endregion
}
#region 計(jì)時(shí)
//====現(xiàn)在時(shí)間======================================
str_發(fā)送后記時(shí) = new StringBuilder(String.Format("{0,9}",
DateTime.Now.Second.ToString() + "s"
+ DateTime.Now.Millisecond.ToString() + "ms:")); //固定長(zhǎng)度9 右對(duì)齊
#endregion
tcpClient.Send(SendByte);// 發(fā)送
#region 委托
if (Help_ModBus.Wt_set != null)
{
Help_ModBus.Wt_set(SendByte, str_發(fā)送后記時(shí).ToString());
}
#endregion
return ReadMessage();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }//顯示錯(cuò)誤
finally
{
//InteractiveLock.Leave();
}
return null;
}
/// <summary>
/// 讀取緩沖區(qū)值
/// </summary>
/// <returns></returns>
private byte[] ReadMessage()
{
DateTime startTime = DateTime.Now;//記錄開(kāi)始時(shí)間
do
{
if (tcpClient.Available >= 5 )// 字節(jié)
{
#region 計(jì)時(shí)
//====現(xiàn)在時(shí)間======================================
str_發(fā)送后記時(shí) = new StringBuilder(String.Format("{0,9}",
DateTime.Now.Second.ToString() + "s"
+ DateTime.Now.Millisecond.ToString() + "ms:")); //固定長(zhǎng)度9 右對(duì)齊
#endregion
#region 加載
byte[] buffer = new byte[tcpClient.Available];//準(zhǔn)備加載
tcpClient.Receive(buffer, buffer.Length, SocketFlags.None);//開(kāi)始加載
#endregion
#region 委托
if (Help_ModBus.Wt_get != null)//委托呼叫ui
{
Help_ModBus.Wt_get(buffer, str_發(fā)送后記時(shí).ToString());// ui顯示
}
#endregion
return buffer;
}
#region 超時(shí)跳出
if ((DateTime.Now - startTime).TotalMilliseconds > ReceiveTimeOut+500)// 超時(shí) 1.5s
{
break;// 已經(jīng)超時(shí)
}
#endregion
} while (true);
return null; // 相當(dāng)失敗
//Thread.Sleep(ReceiveTimeOut);// 延時(shí)15ms
//int count = tcpClient.Available; // 字節(jié)
//if (count == 0)
//{
// return null;
//}
//else
//{
// byte[] buffer = new byte[count];
// tcpClient.Receive(buffer, count, SocketFlags.None);
// //byte[] buffer2 = Encoding.Default.GetBytes(str_發(fā)送后記時(shí).ToString());
// byte[] buffer3 = { buffer2 , buffer };
// //return buffer2;
// #region 計(jì)時(shí)
// //====現(xiàn)在時(shí)間======================================
// str_發(fā)送后記時(shí) = new StringBuilder(String.Format("{0,9}",
// DateTime.Now.Second.ToString() + "s"
// + DateTime.Now.Millisecond.ToString() + "ms:")); //固定長(zhǎng)度9 右對(duì)齊
// #endregion
// if (Wt_get!=null)
// {
// Wt_get(buffer, str_發(fā)送后記時(shí).ToString());
// }
// return buffer;
//}
//==================================
//int count = tcpClient.Available;
//int cycletimer = 0;
//while (count == 0)
//{
// count = tcpClient.Available;
// cycletimer++;
// Thread.Sleep(20);
// if (cycletimer > MaxCycleTimer)
// {
// break;
// }
//}
//if (count == 0)
//{
// return null;
//}
//else
//{
// byte[] buffer = new byte[count];
// tcpClient.Receive(buffer, count, SocketFlags.None);
// return buffer;
//}
}
#endregion【5】斷開(kāi)
#region 斷開(kāi)
/// <summary>
/// 斷開(kāi)連接
/// </summary>
public void DisConnect()
{
if (tcpClient != null)
{
tcpClient.Close();
}
}
#endregion
參數(shù)參考:
C#以太網(wǎng)Sockets服務(wù)器設(shè)計(jì)
到此這篇關(guān)于C#以太網(wǎng)Sockets客戶端設(shè)計(jì)實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C#以太網(wǎng)Sockets客戶端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#連接MySQL數(shù)據(jù)庫(kù)的方法步驟
最近兩天在解決C#連接MySql數(shù)據(jù)庫(kù)的問(wèn)題,通過(guò)不同的從網(wǎng)上學(xué)習(xí),最終找到了解決的辦法,下面這篇文章主要給大家介紹了關(guān)于C#連接MySQL數(shù)據(jù)庫(kù)的方法步驟,需要的朋友可以參考下2023-01-01
C#中foreach原理以及模擬的實(shí)現(xiàn)
這篇文章主要介紹了C#中foreach原理以及模擬的實(shí)現(xiàn)方法,備有詳盡的注釋,便于深入理解C#原理,需要的朋友可以參考下2014-10-10
Windows系統(tǒng)中C#讀寫ini配置文件的程序代碼示例分享
這篇文章主要介紹了C#讀寫ini配置文件的程序代碼示例分享,在Windows下可以利用Win32的API函數(shù)輕松實(shí)現(xiàn),需要的朋友可以參考下2016-04-04
C#實(shí)現(xiàn)訪問(wèn)Web API Url提交數(shù)據(jù)并獲取處理結(jié)果
Web API 是 Web 服務(wù)器和 Web 瀏覽器之間的應(yīng)用程序處理接口,我們常見(jiàn)的模式是訪問(wèn) Web API Url 地址,并獲取 Json 、XML或其它指定格式的處理結(jié)果, 本文我們介紹了使用C#實(shí)現(xiàn)訪問(wèn)Web API Url提交數(shù)據(jù)并獲取處理結(jié)果,需要的朋友可以參考下2024-05-05
C#實(shí)現(xiàn)時(shí)間戳與標(biāo)準(zhǔn)時(shí)間的互轉(zhuǎn)
本文主要介紹了C#中時(shí)間戳與標(biāo)準(zhǔn)時(shí)間互轉(zhuǎn)的方法,其中需要注意的是基準(zhǔn)時(shí)間的問(wèn)題。文中的示例代碼具有一定的學(xué)習(xí)價(jià)值,快來(lái)跟隨小編一起了解一下吧2021-12-12
C#中使用1.7版本驅(qū)動(dòng)操作MongoDB簡(jiǎn)單例子
這篇文章主要介紹了C#中使用1.7版本驅(qū)動(dòng)操作MongoDB簡(jiǎn)單例子,本文給出了連接MongoDB、操作MongoDB數(shù)據(jù)等例子,需要的朋友可以參考下2015-01-01

