当前位置:网站首页>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
边栏推荐
- Cesium 已知一点经纬度和距离求另一个点的经纬度
- How can the PC page call QQ for online chat?
- ES日志报错赏析-Limit of total fields
- Selenium库
- 数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
- 一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
- First choice for stock account opening, lowest Commission for stock trading account opening, is online account opening safe
- 【日常训练】648. 单词替换
- MySQL "invalid use of null value" solution
- The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
猜你喜欢
2022-7-6 beginner redis (I) download, install and run redis under Linux
Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
118. Yanghui triangle
"New red flag Cup" desktop application creativity competition 2022
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
通过 iValueConverter 给datagrid 的背景颜色 动态赋值
数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
随机推荐
Arm cortex-a9, mcimx6u7cvm08ad processor application
Flask session forged hctf admin
Laravel form builder uses
Mysql怎样控制replace替换的次数?
最长上升子序列模型 AcWing 1012. 友好城市
[daily training] 648 Word replacement
GVIM [III] [u vimrc configuration]
2022-7-7 Leetcode 844. Compare strings with backspace
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
Csma/cd carrier monitoring multipoint access / collision detection protocol
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
Laravel5 call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败
供应链供需预估-[时间序列]
【日常训练】648. 单词替换
Environment configuration of lavarel env
Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
Assign a dynamic value to the background color of DataGrid through ivalueconverter
UML 状态图
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]