2013-08-21 87 views
1

服務器程序:運行時綁定錯誤

#include<sys/types.h> 
    #include<sys/socket.h> 
    #include<netinet/in.h> 
    #include<arpa/inet.h> 
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
    #include<unistd.h> 
    #define MYPORT 4444 
    #define BACKLOG 100 
    #define MAXBUFSIZE 100 

    int main(void) 
    { 
     int sockfd,newfd,sin_size,i,count; 
     struct sockaddr_in my_addr,their_addr; 
     char request[MAXBUFSIZE] = "This is the servers request"; 
     char buf[100]; 

     /*Create Socket */ 
     sockfd = socket(AF_INET, SOCK_STREAM, 0); 
     printf("\nSocket created"); 

     my_addr.sin_family = AF_INET; 
     my_addr.sin_port = htons(MYPORT); 
     my_addr.sin_addr.s_addr =inet_addr("10.228.37.9"); 

     /*Bind Socket*/ 
     i=bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)); 
     if(i<0) 
     { 
      printf("\nBind Error"); 
      exit(1); 
     } 
     else 
     { 
      printf("\nBind socket"); 
     } 

     /*Listen */ 
     listen(sockfd, BACKLOG) ; 
     if(listen(sockfd, BACKLOG)==-1) 
     { 
      printf("\nError in listening"); 
     } 
     else 
      printf("Listened Successfully"); 

     sin_size = sizeof(struct sockaddr_in); 

     /*Accept*/ 
     newfd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size); 

     /*Receive from Client*/ 
     count = recv(newfd, buf, sizeof(buf), 0); 
     if (count < 0) 
     { 
      printf("recv error"); 
      exit(1); 
     } 
     buf[count]='\0'; 
     printf("\nReceived: %s",buf); 
     printf("\nSuccessful"); 

     /*Send to Client*/ 
     if (send(newfd, request, (int)strlen(request), 0) < 0) 
     { 
      printf("send error"); 
      exit(1); 
     } 

     return 0; 
    } 

這是使用套接字在C服務器程序, 沒有編譯錯誤,但在運行時程序應等待客戶端但它顯示綁定錯誤。在socket服務器程序 運行時綁定錯誤

+1

提示修復「地址已在使用」 設置標誌SO_REUSEADDR :'strerror(errno)'。 – trojanfoe

+0

你爲什麼試圖綁定到特定的IP地址?通常我們綁定到INADDR_ANY以接受所有連接... – Medinoc

+2

@Medinoc雖然綁定到特定的IP地址沒有任何問題。我的錢在'EADDRINUSE'上。 – trojanfoe

回答

0

通過調用setsocketopt

這只是一個小的示範瞭如何重用端口

#include <stdio.h> 
#include <string.h> 
#include <errno.h> 
#include <stdlib.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <unistd.h> 
#include <signal.h> 
int main(){ 

//create a new socket stream 
int sock_fd = socket(PF_INET, SOCK_STREAM, 0); 
if (sock_fd == -1){ 
    printf("Socket Error %s\n", strerror(errno)); 
} 
int reuse; 
if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(int)) == -1){ 
printf("Reuse port Error : %s\n", strerror(errno)); 
} 
// configure the socket 
struct sockaddr_in sock_addr; 
//bind to port 4500 
sock_addr.sin_port = (in_port_t) htons(4500); 
sock_addr.sin_family = PF_INET; 
sock_addr.sin_addr.s_addr = htonl(INADDR_ANY); 
// bind the socket to given port 
if (bind(sock_fd, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) == -1){ 
    printf("Bind Error %s\n", strerror(errno)); 
} 

//listner to only 2 clients 
listen(sock_fd, 2); 
// keep the connection running 
while(1){ 

struct sockaddr_storage client_addr; 

    // appect a connection from client 
    unsigned int addr_size = sizeof(client_addr); 
    int conn_fd = appect(sock_fd, (struct sockaddr *) &client_addr, &addr_size); 

    char *msg = "Hello World"; 

    send(conn_fd, msg, strlen(msg), 0); 
    close(conn_fd); 
} 
} 
+1

仍然沒有'strerror(errno)': -/ – trojanfoe

+0

你能證明你的答案 – Shushant

+0

是的,我可以。 OP沒有線索*爲什麼* bind()'失敗,直到他添加了'strerror(errno)',並且你用你提供的代碼繼續這個問題。 – trojanfoe