2015-05-09 126 views
0

我正在尋找我的問題的答案,雖然沒有任何相關出現。 我正在寫一個非常簡單的代碼,用於在運行ubuntu 14.04的虛擬機(VBOX)上運行服務器。綁定套接字與errno失敗88

我關掉我的防火牆和我的反病毒程序(讀取,這可能是相關的)

我複查(並尋找​​不同的端口),該端口沒有使用,但繼續接收返回值-1與errno 88(在非套接字上的套接字操作)綁定()函數。

我運行在端口7777 服務器也試過我的主機上運行該代碼

有人建議我在做什麼錯?

p.s也用valgrind檢查了內存泄漏代碼,但看起來很好。

的代碼如下:

#include <string.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <netdb.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/in.h> 
#include <iostream> 
#include <fstream> 
#include <strings.h> 
#include <stdlib.h> 
#include <string> 
#include <pthread.h> 
#include <errno.h> 

using namespace std; 

#define NUM_OF_THREADS 3 

static int connFd; 

void *task1(void *); 

/* 
* 
*/ 
int main(int argc, char** argv) { 

    int pid, portNo, listenFd; 
    socklen_t len; // store the size of the address 
    bool loop = false; 
    struct sockaddr_in svrAdd, clntAdd; 

    pthread_t threadArr[NUM_OF_THREADS]; 

    if (argc < 2){ 
     cerr << "ERROR: ./server <port>" << endl; 
     return 0; 
    } 

    portNo = atoi(argv[1]); // TODO verify the port is between 1024 and 65535 

    //Create the socket 
    if (listenFd = socket(AF_INET, SOCK_STREAM, 0) < 0){ 
     cerr << "ERROR: cannot open socket." << endl; 
     return 0; 
    } 

    bzero((char*)&svrAdd, sizeof(svrAdd)); 

    svrAdd.sin_family = AF_INET; 
    svrAdd.sin_addr.s_addr = INADDR_ANY; 
    svrAdd.sin_port = htons(portNo); 

    cout << "port number is : " << portNo << endl; 

    //bind socket 
    int bound = bind(listenFd, (struct sockaddr *)&svrAdd, sizeof(svrAdd)); 
    if (bound < 0){ 
     cerr << "ERROR: cannot bind, error number: " << errno << endl; 
     return 0; 
    } 

    listen(listenFd, 5); 

    len = sizeof(clntAdd); 

    int noTread = 0; 

    while(noTread < 3){ 
     cout << "Listening..." << endl; 

     if (connFd = accept(listenFd, (struct sockaddr*)&clntAdd, &len)<0){ 
      cerr << "ERROR: cannot accept connection" << endl; 
      return 0; 
     } 
     else{ 
      cout << "Connection successful" << endl; 
     } 

     pthread_create(&threadArr[noTread], NULL, task1, NULL); 
     noTread++; 
    } 

    for (int i=0; i < 3; i++){ 
     pthread_join(threadArr[i], NULL); 
    } 
    /*int sockfd, newsockfd, portno, clilen, n; 
    char buffer[256]; 
    struct sockaddr_in serv_addr, cli_addr; 

    if (argc < 2){ 
     fprintf(stderr, "ERROR, no port provided"); 
     exit(1); 
    } 

    if(sockfd = socket(AF_INET, SOCK_STREAM, 0) < 0){ 
     error("ERROR opening socket"); 
    } 

    bzero((char *) &serv_addr, sizeof(serv_addr)); 

    portno = atoi(argv[1]); 
    serv_addr.sin_family = AF_INET; 

    serv_addr.sin_port = htons(portno); 
    serv_addr.sin_addr.s_addr - INADDR_ANY; 

    if(bind(sockfd, (struct sockaddr*)&))*/ 

    return 0; 
} 

void *task1(void *dummyPt){ 
    cout << "Thread number: " << pthread_self() << endl; 
    char test[300]; 
    bzero(test, 301); 
    bool loop = false; 
    while (!false){ 
     bzero(test, 301); 
     read(connFd, test, 300); 
     string tester(test); 
     cout << "\n\t\t TESTER = " << tester << endl; 

     if(tester == "exit"){ 
      break; 
     } 
    } 
    cout << "\n Closing thread and conn" << endl; 
    close(connFd); 
} 

執行的輸出:

ERROR:不能結合,錯誤的數字:總7777

RUN成功時(88 端口號是時間:162ms)

請幫忙, 謝謝。

回答

1
if (listenFd = socket(AF_INET, SOCK_STREAM, 0) < 0){ 

優先問題。此條件的結果是將0或1分配給listenFd.試試這個:

if ((listenFd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ 
+0

謝謝!現在工作的人應該親眼看到它 – qazwaz