2011-08-19 103 views
2

當我嘗試從我的tcp服務器向我的tcp客戶端發送數據時,我總是收到「Bad file descriptor」錯誤。這對套接字來說意味着什麼?我現在已經呆了一段時間了,我不知道我的代碼有什麼問題。它基本上和我前兩天使用的代碼基本相同,代碼工作正常。我希望有人能告訴我什麼是錯誤的文件描述符嘗試通過套接字發送時的常見原因,以及如何檢查/修復它們。任何幫助表示讚賞。我會在下面發佈一些代碼以防萬一。帶有BSD套接字的錯誤文件描述符

/*Waits to connect a client. Returns true if successful*/ 
bool TcpServer::launchServer() { 
int status; 

struct addrinfo hints; 
struct addrinfo *servinfo; //will point to the results 

//store the connecting address and size 
struct sockaddr_storage their_addr; 
socklen_t their_addr_size; 


memset(&hints, 0, sizeof hints); //make sure the struct is empty 
hints.ai_family = AF_INET; //ipv4 
hints.ai_socktype = SOCK_STREAM; //tcp 

//get server info, put into servinfo 
if ((status = getaddrinfo("192.168.2.3", port, &hints, &servinfo)) != 0) { 
    printf("\ngetaddrinfo error: %m", errno); 
    return false; 
} 

//make socket 
fd = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol); 
if (fd < 0) { 
    printf("\nserver socket failure %m", errno); 
    return false; 
} 

//allow reuse of port 
int yes=1; 
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char*) &yes,sizeof(int)) == -1) { 
    perror("setsockopt"); 
    return false; 
} 

//bind 
if(bind (fd, servinfo->ai_addr, servinfo->ai_addrlen) < 0) { 
    printf("\nBind error %m", errno); 
    return false; 
} 

//free up space 
freeaddrinfo(servinfo); 

//listen 
if(listen(fd, 5) < 0) { 
    printf("\nListen error %m", errno); 
    return false; 
} 
their_addr_size = sizeof(their_addr); 


//accept 
comm_fd = accept(fd, (struct sockaddr*)&their_addr, &their_addr_size); 
if(comm_fd < 0) { 
    printf("\nAccept error %m", errno); 
    return false; 
} 

return true; 
} //END LAUNCHSERVER 






void TcpServer::communicate() { 


fd_set read_flags,write_flags; // the flag sets to be used 
struct timeval waitd = {10, 0};   // the max wait time for an event 
int sel;  // holds return value for select(); 
int numRead; //holds return value for read() 
int numSent; //holds return value for send() 
char in[255]; //in buffer 
char out[255]; //out buffer 

//clear buffersz 
memset(&in, 0, 255); 
memset(&out, 0, 255); 


while(!done) { 
    FD_ZERO(&read_flags); 
    FD_ZERO(&write_flags); 
    FD_SET(comm_fd, &read_flags); 
    FD_SET(comm_fd, &write_flags); 
    FD_SET(STDIN_FILENO, &read_flags); 
    FD_SET(STDIN_FILENO, &write_flags); 

    //call select 
    sel = select(comm_fd+1, &read_flags, &write_flags, (fd_set*)0, &waitd); 

    //if an error with select 
    if(sel < 0) 
     continue; 

    //if socket ready for reading 
    if(FD_ISSET(comm_fd, &read_flags)) { 

     //clear set 
     FD_CLR(comm_fd, &read_flags); 

     memset(&in, 0, 255); 

     numRead = recv(comm_fd, in, 255, 0); 
     //if an error, exit 
     if(numRead < 0) { 
      printf("\nError reading %m", errno); 
      myAgent->getRobot()->pauseSensorStream(); 
      done = true; 
     } //end if error 
     //if connection closed, exit 
     else if(numRead == 0) { 
      printf("\nClosing socket"); 
      close(comm_fd); 
      done = true; 
     } //end if connection closed 
     //if message, call getsendback 
     else if(in[0] != '\0') { 
      //std::cout<<"\nClient: "<<in; 
      getSendBack(in); 
     } //end if message 
    } //end if ready for read 


    //if stdin is ready for reading 
    if(FD_ISSET(STDIN_FILENO, &read_flags)) 
     fgets(out, 255, stdin); 


    //if socket ready for writing 
    if(FD_ISSET(comm_fd, &write_flags)) { 

     //printf("\nSocket ready for write"); 
     FD_CLR(comm_fd, &write_flags); 

     //check validity by checking for a digit 
     if(isdigit(out[0])) { 

      //create message to send 
      std::stringstream tosend; 
      tosend<<"@ "<<out; 
      //std::cout<<"\ntosend: "<<tosend.str(); 

      //send 
      //********ERROR HAPPENS HERE PRINTS OUT MESSAGE BELOW****** 
      numSent = send(comm_fd, tosend.str().c_str(), tosend.str().length(), 0); 
     } //end if valid message 
     //if error, exit 
     if(numSent < 0) { 
      printf("\nError sending %m", errno); 
      done = true; 
     } //end if error 
     //wait for message to get there, then clear 
     usleep(5000); 
     memset(&out, 0, 255); 
    } //end if 
} //end while 
} //END COMMUNICATE 

客戶端代碼基本相同。

+0

你能在'send'之前打印'fd'嗎? – cnicutar

+0

我做了並且comm_fd值爲4. – Sterling

+0

您的代碼在錯誤消息之前是否打印「關閉套接字」? – SKi

回答

0

你回答了你自己的問題。如果沒有明確地初始化numSent和numRead,你會得到垃圾,這可能是numSent的負數,如果out []數組中沒有數字,會導致錯誤。

2

當errno是EBADF時,您的程序會打印「錯誤的文件描述符」。 從手冊頁發送:

EBADF =一個無效的描述符被指定。

我很確定socket在send()調用之前是關閉的。 這可能發生,因爲程序可以在「連接關閉」分支後進入「準備寫入」分支。

嘗試以下操作:

else if(numRead == 0) { 
    printf("\nClosing socket"); 
    close(comm_fd); 
    break; 
} 

相反的:

else if(numRead == 0) { 
     printf("\nClosing socket"); 
     close(comm_fd); 
     done = true; 
    } 
+0

我已經在代碼中,按Ctrl f'如果連接關閉' – Sterling

+0

是的,但你的代碼需要「休息」。我更新了我的答案,因此更容易看到提議的更改。 – SKi

+0

這並沒有改變任何東西。爲什麼呢?更清楚的是,錯誤發生在我的程序開始時。我有每0.2秒發送一次的消息,並且在發送第一條消息之後,它給了我錯誤。 – Sterling

相關問題