C for循环不会停止

时间:2015-11-08 15:13:04

标签: c loops for-loop

我在C中做了一个明确的循环,它应该从0到3。当我运行它时,它会变为无穷大,它永远不会停止。我甚至打印柜台,它一直在继续。

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);
    }

我的结果:

first array number 
1
got 1 0 3 
first array number 
2
got 2 1 3 
first array number 
3
got 3 2 3 
first array number 
4
got 4 3 3 
first array number 
5
got 5 4 3 
first array number 

等等......为什么不停止?

完整代码:

客户端:

#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循环而不是for。还是行不通。我不会与len1比较。它通过条件4&lt; 3.我不知道该怎么做

1 个答案:

答案 0 :(得分:1)

这个

  uint32_t len1; 

  ...

  scanf("%hu", &len1);

肯定会引发臭名昭着的未定义行为。事后可能发生任何事情!修复此问题

  scanf("%u", &len1);

也是这个

  scanf("%" SCNd32,  &nr);

仍然是错误的,因为nr是无符号的。

应该是

  scanf("%" SCNu32,  &nr);

在服务器中

  int l =sizeof(client_addr);

应该是

  socklen_t l =sizeof(client_addr);