我面临选择问题。
下面是我接收数据的代码片段(服务器端):
int tcpip_receive_data (int sockfd, char *recv_buffer, int buffer_len, int flags, long sec_timeout)
{
int ret = 0 ;
fd_set fdsock ;
struct timeval timeout = {0x00,} ;
if ((sockfd < 0) || !(recv_buffer) || !(buffer_len))
{
return FAIL ;
}
if (sec_timeout > 0)
{
/* the socket to monitor */
FD_ZERO (&fdsock) ;
FD_SET ((unsigned int) sockfd, &fdsock) ;
/* set a timeout */
timeout.tv_sec = sec_timeout ;
timeout.tv_usec = 0 ;
/* now check whether some data is there or not */
if ((ret = select (sockfd + 1, &fdsock, NULL, NULL, &timeout)) <= 0)
{
printf ("ERROR: Select() retValue[%d] for SockID[%d]%s:%d", ret, sockfd, __FILE__, __LINE__) ;
return FAIL ;
}
else if (ret == 0)
{
printf ("ERROR: Select timed out for SockID[%d]- %s:%d", sockfd, __FILE__, __LINE__) ;
return FAIL ;
}
}
return (recv (sockfd, recv_buffer, buffer_len, flags)) ;
}
下面是我发送数据的代码片段(客户端):
int main ( int argc, char **argv )
{
struct sockaddr_in sock_client;
char ip_address [IP_ADDR_LEN] = {0,};
char filename [MAXSTR] = {0,};
int port = 0;
int sock_fd = 0;
FILE *input_fp = NULL;
FILE *output_fp = NULL;
int len = 0, ret = 0;
char data [ BUF_SIZE ] = {0,};
char recv_buffer [ BUF_SIZE ] = {0,};
struct hostent *host;
printf ("%d\n", argc);
if ( argc != 4 )
{
fprintf ( stdout, "<Usage> ./wsp_client <IPAddress> <Port> <DataFilename>\n" );
return 0;
}
strcpy ( ip_address ,argv[1] );
port = atoi ( argv[2] );
strcpy ( filename, argv[3] );
printf ("%s:%d:%s\n", ip_address, port, filename );
if ( ( input_fp = fopen ( filename, "rb" ) ) == NULL )
{
perror ("Input file fopen:");
return 0;
}
if ( ( output_fp = fopen ( "./read_data", "wb" ) ) == NULL )
{
perror ("Outuput fopen:");
return 0;
}
while ( !feof(input_fp) )
{
if ( ( ret = fread ( data, 1, BUF_SIZE, input_fp ) ) < 0 )
{
printf ("fread failed, ferror=[%d] feof=[%d]", ferror (stdin), feof(input_fp));
break ;
}
len += ret;
}
fwrite ( data, sizeof (char), len, output_fp );
fclose ( input_fp );
fclose ( output_fp );
//create a scoket.
if ( ( sock_fd = socket ( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
{
perror ( "Socket Error:" );
return 0;
}
//form the structure.
host = gethostbyname(ip_address);
sock_client.sin_family = AF_INET;
sock_client.sin_port = htons (port);
sock_client.sin_addr = *((struct in_addr *)host->h_addr);
//connect
printf ( "Connecting to port %d\n", port );
if ( connect ( sock_fd, (struct sockaddr *) &sock_client, sizeof ( sock_client ) ) < 0 )
{
perror ( "connect:" );
return 0;
}
printf ("Connection Established\n");
printf ("Sending data of lenght:%d\n", len);
if ( ( ret = send ( sock_fd, data, len, 0 ) ) < 0 )
{
perror ("send:");
return 0;
}
printf ("Sent data of lenght:%d\n", len);
recv (sock_fd, recv_buffer, BUF_SIZE, 0);
close ( sock_fd );
}
问题是我编写的客户端程序能够发送数据,但服务器无法接收数据。但是使用其他客户端程序(实际上是一个工具)我能够发送数据并且服务器能够接收数据并处理接收到的数据。
从 tcpdump 中,已经建立了连接,并且客户端已经发送了数据,并且服务器已经确认了(请检查下面粘贴的 Wrieshark)。但是服务器给我的日志说发生了超时。
为什么当套接字中存在数据时选择没有唤醒。为什么它会超时。
请帮我解决问题。