У меня есть этот клиент-серверный код, который использует класс пакета для сериализации данных, отправки их на сервер и десериализации. У меня есть это, чтобы напечатать 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);