当前位置:网站首页>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
边栏推荐
- AI talent cultivation new ideas, this live broadcast has what you care about
- 3D detection: fast visualization of 3D box and point cloud
- Horizontal of libsgm_ path_ Interpretation of aggregation program
- Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
- [untitled]
- THINKPHP框架的优秀开源系统推荐
- Cargo placement problem
- Huawei image address
- Common response status codes
- Arm cortex-a9, mcimx6u7cvm08ad processor application
猜你喜欢

带你掌握三层架构(建议收藏)

XML文件的解析操作
![Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]](/img/d3/20674983717d829489149b4d3bfedf.png)
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]

使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )

PERT图(工程网络图)

AI人才培育新思路,这场直播有你关心的

Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou

566. 重塑矩阵

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

最长上升子序列模型 AcWing 1012. 友好城市
随机推荐
AI人才培育新思路,这场直播有你关心的
接口自动化测试-接口间数据依赖问题解决
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
Build a secure and trusted computing platform based on Kunpeng's native security
Battle Atlas: 12 scenarios detailing the requirements for container safety construction
GVIM [III] [u vimrc configuration]
【AI实战】应用xgboost.XGBRegressor搭建空气质量预测模型(二)
SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
IP and long integer interchange
Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Flask session forged hctf admin
Oracle advanced (V) schema solution
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
2022-7-6 beginner redis (I) download, install and run redis under Linux
Take you to master the three-tier architecture (recommended Collection)
现在网上开户安全么?那么网上开户选哪个证券公司?
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
Best practice | using Tencent cloud AI willingness to audit as the escort of telephone compliance
Selenium库