我需要 C 中的套接字编程方面的帮助。我需要在 C 中编写一个服务器-客户端程序,其中两个 IP 在创建的套接字上相互通信。我成功地做到了这一点。但现在我想在套接字中添加计时器。我研究了一下,发现可以通过 select() 来实现,其中我们给出了超时值。我试过这样做,但徒劳无功。在下面显示的代码中,我尝试连接到客户端并读取创建了 10 秒的新套接字。如果客户端在这 10 秒内发送了一些消息,服务器应该接收该消息并发送回确认。如果不是,则必须发出一条错误消息,说明它已超时。
我的服务器代码如下:
#include<stdio.h>
#include<unistd.h>
#include<sys/time.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int a=1;
fd_set readfds,writefds;
int ready_for_reading,reading;
struct timeval time_out;
FD_ZERO(&readfds);
// FD_ZERO(&writefds);
FD_SET(a,&readfds);
time_out.tv_sec = 10; // 10 seconds
time_out.tv_usec = 0; // 0 milliseconds
// ready_for_reading=select(1,&readfds,NULL,NULL,&time_out);
int sockfd, newsockfd, portno,n,clilen;;
struct sockaddr_in serv_addr,cli_addr;
char buffer[256];
portno=atoi(argv[1]);
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
error("\nError creating socket");
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(portno);
serv_addr.sin_addr.s_addr=INADDR_ANY;
n=bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
if(n<0)
error("\nError binding socket");
listen(sockfd,5);
clilen=sizeof(cli_addr);
printf("\nAccept call reached");
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
printf("\nnewSockfd=%d",newsockfd);
printf("\ntesting value:%d",sizeof(readfds));
if(newsockfd==-1)
error("\nError on accept");
// FD_SET(a,&readfds);
ready_for_reading=select(sizeof(readfds)*8,&readfds,NULL,NULL,&time_out);
printf("\nready_for_reading=%d",ready_for_reading);
if (ready_for_reading == -1)
{
/* Some error has occured in input */
printf("Unable to read your input\n");
return -1;
}
else
{
if (ready_for_reading)
{
reading = read(newsockfd, buffer, 255);
printf("Read, %d bytes from input : %s \n", reading,buffer);
}
else
{
printf(" 10 Seconds are over - no data input \n");
return 0;
}
}
/* n=read(newsockfd,buffer,255);
if(n<0)
error("\nError in reading client's msg");
*/
printf("\nClient's msg is:%s\n",buffer);
n=write(newsockfd,"I got your message",25);
if(n<0)
error("\nError on writing");
return 0;
}
select() 函数始终返回“0”。我需要这方面的帮助。在此先感谢,理查德。