当前位置:网站首页>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
边栏推荐
- 3D Detection: 3D Box和点云 快速可视化
- Redis can only cache? Too out!
- Horizontal of libsgm_ path_ Interpretation of aggregation program
- Evolution of customer service hotline of dewu
- 带你掌握三层架构(建议收藏)
- 数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
- The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
- Dry goods | summarize the linkage use of those vulnerability tools
- Arm cortex-a9, mcimx6u7cvm08ad processor application
- The meaning of variables starting with underscores in PHP
猜你喜欢

Dry goods | summarize the linkage use of those vulnerability tools

Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf

2022-7-6 Leetcode 977. Square of ordered array

2022-7-7 Leetcode 844. Compare strings with backspace

Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques

Vmware共享主机的有线网络IP地址

118. 杨辉三角

How to check the ram and ROM usage of MCU through Keil

Redis 核心数据结构 & Redis 6 新特性详

Data flow diagram, data dictionary
随机推荐
. Net core about redis pipeline and transactions
[Reading stereo matching papers] [III] ints
内存溢出和内存泄漏的区别
Csma/cd carrier monitoring multipoint access / collision detection protocol
2022-7-6 beginner redis (I) download, install and run redis under Linux
请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
How to check the ram and ROM usage of MCU through Keil
Redis 核心数据结构 & Redis 6 新特性详
Cesium 已知一点经纬度和距离求另一个点的经纬度
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
UML sequence diagram (sequence diagram)
PHP中用下划线开头的变量含义
Wired network IP address of VMware shared host
Selenium库
Help tenants
Is it safe to open an account online now? Which securities company should I choose to open an account online?
Learning breakout 2 - about effective learning methods
【日常训练】648. 单词替换
使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
【网络安全】sql注入语法汇总