So I'm reading in files and outputting the contents of the files onto the console using system calls. Also I want to add a space to the output, every 20 lines I encounter. This is where I'm having trouble, in that despite the following few lines of code, the entire file is being displayed without the spaces
//Write file contenst
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
write(1, &nLineCount, sizeof(nLineCount));
if(nLineCount == 20)
{
write(1, "\n", 2);
nLineCount = 0;
}
if(nReadFile = write(1, buffer, nReadFile) == '\n')
{
nLineCount++;
}
}
Here is the entire program (Excluding the .h file containing the libraries)
#include"mymore.h"
int main(int argCount, char *argv[])
{
struct termios initial_settings, new_settings;
tcgetattr(fileno(stdin), &initial_settings);
new_settings=initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
if (tcsetattr(fileno(stdin), TCSANOW, &new_settings)!=0)
{
fprintf(stderr, "could not set attributes\n");
}
int nLineCount = 0;
int nCheckFile = 0;
int nFileCountCounter = 1; //first arguement interested in is argv[1]
int nOpenFile = 0;
int nReadFile = 0;
int nCount = 0;
char *cData;
char buffer[0];
//check that arguements have been provided
if( argCount < 2)
{
write(1, "There needs to be at least one file provided! \n", 50);
return 1;
}
do
{
printf("%d", argCount);
//check if file exists
nCheckFile = access(argv[nFileCountCounter], F_OK);
if(nCheckFile != 0) //if file does not exist
{
write(1, "The file ", 10);
write(1, argv[nFileCountCounter], strlen(argv[1]));
write(1," does not exist! \n", 25);
return 1;
}
else //file does exist
{
write(1, "Opening ", 10);
write(1, argv[nFileCountCounter], strlen(argv[1]));
write(1, "\n", 2);
}
//open the file
nOpenFile = open(argv[nFileCountCounter], O_RDONLY);
if(nOpenFile < 0)
{
write(1, "Failed to open file ", 10);
write(1, argv[nFileCountCounter], strlen(argv[nFileCountCounter]));
write(1, "\n", 2);
return 1;
}
//read file
cData = (char *) malloc(100 * sizeof(char));
cData[nReadFile] = '\0';//append null terminator
//find length of source file
while(cData[nCount] != 0)
{
nCount++;
}
//Write file contenst
while( (nReadFile = read(nOpenFile, buffer, 1) != 0))
{
write(1, &nLineCount, sizeof(nLineCount));
if(nLineCount == 20)
{
write(1, "\n", 2);
nLineCount = 0;
}
if(nReadFile = write(1, buffer, nReadFile) == '\n')
{
nLineCount++;
}
}
cout << nLineCount << endl;
//close file
close(nOpenFile);
//Increment to next file
nFileCountCounter++;
}while(nFileCountCounter < argCount);//while there are still arguements
tcsetattr(fileno(stdin), TCSANOW, &initial_settings);
return 0;
}
This is actually my first experience using system calls, and one thing I think I've noticed is that the write command is being executed before any of the c code?
Any ideas?
Thanks
There are plenty of mistakes in your code:
char buffer[0]; should be char buffer[1];
char buffer[0]; defines a buffer which can store zero character, I suppose it is not what you want.
write(1, "There needs to be at least one file provided! \n", 50); should be
char *msg = "There needs to be at least one file provided! \n";
write(1, msg, strlen(msg));
the message is longer than 50. There are serval similar mistakes, and can be fixed similarly.
while( (nReadFile = read(nOpenFile, buffer, 1) != 0)) should be
while( (nReadFile = read(nOpenFile, buffer, 1)) != 0)
In C, operator != has higher precedence than =, so nReadFile = read(nOpenFile, buffer, 1) != 0 meas nReadFile = (read(nOpenFile, buffer, 1) != 0).
if(nReadFile = write(1, buffer, nReadFile) == '\n')
If you want to check whether the currect character is a newline, you should to something like if (buffer[0] == '\n').
cout << nLineCount << endl;
This is C++, not C.
Related
I managed to compile ncat. I am using -k option to keep server open. Instead of accepting data to STDOUT, my goal is to write to files instead. So far I was able to write to a file instead of STDOUT but my goal is to loop through new files on each new connection. Right now it is appending to the same filename_0 and f++ is not incrementing. Here is what I have so far. The original code will be below. The difference is in the else clause, basically if n is actually greater than 0. On each loop, n is 512 bytes until the last chunk. I just want to be able to have new files from each new connection. filename_0, filename_1, filename_3, etc.
MODIFIED CODE:
/* Read from a client socket and write to stdout. Return the number of bytes
read from the socket, or -1 on error. */
int read_socket(int recv_fd)
{
char buf[DEFAULT_TCP_BUF_LEN];
struct fdinfo *fdn;
int nbytes, pending;
int f = 0;
fdn = get_fdinfo(&client_fdlist, recv_fd);
ncat_assert(fdn != NULL);
nbytes = 0;
do {
int n, s;
n = ncat_recv(fdn, buf, 512, &pending);
if (n <= 0) {
if (o.debug)
logdebug("Closing fd %d.\n", recv_fd);
#ifdef HAVE_OPENSSL
if (o.ssl && fdn->ssl) {
if (nbytes == 0)
SSL_shutdown(fdn->ssl);
SSL_free(fdn->ssl);
}
#endif
close(recv_fd);
checked_fd_clr(recv_fd, &master_readfds);
rm_fd(&client_fdlist, recv_fd);
checked_fd_clr(recv_fd, &master_broadcastfds);
rm_fd(&broadcast_fdlist, recv_fd);
conn_inc--;
if (get_conn_count() == 0)
checked_fd_clr(STDIN_FILENO, &master_readfds);
return n;
}
else {
char filename[20];
snprintf(filename, sizeof(char) * 20, "filename_%i", f);
FILE *fp = fopen(filename, "a");
if (fp == NULL)
{
printf("Could not open file");
return 0;
}
//Write(STDOUT_FILENO, buf, n);
s = fwrite(buf, 1, n, fp);
fclose(fp);
f++;
nbytes += n;
}
} while (pending);
return nbytes;
}
ORIGINAL CODE:
int read_socket(int recv_fd)
{
char buf[DEFAULT_TCP_BUF_LEN];
struct fdinfo *fdn;
int nbytes, pending;
fdn = get_fdinfo(&client_fdlist, recv_fd);
ncat_assert(fdn != NULL);
nbytes = 0;
do {
int n;
n = ncat_recv(fdn, buf, sizeof(buf), &pending);
if (n <= 0) {
if (o.debug)
logdebug("Closing fd %d.\n", recv_fd);
#ifdef HAVE_OPENSSL
if (o.ssl && fdn->ssl) {
if (nbytes == 0)
SSL_shutdown(fdn->ssl);
SSL_free(fdn->ssl);
}
#endif
close(recv_fd);
checked_fd_clr(recv_fd, &master_readfds);
rm_fd(&client_fdlist, recv_fd);
checked_fd_clr(recv_fd, &master_broadcastfds);
rm_fd(&broadcast_fdlist, recv_fd);
conn_inc--;
if (get_conn_count() == 0)
checked_fd_clr(STDIN_FILENO, &master_readfds);
return n;
}
else {
Write(STDOUT_FILENO, buf, n);
nbytes += n;
}
} while (pending);
return nbytes;
}
I was able to figure out using the other functions involved. i passed a pointer into this function to write to it. the handler is a function i added the open() file pointer to.
I have to implement standard unix command "tail" without fopen, fclose, fread, fseek, printf. I am ready with everything else except handle of partial reads and writes.
And here is my print with handle of partial read and write of last 10 lines. It's happening after lseek to the right position for fd!
I test it several times with different files and it works like real tail, but i cant handle partial reads and writes like i said.
'''code'''
do
{
memset(buff, 0, SIZE);
read_value = read(fd, buff, SIZE);
ssize_t bytes_read_total = read_value;
int count=2;
char buff_copy[SIZE];
while (bytes_read_total < SIZE)
{
read_value = read(fd, buff + bytes_read_total, SIZE - bytes_read_total);
if(count%2==0)
strcpy(buff_copy, buff);
if (read_value == -1)
{
char *err;
err = (char *)malloc(sizeof(char)*(50+strlen(file)));
strcpy(err, "tail: error reading '");
strcat(err, file);
strcat(err, "'");
perror(err);
free(err);
return 2;
}
if(read_value==0)
{
strcpy(buff, buff_copy);
read_value = strlen(buff_copy);
break;
}
bytes_read_total+= read_value;
}
ssize_t bytes_written;
ssize_t bytes_written_total = 0;
while (read_value != bytes_written_total)
{
bytes_written = write(STDOUT_FILENO, buff+bytes_written_total, read_value - bytes_written_total);
if (bytes_written == -1)
{
perror("tail: error writing 'standard output'");
return 3;
}
bytes_written_total += bytes_written;
}
}while(read_value);
'''code'''
I'm having trouble reading in a file line by line. Apparently the read() system call grabs the whole file. I'm trying to read in a file with lines of variable length, however I do know that no line's length can exceed SBUFSIZE bytes. I'm supposed to read in each line in the file and put each line of the file onto a data structure. However my approach pushes the whole file as one line onto the data structure, which is not acceptable. Is there a modified version of read() which stops at the '\n' character?
#define SBUFSIZE 1025
pthread_mutex_t buffer_lock;
void* process_file(void* file_name)
{
int input_fd;
/* Temporary buffer, for reading in the files, one line at a time. */
char buf[SBUFSIZE];
memset(buf, '\0', SBUFSIZE);
if ((input_fd = open((char*) file_name, O_RDONLY)) == -1) {
fprintf(stderr, "Cannot open the file '%s'\n", (char*) file_name);
pthread_exit((void*) 1); /* This is my error flag. */
}
while (read(input_fd, buf, SBUFSIZE)) {
int ret;
printf("|%s|\n", buf);
while (true) {
pthread_mutex_lock(&buffer_lock);
ret = stack_push(buf);
if (ret == STACK_FULL) {
pthread_mutex_unlock(&buffer_lock);
usleep(rand() % 101);
} else {
break;
}
}
pthread_mutex_unlock(&buffer_lock);
memset(buf, '\0', SBUFSIZE);
if (ret != STACK_SUCCESS) {
exit(EXIT_FAILURE);
}
}
close(input_fd);
pthread_exit((void*) 0); /* This is my good flag. */
}
You can process line-by-line as follows:
char buf[SBUFSIZE + 1];
size_t bufsize = 0;
for(;;)
{
ssize_t nread = read(input_fd, buf + bufsize, SBUFSIZE - bufsize);
if(nread < 0)
perror("read failed");
bufsize += nread;
if(!bufsize)
break; // end of file
const char *eol = memchr(buf, '\n', bufsize);
if(!eol)
eol = buf + bufsize++;
*eol = 0;
printf("processing line: |%s|\n", buf);
process_line(buf);
++eol;
bufsize -= eol - buf;
memmove(buf, eol, bufsize);
}
I want to implement a simple TCP server with blocking read, that receives messages sent from a client character by character until a separator. Once a message is received, it has to wait until the next message appears. Here is my pseudocode:
// Messages sent from the client
char *message1 = "mssg1\n"
char *message2 = "mssg2\n"
// On server side
char buffer;
char completeMessage[5]
while(1){
while(buffer != '\n'){
recv(sock, &buffer, 1, 0); // 1 is the read size
if(buffer != '\n') {
printf("buffer: %c\n", buffer);
completeMessage[n] = buffer;
count ++;
}
else{
printf("Complete message: %s\n", completeMessage);
count = 0;
}
}
}
And the result is the following:
buffer: m
buffer: s
buffer: s
buffer: g
buffer: 1
Complete message: mssg1
buffer:
buffer:
buffer:
buffer:
buffer:
buffer:
// Error due to buffer overflow
I don't know why recv instead of waiting for the next message character (blocking read), it continues reading blank spaces. My questions are the following:
Is recv really a socket blocking read function?
Is there something wrong or missing in the code?
Any other suggestions for implementing this?
Is recv really a socket blocking read function?
Yes, unless you made the handle non-blocking.
Is there something wrong or missing in the code?,
You're not checking what recv returns. 0 indicates EOF, and -1 indicates an error.
You don't check how full your buffer is, so you risk buffer overflows.
You're not terminating the string in completeMessage with a NUL as required by printf %s.
Any other suggestions for implementing this?
You shouldn't read a character at a time!
#define BUFFER_SIZE (64*1024)
char* extract_string(const char* start, const char* end) {
size_t len = end - start;
char* dst = malloc(len+1);
if (dst == NULL)
return NULL;
memcpy(dst, src, len);
dst[len] = '\0';
return dst;
}
{
char buf_start[BUFFER_SIZE];
char* buf_end = buf_start + BUFFER_SIZE;
char* window_start = buf_start;
char* window_end = buf_start;
while (1) {
if (window_end == buf_end) { // No more space.
fprintf(stderr, "Overly large message");
return 0;
}
ssize_t rv = recv(sock, window_end, buf_end-window_end, 0);
if (rv == -1) { // Error.
perror("recv");
return 0;
}
if (rv == 0) { // EOF.
return 1;
}
while (rv--) {
if (*(window_end++) == '\n') {
char* msg = extract_string(window_start, window_end-1); // Excl LF.
if (msg == NULL) {
fprintf(stderr, "Out of memory");
return 0;
}
// Do something with msg
printf("Complete message: %s\n", msg);
free(msg);
window_start = window_end;
}
}
memmove(buf_start, window_start, window_end-window_start);
window_end -= (window_start - buf_start);
window_start = buf_start;
}
}
There are quite a number of problems with your code, namely that you are ignoring the return value of recv(), you are not null-terminating your buffer before printing it, and you are not protecting yourself from a buffer overflow.
Try something more like this instead:
char ch, *tmp, *message = NULL;
int ret, length = 0, allocated = 0;
while (1)
{
ret = recv(sock, &ch, 1, 0);
if (ret <= 0)
{
if (ret < 0)
printf("Read error: %d\n", errno); // or WSAGetLastError() on Windows
else
printf("Client disconnected\n");
break;
}
if (ch == '\n')
{
if ((length > 0) && (message[length-1] == '\r'))
--length;
printf("Complete message: '%.*s'\n", length, message);
length = 0;
}
else
{
printf("ch: %c\n", ch);
if (length == allocated)
{
if (length >= 5000) // some max length of your choosing...
{
printf("Message length too large!\n");
break;
}
// just for example. You should use a more robust growth algorithm in production code...
tmp = (char*) realloc(message, allocated + 10);
if (!tmp)
{
printf("Memory allocation failed\n");
break;
}
message = tmp;
allocated += 10;
}
message[length] = ch;
++length;
}
}
free(message);
Alternatively, don't read char-by-char. Read as much data as you can from the socket on any given read and store it all in a growing buffer, and then scan that buffer for complete messages, eg:
char *buffer = (char*) malloc(100);
if (!buffer)
{
printf("Memory allocation failed\n");
}
else
{
int ret, offset, remaining, inbuf = 0, allocated = 100;
char *ptr;
while (1)
{
if (inbuf == allocated)
{
if (inbuf >= 5000) // some max length of your choosing...
{
printf("Buffer length too large!\n");
break;
}
// just for example. You should use a more robust growth algorithm in production code...
tmp = (char*) realloc(buffer, allocated + 100);
if (!tmp)
{
printf("Memory allocation failed\n");
break;
}
buffer = tmp;
allocated += 100;
}
ret = recv(sock, buffer+inbuf, allocated-inbuf, 0);
if (ret <= 0)
{
if (ret < 0)
printf("Read error: %d\n", errno); // or WSAGetLastError() on Windows
else
printf("Client disconnected\n");
break;
}
printf("Received: %.*s\n", ret, buffer+inbuf);
inbuf += ret;
while (ptr = (char*)memchr(buffer, '\n', inbuf))
{
offset = (ptr-buffer);
if ((offset > 0) && (buffer[offset-1] == '\r'))
--offset;
printf("Complete message: '%.s'\n", offset, buffer);
++ptr;
remaining = (inbuf - (ptr - buffer));
if (remaining > 0)
memmove(buffer, ptr, remaining);
inbuf = remaining;
}
}
free(buffer);
}
I have an assignment in which a TCP client sends data to the TCP server in the form of:
IP_address\0port\0message\n
Now, the server (IP address 10.0.2.15) receives the packet fine when I send some data through a terminal like this:
printf "127.0.0.1\0004444\000Some message\n" | nc -N 10.0.2.15 3333
However, the second part of the assignment is to read a packet that comes in multiple segments:
(printf "127.0.0.1"; sleep 0.3; printf "\0004444\000"; sleep 0.3; \
printf "It works"; sleep 0.5; printf "\n") | nc -N 10.0.2.15 3333
How should I implement the read function on the server so that, if possible, all the segments are stored into a buffer?
The number of bytes recv() returns can be as few as 1 byte up to as many bytes as requested. TCP is a byte stream, it has no concept of messages, that has to be handled in the application code instead.
The receiver must know how many bytes to expect, and then keep reading in a loop until it has read that many bytes, however many reads it takes.
However, in this situation, the receiver does not know the exact length of the message, because the sender is not sending the message length before sending the message itself, so the only option available is for the receiver to read from the socket byte-by-byte until it encounters the terminating \n.
For example:
int readLine(int socket, char **line)
{
int r, len = 0, cap = 256;
char b;
*line = NULL;
char *outline = (char*) malloc(cap);
if (!outline) return -2;
do
{
r = recv(socket, &b, 1, 0);
if (r <= 0)
{
free(outline);
return r;
}
if (b == '\n')
break;
if (len == cap)
{
cap += 256;
char *newline = (char*) realloc(outline, cap);
if (!newline)
{
free(outline);
return -2;
}
outline = newline;
}
outline[len] = b;
++len;
}
while (true);
if ((len > 0) && (line[len-1] == '\r'))
--len;
if (len == cap)
{
char *newline = (char*) realloc(outline, cap + 1);
if (!newline)
{
free(outline);
return -2;
}
outline = newline;
}
outline[len] = '\0';
*line = outline;
return 1;
}
char *line;
int r;
do
{
r = readLine(cliSock, &line);
if (r <= 0)
{
if (r == 0)
printf("client disconnected\n");
else if (r == -2)
printf("memory error\n");
else
printf("read error\n");
break;
}
// process line as needed...
free(line);
}
while (true);
Alternatively, you can use an intermediate buffer to help you cache data between reads and get data out of the socket more efficiently:
char *buffer;
int buflen, bufcap;
int readLine(int socket, char **line)
{
char *ptr;
int r, idx = 0;
*line = NULL;
do
{
ptr = memchr(buffer + idx, '\n', buflen - idx);
if (ptr)
{
int total = ((ptr + 1) - buffer);
int len = (total - 1);
if ((len > 0) && (buffer[len-1] == '\r'))
--len;
*line = (char*) malloc(len + 1);
if (*line == NULL)
return -2;
memcpy(*line, buffer, len);
(*line)[len] = '\0';
if (total < buflen)
memmove(buffer, buffer + total, buflen - total);
buflen -= total;
break;
}
if (buflen == bufcap)
{
int newcap = bufcap + 256;
char *newbuffer = (char*) realloc(buffer, newcap);
if (!newbuffer)
return -2;
buffer = newbuffer;
bufcap = newcap;
}
r = recv(socket, buffer + buflen, bufcap - buflen, 0);
if (r <= 0)
return r;
buflen += r;
}
while (true);
return 1;
}
buflen = 0;
bufcap = 256;
buffer = (char*) malloc(bufcap);
if (buffer)
{
char *line;
int r;
do
{
r = readLine(cliSock, &line);
if (r <= 0)
{
if (r == 0)
printf("client disconnected\n");
else if (r == -2)
printf("memory error\n");
else
printf("read error\n");
break;
}
// process line as needed...
free(line);
}
while (true);
free(buffer);
}