Программирование сокетов на языке Си
Я работаю над программированием сокетов и сталкиваюсь с проблемой тайм аута используемого перед чтением() Он всегда идет к условию ret=0 и не идет вперед, хотя клиент дает данные
Это файл server.c
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> #include <unistd.h> #include <netinet/in.h> #include <string.h> #include <signal.h> #include <time.h> #include <sys/select.h> #include <sys/fcntl.h> #include <sys/time.h> #define BACKLOG 3 void error(const char *ptr); /*Global Variables*/ int sockfd = 0, newsockfd = 0; int ret = 0; void call(char *ptr) { FILE *fp; char arr[100] = {'\0'}; fp = popen(ptr, "r"); while (1) { fread(arr, sizeof(arr), 1, fp); write(newsockfd, arr, sizeof(arr)); if (feof(fp) == 1) { sleep(1); //used to change priority /*If i am not using sleep() than the client goes into blocking and waiting for server*/ write(newsockfd, "quit", sizeof("quit")); break; } } pclose(fp); } void _close() { printf("closing the connection"); ret = close(newsockfd); if (ret < 0) error("close"); ret = close(sockfd); if (ret < 0) error("close"); exit(0); } void handler(int _catch) { printf("received signal of %d\n", _catch); _close(); } int main(int argc, char *argv[]) { int port = 0; int socklen = 0; int i = 0, j = 0; int ret1 = 0; char buff[255]; char cmp_str[] = {"quit"}; char string[100] = {"\0"}; char *retptr = NULL; struct sockaddr_in server_addr, client_addr; clock_t start, end; if (argc < 2) { printf("Please give the port address\n"); exit(1); } socklen = sizeof(client_addr); /*Taking port address*/ port = atoi(argv[1]); /**Creating a socket**/ sockfd = socket(PF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("socket"); /** sockaddr**/ server_addr.sin_family = AF_INET; /* Internet address family for IPv4*/ server_addr.sin_addr.s_addr = (htonl)INADDR_ANY; /*Any incoming interface*/ server_addr.sin_port = htons(port); /*Local port*/ /**Bind the socket**/ ret = bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); if (ret < 0) error("bind"); /**listen**/ ret = listen(sockfd, BACKLOG); if (ret < 0) error("listen"); /**accept function**/ newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &socklen); if (newsockfd < 0) error("accept"); /*Signal Handling*/ signal(SIGINT, handler); /*Communication*/ fcntl(newsockfd, F_SETFL, O_NONBLOCK); // set to non-blocking fcntl(newsockfd, F_SETFL, O_ASYNC); // set to asynchronous I/O system("clear"); /*read*/ bzero(buff, sizeof(buff)); ret = read(newsockfd, (void *)buff, sizeof(buff)); /*ret1 = select(BACKLOG, &readfds, NULL, NULL, &timeout); if (ret == 0) { printf("time out\n"); _close(); }*/ if (ret < 0) perror("read"); printf("Client :- %s", buff); call("ls | grep txt"); /*Read the query*/ bzero(buff, sizeof(buff)); int opt = 3; setsockopt(newsockfd, SOL_SOCKET, SO_RCVLOWAT, &opt, sizeof(opt)); /*For Time out connection*/ fd_set readfds; FD_ZERO(&readfds); FD_SET(0, &readfds); struct timeval timeout; timeout.tv_sec = 10; //seconds// timeout.tv_usec = 0; //micro seconds// ret=select(1, &readfds, NULL, NULL, &timeout); if (ret==0) { printf("time out\n"); _close(); } else if(ret>0) { ret = read(newsockfd, (void *)buff, sizeof(buff)); if (ret < 0) perror("read"); if (ret1 == 0) { printf("time out\n"); _close(); } printf("Client :- %s", buff); /*transfering the file*/ strncpy(string, "cat", strlen("cat")); for (i = 0; string[i] != '\0'; i++) ; string[i] = ' '; i++; for (i, j = 0; buff[j] != '\0'; j++, i++) { string[i] = buff[j]; } call(string); _close(); } } void error(const char *ptr) { perror(ptr); exit(1); }
Что я уже пробовал:
Искал и читал статьи, но никакого решения не получал