基于PXA270嵌入式系統(tǒng)的Socket通信設(shè)計(jì)
客戶(hù)端與服務(wù)器端進(jìn)程的作用是非對(duì)稱(chēng)的,因此編碼不同。同時(shí),服務(wù)器進(jìn)程一般是先于客戶(hù)請(qǐng)求請(qǐng)求而啟動(dòng)的,只要系統(tǒng)運(yùn)行,該服務(wù)進(jìn)程一直存在,直到正常或強(qiáng)迫終止。服務(wù)器端主要程序如下:
mysocket=socket(AF_INET,SOCK_STREAM,0);
//建立新的套接字
if(mysocket==-1)
printf(error!!! failed to created mysocketn);
return(0); //socket建立不成功,回初始位置
mybindcheck=bind(mysocket,(struct sockaddr*)addr_local,sizeof(struct sockaddr));
if(mybindcheck==-1)
printf(error!!!failed to bind the IP and port with mysocketn);
return(0);
本文引用地址:http://www.ex-cimer.com/article/152489.htm
圖4 服務(wù)器端程序簡(jiǎn)單示意圖
{ printf(OK--you have successed bind your IP with port %dn,port);
listencheck=listen(mysocket,howmany);
if(listencheck==-1)
printf(error!!! you have failed listen this port,program end heren);
return(0); //調(diào)用監(jiān)聽(tīng)函數(shù)
sin_size=sizeof(struct sockaddr_in);
newsocket=accept(mysocket,(struct sockaddr*)addr_remote,sin_size);
//調(diào)用接收函數(shù)
if(newsocket==-1)
printf(error!!!failed to got remote connect this server,program end heren);
return(0); //建立新的socket失敗返回
printf(OK-- now have created the newsocket to use this own connection,use this communicate with clint%sn,inet_ntoa(addr_remote.sin_addr),port);
printf(OK-- server have successed got connect from clint IP = %s,port = %d,now connecting is running;n,inet_ntoa(addr_remote.sin_addr),port)
pid_t pid;
pid=fork(); //調(diào)用fork()建立子進(jìn)程
if(pid>0)
printf(OK-- i am a father procces,child proccess will continue for you,it's ID= %d,now end newsock and use old socket to listen again................. n,pid);
close(newsocket);
printf(OK-- i am a child procces,i am responsible for this new communicate,blow i will do for connectn);
printf(OK-- please enter your data which you want to send n);
while(1)
bzero(sendbuffer,long);
scanf(%s,sendbuffer);
sendcheck=send(newsocket,sendbuffer,strlen(sendbuffer),0); //發(fā)送數(shù)據(jù)
if(sendcheck==-1)
printf(error!!!failed to send to remoten);
close(newsocket);
else
printf(OK--now you have send %d byte data to remote!!!pleases send again!!!!n,sendcheck);
tcp/ip相關(guān)文章:tcp/ip是什么
評(píng)論