当前位置: 技术问答>linux和unix
socket编程,服务器端收到数据后,立即向客户端发送数据,但客户端收不到?代码贴出,帮我分析一下。
来源: 互联网 发布时间:2015-11-16
本文导语: socket编程,服务器端收到数据后,立即向客户端发送数据,但客户端收不到?代码贴出,帮我分析一下。 这是服装器端代码: FD_ZERO (&fdR0); FD_SET (sockfd, &fdR0); while(socketstat!=SOCK_ERR) { switch(socketstat) { case SOCK_RECV: ...
socket编程,服务器端收到数据后,立即向客户端发送数据,但客户端收不到?代码贴出,帮我分析一下。
这是服装器端代码:
FD_ZERO (&fdR0);
FD_SET (sockfd, &fdR0);
while(socketstat!=SOCK_ERR)
{
switch(socketstat)
{
case SOCK_RECV:
fdR1 = fdR0;
timeout.tv_sec = 10;
timeout.tv_usec = 500000;
switch (select (FD_SETSIZE, &fdR1, NULL, NULL, &timeout))
{
case 0: //timeout
printf ("select time out!n");
socketstat = SOCK_ERR;
break;
case -1: //error
printf ("select error!n");
socketstat = SOCK_ERR;
break;
default:
if (FD_ISSET (sockfd, &fdR1))
{
memset(recvbuffer,0x00,sizeof(recvbuffer));
if ((len =recv (sockfd, recvbuffer,1024,0)) == -1)
{
fprintf (stderr, "Read Error:%sn", strerror(errno));
socketstat = SOCK_ERR;
}
//printf("len=%d",len);
if (len > 0)
{
len=send(sockfd,"abc",3,0);
printf("%s , recvlen=%dn ",recvbuffer,len);
socketstat = SOCK_SEND;
}
else
{
socketstat = SOCK_ERR;
}
}
}
break;
case SOCK_SEND:
memset(readbuffer,0x00,sizeof(readbuffer));
getimage(openfile,readbuffer,&buffersize);
printf("ok %dn",buffersize);
sendlen=0;
do{
len=send(sockfd,readbuffer+sendlen,buffersize-sendlen,0);
//len=send(sockfd,"abc",3,0);
if(len
这是服装器端代码:
FD_ZERO (&fdR0);
FD_SET (sockfd, &fdR0);
while(socketstat!=SOCK_ERR)
{
switch(socketstat)
{
case SOCK_RECV:
fdR1 = fdR0;
timeout.tv_sec = 10;
timeout.tv_usec = 500000;
switch (select (FD_SETSIZE, &fdR1, NULL, NULL, &timeout))
{
case 0: //timeout
printf ("select time out!n");
socketstat = SOCK_ERR;
break;
case -1: //error
printf ("select error!n");
socketstat = SOCK_ERR;
break;
default:
if (FD_ISSET (sockfd, &fdR1))
{
memset(recvbuffer,0x00,sizeof(recvbuffer));
if ((len =recv (sockfd, recvbuffer,1024,0)) == -1)
{
fprintf (stderr, "Read Error:%sn", strerror(errno));
socketstat = SOCK_ERR;
}
//printf("len=%d",len);
if (len > 0)
{
len=send(sockfd,"abc",3,0);
printf("%s , recvlen=%dn ",recvbuffer,len);
socketstat = SOCK_SEND;
}
else
{
socketstat = SOCK_ERR;
}
}
}
break;
case SOCK_SEND:
memset(readbuffer,0x00,sizeof(readbuffer));
getimage(openfile,readbuffer,&buffersize);
printf("ok %dn",buffersize);
sendlen=0;
do{
len=send(sockfd,readbuffer+sendlen,buffersize-sendlen,0);
//len=send(sockfd,"abc",3,0);
if(len