新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > 基于PXA270嵌入式系统的Socket通信设计

基于PXA270嵌入式系统的Socket通信设计

作者:时间:2009-05-18来源:网络收藏

客户端与服务器端进程的作用是非对称的,因此编码不同。同时,服务器进程一般是先于客户请求请求而启动的,只要运行,该服务进程一直存在,直到正常或强迫终止。服务器端主要程序如下:
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.eepw.com.cn/article/152489.htm

图4 服务器端程序简单示意图
{ 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); //调用监听函数
sin_size=sizeof(struct sockaddr_in);
newsocket=accept(mysocket,(struct sockaddr*)addr_remote,sin_size);
//调用接收函数
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(); //调用fork()建立子进程
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); //发送数据
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);

linux操作系统文章专题:linux操作系统详解(linux不再难懂)

tcp/ip相关文章:tcp/ip是什么




评论


相关推荐

技术专区

关闭