基于PXA270嵌入式系統(tǒng)的Socket通信設(shè)計(jì)
3.1 客戶端程序設(shè)計(jì)
客戶端可以向服務(wù)器端發(fā)送連接請(qǐng)求,并且客戶端也可以接收到來(lái)自服務(wù)器端發(fā)送回來(lái)的數(shù)據(jù)??蛻舳丝梢耘袛喈?dāng)前自己的工作狀態(tài),如連接的建立,啟動(dòng)的成功和數(shù)據(jù)包通信的個(gè)數(shù)等??蛻舳顺绦?a class="contentlabel" href="http://cafeforensic.com/news/listbylabel/label/設(shè)計(jì)">設(shè)計(jì)主要按以下的步驟完成函數(shù)的調(diào)用:
①建立自己的Socket(并驗(yàn)證建立成功);
②啟動(dòng)連接(并驗(yàn)證建立成功);
③返回連接信息;
④接收收到的數(shù)據(jù);
⑤判斷數(shù)據(jù)的屬性。
客戶端程序設(shè)計(jì)的程序基本流程如圖3所示。
本文引用地址:http://cafeforensic.com/article/152489.htm
圖3 客戶端程序簡(jiǎn)單示意圖
客戶端打開(kāi)通信通道,并連接到服務(wù)器所在主機(jī)的特定端口,向服務(wù)器發(fā)送請(qǐng)求報(bào)文,等待并接收應(yīng)答,請(qǐng)求結(jié)束后關(guān)閉通信通道并終止通信??蛻舳酥饕绦蛉缦拢?br /> Int main(int argc,char *argv[])
if(argc!=3)
printf(error!!!please enter the remote IP and PORT please!!! the form like 192.168.0.* 4000n);
mysocket=socket(AF_INET,SOCK_STREAM,0); //建立一個(gè)套接字
if(mysocket==-1)
printf(error!!! failed to created the new socket,program end heren);
printf(OK-- you have successful created a socket named mysocketn);
return(0); //socket 建立不成功,回初始位置
connectcheck=connect(mysocket,(struct sockaddr*)addr_remote,sizeof(struct sockaddr));
//調(diào)用connect函數(shù)連接服務(wù)器端
if(connectcheck==-1)
printf(error!!!sorry you have failed to connect the remote server!!try again !program end heren);
// connect不成功回初始位置
printf(OK-- Now you have successful connect the server,this server IP =%s,and it's PORT =%s,now you can communicat with this server!!!!!!!n ,argv[1],argv[2])
//打印服務(wù)器IP地址和端口號(hào)
while(1)
bzero(gotbuffer,long);
number=recv(mysocket,gotbuffer,long,0);
//調(diào)用阻塞函數(shù)
if(number==-1)
printf(error!!! some thing wrong !let you can not got the data form server,program end heren);
return(0);
gotbuffer[number]='