2014-12-08 160 views
0

我想在c中編寫客戶端/服務器程序。 我正在連接拒絕。我無法弄清楚有什麼問題 任何人都可以請幫助我。連接拒絕錯誤

這是服務器

// include directories 
#include "stdlib.h" 
#include "stdio.h" 
#include "string.h" 
#include "unistd.h" 
#include "sys/socket.h" 
#include "sys/types.h" 
#include "netinet/in.h" 
#include "strings.h" 
#include "arpa/inet.h" 
#include "math.h" 

#define PORTNUM 1300 
#define LISTENQ 10 
#define BUFFER 1024 

int main(int argc, char *argv[]) 
{ 
    int i, maxi, maxfd, listenfd, connfd, sockfd, flag, previousfd; 
    int nready, client[FD_SETSIZE]; 
    struct sockaddr_in servaddr,cliaddr; 
    socklen_t clilen; 
    fd_set rset, allset; 
    ssize_t n; 
    struct frame fpart, bufferedPart1,bufferedPart2 ; 
    char recvline[BUFFER], sendline[BUFFER]; 

    /* First call to socket() function */ 
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 
    { 
     perror("Socket Error"); 
     exit(-1); 
    } 
    printf("I ma here"); 
    /* Initialize socket structure */ 
    bzero(&servaddr, sizeof(servaddr)); 
    servaddr.sin_family = AF_INET; 
    servaddr.sin_port = htons(5000); 
    servaddr.sin_addr.s_addr = INADDR_ANY; 

    /* Now bind the host address using bind() call.*/ 
    if((bind(sockfd,(struct sockaddr *)&servaddr, sizeof(struct sockaddr_in)))== -1) 
    { 
     perror("Bind Error"); 
     exit(-1); 
    } 
    printf("I ma here"); 
    /* Now start listening for the clients */ 
    listen(listenfd, LISTENQ); 
    printf("I ma here"); 
    flag = 0; 
    maxfd = listenfd;   /* initialize */ 
    maxi = -1;     /* index into client[] array */ 
    for (i = 0; i < FD_SETSIZE; i++) 
     client[i] = -1;   /* -1 indicates available entry */ 
    FD_ZERO(&allset); 
    FD_SET(listenfd, &allset); 

    for (; ;) { 
     rset = allset;   /* structure assignment */ 
     nready = select(maxfd + 1, &rset, NULL, NULL, NULL); 
     if (FD_ISSET(listenfd, &rset)) { 
      /* new client connection */ 
      clilen = sizeof(cliaddr); 
      printf("I ma here"); 
      connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &clilen); 
      for (i = 0; i < FD_SETSIZE; i++) 
       if (client[i] < 0) { 
        client[i] = connfd; /* save descriptor */ 
        break; 
       } 
      if (i == FD_SETSIZE) 
       perror("too many clients"); 
      FD_SET(connfd, &allset);  /* add new descriptor to set */ 
      if (connfd > maxfd) 
       maxfd = connfd; /* for select */ 
      if (i > maxi) 
       maxi = i;   /* max index in client[] array */ 
      if (--nready <= 0) 
       continue;   /* no more readable descriptors */ 
     } 
     for (i = 0; i <= maxi; i++) { 
      /* check all clients for data */ 
      if ((sockfd = client[i]) < 0) 
       continue; 
      if (FD_ISSET(sockfd, &rset)) { 
       if ((n = read(sockfd, recvline, sizeof(recvline))) == 0) { 
        /* connection closed by client */ 
        close(sockfd); 
        FD_CLR(sockfd, &allset); 
        client[i] = -1; 
       }     } 
       if (--nready <= 0) 
        break;   /* no more readable descriptors */ 
      } 
     } 
    } 
} 

服務器應該能夠聽不止一個客戶端。所以我用select。

+0

你得到的具體錯誤是什麼? – 2014-12-08 03:41:57

+0

NB'listen()'也可以返回-1,'accept()'也是。你沒有檢查。 – EJP 2014-12-08 04:38:31

回答

0

根據man 2 listen,第一個參數應該是應該偵聽連接的套接字的文件描述符。

在您的服務器代碼中,listenfd未初始化。你實際需要的是listen(2),是帶有文件描述符sockfd的套接字。

初始化listenfd作爲sockfd只是打電話listen(2),或sockfd在服務器端取代listenfd,它應該工作了。


另外,看看this question何時在#include語句中使用"..."<...>