簡體   English   中英

我將如何遍歷我的數組(緩沖區——包含一個文本文件),並以 30 個字節的塊打印出來?

[英]How would I loop through my array (buffer — contains a text file) , and print it out in chunks of 30 bytes?

我有這個客戶端/服務器代碼,它使用數據包 class 來序列化數據,將其發送到服務器並反序列化它。 我已經讓它打印出 30 個字節的緩沖區,但我如何讓它連續打印出 30 個字節,直到它到達文件末尾? 請原諒我對 C++ 和套接字編程的了解,因為它們都很苗條。 客戶

#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fstream>
#include <string>
#include "packet.cpp"


#define PORT 8080
#define MAXLINE 30



int main(){

//UDP
char buffer[MAXLINE];

// Read from text file and add to buffer
FILE *fp = fopen("file.txt", "r");
if ( fp != NULL ) 
{   
    size_t newLen = fread(buffer, sizeof(char), MAXLINE, fp);
    fclose(fp);
}


// Loop through buffer and print contents using packet class
for (int i = 0; i < 30; i++)
{
    cout << "------------------------------------\n";
    int t = 1;
    int s = 0;
    int l = 30;
    packet packet(t, s, l, buffer);
    int type = packet.getType();
    int seqnum = packet.getSeqNum();
    int length = packet.getLength();
    char *spacket = packet.getData();
    packet.printContents();
}



//Create the Socket
int clientSocket = socket(AF_INET, SOCK_DGRAM, 0);

//Check for Errors
if (clientSocket < 0){
    perror("socket creation failed");
    exit(EXIT_FAILURE);
}

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

socklen_t server_len = sizeof(server_addr);
sendto(clientSocket, (const char *)buffer, strlen(buffer), MSG_CONFIRM, 
    (const struct sockaddr *)&server_addr, sizeof(server_addr));

int n = recvfrom(clientSocket, (char *)buffer, MAXLINE, MSG_WAITALL, 
    (struct sockaddr *)&server_addr, &server_len);
buffer[n] = '\0';


//Close the Socket
close(clientSocket);

return 0;
}

服務器

    #include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "packet.cpp"

#define PORT 8080
#define MAXLINE 30

//int argc, char *argv[]



int main(){
//UDP 
char buffer[MAXLINE];


//Create a socket 
int serverSocket = socket(AF_INET, SOCK_DGRAM, 0);


//Error Message 
if (serverSocket < 0){
    perror("socket creation failed");
    exit(EXIT_FAILURE);
}

struct sockaddr_in server_addr, client_addr;
memset((char *) &server_addr, 0, sizeof(server_addr));
memset((char *) &client_addr, 0, sizeof(client_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

int bindTogether = bind(serverSocket, (const struct sockaddr *) &server_addr, sizeof(server_addr));

if (bindTogether < 0){
    perror("bind failed");
    exit(EXIT_FAILURE);
}

socklen_t client_len = sizeof(client_addr);
int n = recvfrom(serverSocket, (char *)buffer, MAXLINE, MSG_WAITALL, 
    (struct sockaddr * ) &client_addr, &client_len);

buffer[n] = '\0';

sendto(serverSocket, (const char *)buffer, strlen(buffer), MSG_CONFIRM,
 (const struct sockaddr *) &client_addr,client_len);


//Close the Socket 
close(serverSocket);

return 0;

}

    #include "packet.h"
#include <stdlib.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <stdio.h>

using namespace std;  // using standard namespace

// constructor for packet class
// d points to memory already allocated prior to this call OR ELSE should be set to NULL
packet::packet(int t, int s, int l, char * d){
    type = t;
    seqnum = s;
    length = l;
    data = d;
}

// returns the type of packet
int packet::getType(){
    return type;
}

// returns the sequence number
int packet::getSeqNum(){
    return seqnum;   
}

// returns the length
int packet::getLength(){
    return length;   
}

// returns pointer to data
char * packet::getData(){
    return data;   
}

// print function for testing purposes
void packet::printContents(){
    cout << "type: " << type << "  seqnum: " << seqnum << " length: " << length << endl;
    if(data != NULL)
        cout << "data: " << data << endl << endl;
    else
        cout << "data: null" << endl << endl; 
}

// This function serializes the data such that type, seqnum, length, and data values are placed 
// in a char array, spacket, and separated by a single space; that is, spacket contains the serialized data
void packet::serialize(char * spacket){
    sprintf (spacket, "%d %d %d %s", type, seqnum, length, data);   
}

// This function deserializes a char array, spacket, which is the result of a call to serialize (above)
// Warning: Will fail horribly if spacket does not have the correct format as provided by serialize()
// Warning: If length value is non-zero, then the data array must be instantiated or this will fault
void packet::deserialize(char * spacket){
    char * itr;
    itr = strtok(spacket," ");
    char * null_end;

    this->type = strtol(itr, &null_end, 10);

    itr = strtok(NULL, " ");
    this->seqnum = strtol (itr, &null_end, 10);

    itr = strtok(NULL, " ");
    this->length = strtol (itr, &null_end, 10);

您可以使用%運算符。 這是一個例子:

for (int i = 0; i < 300; ++i)
{
    if ((i % 30) == 0)
    {
       std::cout << "\n";
    }
    std::cout << i << " ";
}
std::cout << std::endl;

每 30 個數字發送換行符。

暫無
暫無

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

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