簡體   English   中英

C中的套接字:read()返回> 0字節讀取,但緩沖區為零

[英]Sockets in C: read() returns > 0 bytes read, but buffer is zero

我正在嘗試從C中的套接字讀取數據。將出現兩條消息。

我正在嘗試將消息的大小發送到套接字,以便read()命令將知道消息何時結束並且可以在此時停止讀取。 否則,它將把這兩個消息讀入一個blob。 問題是即使讀取> 0個字節,read命令也會將緩沖區設置為0。

這是代碼:

n = read(newsockfd, buffer, sizeof(int)); 
     int bytes_to_read = atoi(buffer); 
     fprintf(stdout, "Here is the size of the data to look for: %d", bytes_to_read);
     /*read message*/
     fprintf(stdout, "Reading first message.\n");
     int bytesread = 0;
     int chunk = bytes_to_read > 255 ? 255 : bytes_to_read;  
     do {
        bzero(buffer,256);
        n = read(newsockfd,buffer,chunk);
        bytes_read += n; 
        fprintf(stdout, "The value of n is %d and here is the new buffer after the first read: %s and new buffer length is : %d\n", n, buffer, strlen(buffer));
        strcat(plaintext, buffer); 
        size += 256;
        plaintext = realloc(plaintext, size); 
     } while (bytes_read < bytes_to_read);

我得到的輸出顯示n = 36(按預期),但是buffer為0,buffer的長度為1。

為什么將read()命令將緩沖區設置為0? 有沒有更簡單的方法告訴read()在哪里停止讀取?

編輯:根據我得到的反饋,我徹底重寫了這一點。 請注意,我知道客戶端將發送一個包含數據中字節數的數據包作為unit32_t,然后它將發送數據。

char * readText(int newsockfd) {
char * text = malloc(256); 
char buffer[256];
int n; 
int size = 256;
/*find out how many bytes are in the data*/
 uint32_t bytes_to_read; 
 n = read(newsockfd, &bytes_to_read, sizeof(uint32_t)); 
 bytes_to_read = ntohl(bytes_to_read); 

 /*read data from client*/
 int bytes_read = 0;
 int bytes_left = bytes_to_read; 
 /*the maximum we can read at one time is 255 bytes since that is the size of the buffer. 
 * the buffer could have been larger but we'd still have to check the data size and keep calling
 * read() until we get all of the data. 
 * If the data to read is smaller than the buffer, then we just read it in one go. If the data is larger, 
 * then we read it in 255 byte-sized chunks until we have read all of it*/ 
 int chunk = (bytes_to_read > 255) ? 255 : bytes_to_read;
 do {
    bzero(buffer,256);
    n = read(newsockfd,buffer,chunk);
    /*copy over the data we have read so far into the text buffer
    We offset by the amount we have already read in so we do not overwrite the data already 
    in the array*/
    memcpy(text + bytes_read, buffer, n);
    bytes_read += n; 
    bytes_left -= n; 
    /*if we have less than 255 bytes left, just read that amount. This prevents read() from pulling in too much data and messing up the next read() call*/ 
chunk = (bytes_left > 255) ? 255 : bytes_left;
    size += n;
    /*grow the text variable by as much as we just read in. This makes room for 
    the next chunk of data. The next chunk could be as large as 255 bytes*/ 
    text = realloc(text, size); 
 } while (bytes_read < bytes_to_read); 
 text[bytes_read] = '\0'; //null terminate our string so we can use string functions on it. 
 if (n < 0) error("ERROR reading from socket");
return text; }`
  1. 無法保證您會收到完整的長度信息。

  2. 您正在讀取它的sizeof int字節,它表示二進制數據,但是隨后您要調用atoi() ,它以十進制字符串的形式建議以空值終止的ASCII數據。 哪有

  3. 如果返回值為36,則表示已接收到多少字節,如果緩沖區包含{0, 1} ,也將接收到該值。

  4. 僅在任意字節數組上使用strlen()strcat()完全無效。 這些函數適用於以null終止的字符串。 關於TCP的所有內容,沒有什么能說您收到的每個緩沖區都是一個以null終止的字符串,也沒有任何內容可以說是可以打印的。

您所做的根本無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM