基于PXA270嵌入式系統的Socket通信設計
3.1 客戶(hù)端程序設計
客戶(hù)端可以向服務(wù)器端發(fā)送連接請求,并且客戶(hù)端也可以接收到來(lái)自服務(wù)器端發(fā)送回來(lái)的數據??蛻?hù)端可以判斷當前自己的工作狀態(tài),如連接的建立,啟動(dòng)的成功和數據包通信的個(gè)數等??蛻?hù)端程序設計主要按以下的步驟完成函數的調用:
①建立自己的Socket(并驗證建立成功);
②啟動(dòng)連接(并驗證建立成功);
③返回連接信息;
④接收收到的數據;
⑤判斷數據的屬性。
客戶(hù)端程序設計的程序基本流程如圖3所示。

圖3 客戶(hù)端程序簡(jiǎn)單示意圖
客戶(hù)端打開(kāi)通信通道,并連接到服務(wù)器所在主機的特定端口,向服務(wù)器發(fā)送請求報文,等待并接收應答,請求結束后關(guān)閉通信通道并終止通信??蛻?hù)端主要程序如下:
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));
//調用connect函數連接服務(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地址和端口號
while(1)
bzero(gotbuffer,long);
number=recv(mysocket,gotbuffer,long,0);
//調用阻塞函數
if(number==-1)
printf(error!!! some thing wrong !let you can not got the data form server,program end heren);
return(0);
gotbuffer[number]='国产精品自在自线亚洲|国产精品无圣光一区二区|国产日产欧洲无码视频|久久久一本精品99久久K精品66|欧美人与动牲交片免费播放