2015-11-08 158 views
-4

我在C中做了一個清楚的循環,應該從0到3。當我運行它時,它會進入無限,它永遠不會停止。我甚至打印櫃檯,並繼續進行。C for循環不會停止

uint32_t len1, nr; --- making TCP client/server 
len1 = 3; 
for (i =0; i < len1; i++) { 
     printf("first array number \n"); 
     scanf("%hu", &nr); 
     printf("got %hu %hu %hu \n", nr, i, len1); 
     nr = htonl(nr); 
     send(c, &nr, sizeof(nr), 0); 
    } 

我的結果:

​​

等等......爲什麼它不停止?

完整代碼:

客戶:

#include <sys/types.h> 
#include <sys/socket.h> 
#include <netinet/ip.h> 
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <inttypes.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]) { 
    uint32_t len1, nr; 
    int i; 
    int *l1; 
    int c= socket(AF_INET, SOCK_STREAM, 0); 
    if (c < 0) { 
     printf("Error creating the socket \n"); 
     return 1; 
    } 

    struct sockaddr_in server_addr; 
    memset(&server_addr, 0, sizeof(server_addr)); 
    server_addr.sin_port = htons(9999); 
    server_addr.sin_addr.s_addr = INADDR_ANY; 
    server_addr.sin_family = AF_INET; 

    if (connect(c, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { 
     printf("Error connecting to the server \n"); 
     return 1; 

    } 
    printf("Length1 = \n"); 
    scanf("%hu", &len1); 
    len1 = htonl(len1); 
    send(c, &len1, sizeof(len1), 0); 
    len1 = ntohl(len1); 
    printf("got %hu \n", len1); 
    for (i =0; i < len1; i++) { 
     printf("first array number \n"); 
     scanf("%" SCNd32, &nr); 
     printf("got %hu %hu %hu \n", nr, i, len1); 
     nr = htonl(nr); 
     send(c, &nr, sizeof(nr), 0); 
    } 

    free(l1); 
    close(c); 
    return 0; 

} 

服務器:

#include <sys/types.h> 
#include <sys/socket.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <netinet/in.h> 
#include <netinet/ip.h> 
#include <arpa/inet.h>   
#include <string.h> 
#include <pthread.h> 

pthread_t th[5]; 
int i; 

void* c_thread(void *arg) { 
    int *c = (int*)arg; 
    int i; 
    int *l1; 
    uint32_t len1, nr; 

    recv(*c, &len1, sizeof(len1), MSG_WAITALL); 
    len1 = ntohl(len1); 
    printf("got %hu \n", len1); 
    l1 = malloc(len1*sizeof(int)); 
    for (i = 0; i < len1; i++) { 
     recv(*c, &nr, sizeof(nr), MSG_WAITALL); 
     nr = ntohl(nr); 
    printf("got %hu %hu \n", nr, i);  
     *(l1 + i) = nr; 
    } 

    close(*c); 
    free(l1); 

} 

int main (int argc, char *argv) { 
    int yes = 1; 
    struct sockaddr_in server_addr, client_addr; 
    int s = socket(AF_INET, SOCK_STREAM, 0); 
    if (s < 0){ 
     printf("Error creating the socket \n"); 
     return 1; 
    } 

    setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); 
    printf("Socket created \n"); 

    memset(&server_addr, 0, sizeof(server_addr)); 
    server_addr.sin_port = htons(9999); 
    server_addr.sin_family = AF_INET; 
    server_addr.sin_addr.s_addr =INADDR_ANY; 

    if (bind(s, (struct sockaddr*) &server_addr, sizeof(server_addr))< 0) { 
     printf("Error binding in the server \n"); 
     return 1; 
    } 

    listen(s,10); 
    printf("Server is listening \n"); 
    int l =sizeof(client_addr); 
    memset(&client_addr, 0 ,sizeof(client_addr)); 
    printf("Server is running \n"); 
    i = 0; 
    while(1){ 
     int c = accept(s,(struct sockaddr*)&client_addr, &l); 
     if (c < 0) { 
      printf("Error connecting to the client \n"); 
      return 1; 
     } 
     printf("COnnectedto the client \n"); 
     int*argum = malloc(sizeof(int)); 
     *argum=c; 
     pthread_create(&th[i],NULL,c_thread, argum); 
     i++; 
    } 
    return 0; 
} 

試圖把一個while循環,而不是爲。還是行不通。我不會與len1比較。它通過條件4 < 3.我不知道該怎麼辦

+3

如何所有這些變量定義的?請發佈*完整*代碼。 – alk

+0

併發布確切的代碼。你的代碼(基於輸出)似乎沒問題。 – deepmax

+2

是否所有變量都按照您的格式建議的那樣真正定義爲'unsigned short int'? –

回答

1

uint32_t len1; 

    ... 

    scanf("%hu", &len1); 

肯定激起了臭名昭著的未定義的行爲。之後會發生任何事情!修復這是

​​

而且這個

scanf("%" SCNd32, &nr); 

仍然是錯誤的,因爲nr是無符號。

應該

scanf("%" SCNu32, &nr); 

在服務器這個

int l =sizeof(client_addr); 

應該

socklen_t l =sizeof(client_addr);