tcp nes wip
This commit is contained in:
parent
3695a54e9a
commit
caf1ae0b7d
|
|
@ -132,20 +132,25 @@ std::string TcpNet::client(const std::string &address, int portno, const std::st
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
std::string TcpNet::read_from_socket(int sockfd) {
|
||||
char buffer[TCPNET_BUFFER_SIZE];
|
||||
std::string request;
|
||||
|
||||
// read length header
|
||||
unsigned long long bytesLen = 0;
|
||||
unsigned long long readdatalen = 0;
|
||||
if (USE_LENGTH_HEADER) {
|
||||
long n = read(sockfd, &bytesLen, sizeof(bytesLen));
|
||||
if (n != 0 && n != sizeof(bytesLen))
|
||||
long n = read(sockfd, &readdatalen, sizeof(readdatalen));
|
||||
if (n != 0 && n != sizeof(readdatalen))
|
||||
error("ERROR reading length header failed");
|
||||
}
|
||||
|
||||
// read data
|
||||
long n;
|
||||
long readed = 0;
|
||||
do {
|
||||
memset(buffer, 0, TCPNET_BUFFER_SIZE);
|
||||
n = read(sockfd, buffer, TCPNET_BUFFER_SIZE - 1);
|
||||
|
|
@ -160,25 +165,26 @@ std::string TcpNet::read_from_socket(int sockfd) {
|
|||
|
||||
std::string part{buffer};
|
||||
request.append(part);
|
||||
} while ((USE_LENGTH_HEADER && n < bytesLen) || n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
|
||||
readed += n;
|
||||
} while ((USE_LENGTH_HEADER && readed < readdatalen) || n == TCPNET_BUFFER_SIZE - 1); // TODO what if data exactly of this size
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
void TcpNet::write_to_socket(int sockfd, const std::string &str) {
|
||||
const char *buffer = str.c_str();
|
||||
int pos = 0;
|
||||
long n;
|
||||
|
||||
// write length header
|
||||
unsigned long long bytesLen = str.length();
|
||||
unsigned long long writedatalen = str.length();
|
||||
if (USE_LENGTH_HEADER) {
|
||||
n = write(sockfd, &bytesLen, (int) sizeof(bytesLen));
|
||||
n = write(sockfd, &writedatalen, (int) sizeof(writedatalen));
|
||||
if (n < 0)
|
||||
error("ERROR writing size number to socket");
|
||||
}
|
||||
|
||||
// write data
|
||||
int pos = 0;
|
||||
do {
|
||||
n = write(sockfd, buffer + pos, (int) (str.length() - pos));
|
||||
if (n < 0)
|
||||
|
|
|
|||
1
ml.cpp
1
ml.cpp
|
|
@ -21,6 +21,7 @@
|
|||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <ctime>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@
|
|||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
|
||||
std::string read_file_contents(const std::string &filename) {
|
||||
std::ifstream f;
|
||||
|
|
|
|||
Loading…
Reference in New Issue