当前位置:网站首页>C # use TCP protocol to establish connection
C # use TCP protocol to establish connection
2022-07-07 14:12:00 【Peacock Flying Southeast - Shenzhen】
background : There is one deployed in Raspberry pie A server on Need to use windows The system is connected The data format is json Format So this article only has the client code :
1 Declare a global variable
Socket socket = null;2 stay mainWindow Write in :
IPAddress ipadr = IPAddress.Parse("192.168.2.42"); //---ip The address is the server address
socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
socket.Connect(ipadr,1616); //------------ It depends on whether the server has set the port number
}catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(" Failed to connect to server Please press enter to exit !");
return;
}3 Button trigger event
private void Button_Click(object sender, RoutedEventArgs e)
{
//---- Definition json data format
Comand com = new Comand(); //------ You need to define additional classes Class has two properties All are string type
com.SendCmd = "1";
com.msg = "null";
string jsonData = JsonConvert.SerializeObject(com); //---- Need extra in The package manager console imports packages Otherwise, the report will be wrong
socket.Send(Encoding.ASCII.GetBytes(jsonData));
try
{
Thread receiveThread = new Thread(ReceiveMessage); //---- The method of receiving messages is defined below
receiveThread.Start(receiveThread);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}4 ReceiveMessage Method
public void ReceiveMessage(object obj)
{
try
{
byte[] bt = new byte[1024];
int len = socket.Receive(bt);
string data = Encoding.ASCII.GetString(bt,0,len);
listBox.Dispatcher.BeginInvoke( //---- Start delegation
new Action(() => { listBox.Items.Add("\r\n Received data :" + data);}),null);
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(" Accept failure Please confirm whether it is disconnected !");
// receiveSocket.Shutdown(SocketShutdown.Both);
// receiveSocket.Close();
return;
}
}ps: There is a drawback Raspberry pie There will be a black screen You need to restart the software when you open it again windows Here, write the connection in the main method That means the raspberry pie reopens here windows The software should also be reopened Reconnect the This is a bad point
边栏推荐
- Beginner XML
- IP address home location query
- 最长上升子序列模型 AcWing 1012. 友好城市
- 使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- Laravel Form-builder使用
- 一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
- Dry goods | summarize the linkage use of those vulnerability tools
- Excuse me, why is it that there are no consumption messages in redis and they are all piled up in redis? Cerely is used.
- 手把手教会:XML建模
猜你喜欢

Parsing of XML files

2022-7-7 Leetcode 34. Find the first and last positions of elements in a sorted array

docker部署oracle

Vscode configuration uses pylint syntax checker

AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?

高等数学---第八章多元函数微分学1

Vmware共享主机的有线网络IP地址
![供应链供需预估-[时间序列]](/img/2c/82d118cfbcef4498998298dd3844b1.png)
供应链供需预估-[时间序列]
![[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?](/img/fb/17e029b1d955965d7e2e0f58701d91.png)
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?

最长上升子序列模型 AcWing 482. 合唱队形
随机推荐
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
Selenium库
请问,如图,pyhon云函数提示使用了 pymysql模块,这个是怎么回事?
Supply chain supply and demand estimation - [time series]
Vmware 与主机之间传输文件
FC连接数据库,一定要使用自定义域名才能在外面访问吗?
c#通过frame 和 page 切换页面
Details of redis core data structure & new features of redis 6
Laravel Form-builder使用
THINKPHP框架的优秀开源系统推荐
Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
高等數學---第八章多元函數微分學1
为租客提供帮助
Environment configuration
参数关键字Final,Flags,Internal,映射关键字Internal
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
requires php ~7.1 -> your PHP version (7.0.18) does not satisfy that requirement
Data flow diagram, data dictionary