|
server.c:
j=read(fd,a,(sizeof a -1));
if(j==-1){close(fd);bail("read()");}
if(j==0){close(fd);printf("end of file\n");exit(1);}
else
{
a[j]=0;
printf("read:%s\n",a);
k=sendto(s,a,strlen(a),0,(struct sockaddr *)&adrc,l);
if(k==-1)bail("sendto()");
}
k=recvfrom(s,b,sizeof b,0,(struct sockaddr *)&adrc,&l);
if(k==-1)bail("recvfrom()");
printf("from %s port %u: \n\t'%s'\n",inet_ntoa(adrc.sin_addr),(unsigned)ntohs(adrc.sin_port),b);
////////////////////////////////////////////////////////////////////
client.c:
j=read(fd,a,(sizeof a -1));
if(j==-1){close(fd);bail("read()");}
if(j==0){close(fd);printf("end of file\n");exit(1);}
else
{
a[j]=0;
printf("read:%s\n",a);
k=sendto(s,a,strlen(a),0,(struct sockaddr *)&adrs,l);
if(k==-1)bail("sendto()");
}
k=recvfrom(s,b,sizeof b,0,(struct sockaddr *)&adrc,&x);
if(k==-1)bail("recvfrom()");
printf("from %s port %u: \n\t'%s'\n",inet_ntoa(adrc.sin_addr),(unsigned)ntohs(adrc.sin_port),b);
///////////////////////////////////////////////////////
执行结果:
client:
read:Unable to han ////////////////////////这应该是正确读入的数据
from 192.168.2.97 port 9000:
'Unable to han.?縐nable to han'/////////////////从主机接收的
server:
read:Unable to han///////////////////////主机读入的正确数据
from 192.168.2.120 port 1024:
'Unable to hanX@Unable to han'///////////////////从客户机接受的
server和client都读的同一个文件。为什么都收到两遍,第二遍还不正确,请教! |
|