当前位置:网站首页>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
边栏推荐
- wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- mysql导入文件出现Data truncated for column ‘xxx’ at row 1的原因
- 请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
- 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
- 数据库系统概论-第一章绪论【概念模型、层次模型和三级模式(外模式、模式、内模式)】
- Dry goods | summarize the linkage use of those vulnerability tools
- Use day JS let time (displayed as minutes, hours, days, months, and so on)
- 2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
猜你喜欢

Details of redis core data structure & new features of redis 6

Evolution of customer service hotline of dewu

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

VSCode 配置使用 PyLint 语法检查器

SAKT方法部分介绍
![[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?

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)

118. 杨辉三角

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

Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
随机推荐
Laravel form builder uses
Cesium knows the longitude and latitude of one point and the distance to find the longitude and latitude of another point
数据流图,数据字典
Introduction to sakt method
手里的闲钱是炒股票还是买理财产品好?
Assign a dynamic value to the background color of DataGrid through ivalueconverter
Environment configuration of lavarel env
Leetcode simple question sharing (20)
[high frequency interview questions] difficulty 2.5/5, simple combination of DFS trie template level application questions
UML 状态图
高等數學---第八章多元函數微分學1
请问指南针股票软件可靠吗?交易股票安全吗?
MySQL "invalid use of null value" solution
Environment configuration
648. Word replacement: the classic application of dictionary tree
TPG x AIDU | AI leading talent recruitment plan in progress!
高等数学---第八章多元函数微分学1
Attribute keywords aliases, calculated, cardinality, ClientName
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
Regular expression integer positive integer some basic expressions