實現Socket通訊

2022-09-19 14:36:13 字數 3046 閱讀 2265

1.網頁測試工具

2.串列埠除錯工具llcom  相關資訊輸出到串列埠,便於掌握程式的運**況

3.arduino string庫函式詳解

本程式:使用socket實現通訊,dtu向伺服器傳送"ready"表示裝置上線,服務端向dtu傳送1或2控制dtu上led燈的顏色,3關閉所有socket,裝置下線;服務端發回的其他資訊可列印到串列埠

注意選擇開發板為m5stack atom

#include #include 

atom_dtu_nb dtu;

const

char* hex = "

0123456789abcdef";

string data1 = "

ready";

string readstr;

string str_hex;

string hex_str;

char getvalue(char x)

}int

s,ed;

void stringtohex(string thedata)

}void

hextostring(string thedata)

}void

setup()

while(!readstr);

serial.print(readstr);

if(readstr.endswith("

ok\r\n"))

else

dtu.sendmsg(

"at+csoc=1,1,1\r\n");

delay(

2);

dtu.sendmsg(

"at+csocon=0,37652,112.125.89.8\r\n");

dowhile(!readstr);

serial.print(readstr);

if(readstr.endswith("

ok\r\n"))

else

stringtohex(data1);

string tmp1 = "

at+csosend=0,10,";

tmp1 +=str_hex;

tmp1 += "

\r\n";

dtu.sendmsg(tmp1);

//向服務端傳送"ready"

}void

loop()

else

if(hex_str.equals("2"

))

else

if(hex_str.equals("

3"))

else

serial.print(hex_str);

}}

第一版更加嚴謹的第二版要注意的是dtu.waitmsg()讀到的字串都是以"\r\n"開頭和結束的,筆者在這上面跌了跟頭,使用startswith和endswith()函式多次因為沒注意到這一點產生bug

#include #include 

atom_dtu_nb dtu;

const

char* hex = "

0123456789abcdef";

string data1 = "

ready";

string readstr;

string str_hex;

string hex_str;

string tmp1;

char

socket_id;

char getvalue(char x)

}int

s,ed;

void stringtohex(string thedata)

}void

hextostring(string thedata)

}void

myread()

while(!readstr);

serial.print(readstr);

}void

setup()

else

dtu.sendmsg(

"at+csoc=1,1,1\r\n");

delay(

2);

myread();

socket_id = readstr.charat(readstr.indexof("

+csoc:

")+7

); tmp1 = "

at+csocon=*,34026,112.125.89.8\r\n";

tmp1.setcharat(tmp1.indexof("*

"),socket_id);

dtu.sendmsg(tmp1);

readstr = dtu.waitmsg(3000

);

if(readstr.endswith("

ok\r\n"))

else

stringtohex(data1);

tmp1 = "

at+csosend=*,10,";

tmp1.setcharat(tmp1.indexof("*

"),socket_id);

tmp1 +=str_hex;

tmp1 += "

\r\n";

dtu.sendmsg(tmp1);

//向服務端傳送"ready"

應用跑完,關閉掉所有的socket連線,是乙個好習慣。

perl實現socket通訊

perl中可以直接呼叫linux下c庫中berkeley套接字內建介面,呼叫方式和c語言一致,只是換成perl的語法而已。偶就不說了,網上一籮筐資料。我使用perl中的io socket模組來開發,個人經驗使用perl語言開發的過程中能使用模組的盡量使用,乙個是可靠,二個是方便,最主要的是便於跨平台...

C 實現Socket通訊

using system using system.collections.generic using system.linq using system.text using system.net using system.net.sockets class program static socke...

利用socket實現通訊

利用套接字 socket 實現通訊,client傳送兩個數,server接收並返回這兩個數的和 server.c include include include include include includeint main client.c include include include 包含套接...