2009-10-20 74 views
1

我試圖運行我的服務器程序,但我不斷收到此錯誤:綁定:地址已在使用

錯誤的綁定:地址已在使用

int main(int argc, char *argv[]){ 

     if (argc < 6){ 
      printf("usage: stringSearcher <filename> <stringLength> <searchLength> <nChildren> <nThreads> <blockSize>\n"); 
      exit(0); 
     } 

     char* fileName = argv[1]; 
     int stringLen = atoi(argv[2]); 
     int searchLen = atoi(argv[3]); 
     nChildren = atoi(argv[4]); 
     if (nChildren == 0) 
      nChildren = 1; 
     int nThreads = atoi(argv[5]); 
     int blockSize = atoi(argv[6]); 
     int childNo = 0; 

    //Opens new socket 
     int sockfd; 
     sockfd = socket(AF_INET, SOCK_STREAM, 0); 

     if (sockfd < 0){ 
     perror("ERROR opening socket"); 
     exit(0); 
     } 



    int newsockfd, portno, clilen; 
    struct sockaddr_in serv_addr, cli_addr; 
    int n; 

    portno = 5000; 
    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_addr.s_addr = INADDR_ANY; 
    serv_addr.sin_port = htons(portno); 

    int opt = 1; 
    setsockopt(1, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ 
    perror("ERROR on binding\n"); 
    exit(1); 
    } 

回答

7
setsockopt(1, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 

第一個參數必須是sockfd而不是'1'

0

winsock開發的一個很好的工具是TCPView。 它可以很容易地看到哪個進程正在監聽哪個端口,套接字狀態等。

相關問題