2015-05-26 196 views
0

我有一個非常簡單的客戶端和服務器代碼:客戶端無法與服務器C連接(Socket編程)

客戶

int sockfd = 0; 
    int bytesReceived = 0; 
    char recvBuff[BUFSIZE]; 
    memset(recvBuff, '0', sizeof(recvBuff)); 
    struct sockaddr_in serv_addr; 
    printf("here i am\n"); 
    /* Create a socket first */ 
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0){ 
     printf("\n Error : Could not create socket \n"); 
     return 1; 
    } 
    fprintf(stderr, "connection stats\n"); 
    fprintf(stderr, "connecting to %d - %s \n",port,server); 
    /* Initialize sockaddr_in data structure */ 
    serv_addr.sin_family = AF_INET; 
    serv_addr.sin_port = htons(port); // port 
    serv_addr.sin_addr.s_addr = server; 
    printf(" attempting connection %s:%d\n", server, port); 
    /* Attempt a connection */ 
    int con = connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)); 
    printf("Error %d\n", con); 
    exit(-1); 

和服務器

int listenfd = 0; 
    int connfd = 0; 
    struct sockaddr_in serv_addr; 
    char sendBuff[BUFSIZE]; 
    int numrv; 

    listenfd = socket(AF_INET, SOCK_STREAM, 0); 

    printf("Socket retrieve success\n"); 

    memset(&serv_addr, '0', sizeof(serv_addr)); 
    memset(sendBuff, '0', sizeof(sendBuff)); 
    printf("here 2\n"); 
    serv_addr.sin_family = AF_INET; 
    printf("here 3\n"); 
    serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 
    printf("here 4\n"); 
    serv_addr.sin_port = htons(port); 
    printf("here 5\n"); 
    int optval = 1; 
    printf(" opening port %d\n", port); 
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, 
       (const void *) &optval, sizeof(int)); 
    bind(listenfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); 
    printf("binded\n"); 


    while (1) { 
     connfd = accept(listenfd, (struct sockaddr *) NULL, NULL); 
     printf("connfd %d", connfd); 


     close(connfd); 
     sleep(1); 


    } 

客戶端輸出:

here i am 
connecting to 8888 - localhost 
attempting connection localhost:8888 

服務器輸出

Socket retrieve success 
here 2 
here 3 
here 4 
here 5 
opening port 8888 
binded 

,等待像幾分鐘後..我注意以下事項: 客戶:

here i am 
connecting to 8888 - localhost 
attempting connection localhost:8888 
Error -1 

服務器:

Socket retrieve success 
here 2 
here 3 
here 4 
here 5 
opening port 8888 
binded 
connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd -1connfd 

其中由-1,我可以猜測連接失敗。 我不知道我失蹤了什麼?

+0

你應該後'connfd%D',要不然就把一個'\ N' printf的輸出將被緩衝,當它發生時你不會看到它。你也可以調用'fflush'來打印緩衝輸出 – Eregrith

+0

@Eregrith:你是對的..所以基本上,連接失敗瞬間..但我不知道..爲什麼服務器不接受客戶端連接.. – Fraz

+0

看看'errno'和'perror'也許可以幫助,將它與[connect()手冊](http://linux.die.net/man/2/connect) – Eregrith

回答

2

在呼叫accept()之前,服務器錯過了在綁定套接字上調用listen()


而且這個

serv_addr.sin_addr.s_addr = server; 

應該發出編譯器警告。你不能指定一個C-字符串(我認爲server是)到一個in_addr_tserv_addr.sin_addr.s_addr是什麼)。

修復此做:

serv_addr.sin_addr.s_addr = inet_addr(server); 

或更好的錯誤檢查,這一點,像這樣:

{ 
    in_addr_t in_addr = inet_addr(server); 
    if (INADDR_NONE == in_addr) 
    { 
    perror("inet_addr() failed"); 
    abort(); /* or whatever error handling you choose. */ 
    } 

    serv_addr.sin_addr.s_addr = in_addr; 
} 
+0

葉普..那沒有解決..所以現在服務器正在等待傳入連接..但客戶端無法連接到它..我錯過了我的客戶端代碼上的任何東西? – Fraz

+0

好吧..所以我能夠做到這一點..而不是本地主機,如果我有serv_addr.sin_addr.s_addr = inet_addr(「127.0.0.1」);那它工作正常嗎?但我其實有字符串本地主機?有沒有辦法來初始化localhost..indows 127.0.0.1 – Fraz

+0

我得到PING本地主機(127.0.0.1)56(84)字節的數據。 從本地主機(127.0.0.1)64字節:icmp_seq = 1 ttl = 64時間= 0.011毫秒 本地主機(127.0.0.1)64字節:icmp_seq = 2 ttl = 64時間= 0.018 ms 但是,當我使用localhost 127.0.0.1,客戶端連接失敗(返回-1) – Fraz

相關問題