Click here to Skip to main content
15,746,108 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
/* Function: do_server() - write data to stdout
void do_server(int fd)
    char buf[BUFS];
    int r;
    int read_bytes(int, char *, int);

    while((r = read_bytes(fd, buf, 1)) > 0)
        write(1, buf, r);

How can I make here that another function called write_line is executed at the same time to write the received data in a TXT? please help.

What I have tried:

How can I make here that another function called write_line is executed at the same time to write the received data in a TXT? please help.
Updated 8-May-23 13:56pm
Richard MacCutchan 2-May-23 8:24am    
Instead of reading the bytes one at a time, try reading as many as will fit in the buffer. You can then write the complete set to your output file. And unless the input has some way of identifying the end of each line, you should just write the data as is.

What you have reads one byte at a time and writes it to file descriptor 1. You need to assemble a line of text in another buffer and then call write_line with it. The code you have will probably work OK for output but you might have to add newline characters to get line breaks in your output.
Share this answer
Member 15992924 1-May-23 13:23pm    
Thanks for your help. I have another part that is the write, but I don't know how to put it. Sorry for being so nobs. That is why I ask for help to see what I have to put at this point so that at the same time that it writes the data to the console, it writes it to a file.
The line breaks are not a problem but I don't know how to do this part that you commented on in the code. Thank you
If you really mean concurrent in the sense of parallel, this would be possible with threads.
Since the buffer is then a critical resource, it is necessary to synchronize here.

Rick's suggestion to first fill the array buf[] until buf[] is full or the read is canceled seems to be the much easier way without these problems.

Instead of while you could write something like this:
int i, r=1;
for(i=0; (i < sizeof(buf)) && (r >0); i++) 
    r = read_bytes(fd, &buf[i], 1));
write(stdout, buf, i);
write_line(fdout, buf i);

1. I would recommend not to hardcode the first parameter 1 in write, but to write stdout instead
2. Writing single bytes is very unperformant. Shouting multiple bytes would be better.
Share this answer
Member 15992924 2-May-23 13:57pm    
I have done what you told me and now I do not see the data that arrives in the shell and the file that I have put in the write_line creates a .txt and it does not leave me anything it does not write to me, I have two function options write_line I leave them to you here for you to see

Thanks for your help.

/*int write_line(char string[MAX_LENGTH])

FILE *fp;

fp = fopen("./vdn.log", "a+");
fwrite(string, sizeof ( char ), strlen(string), fp);
return 0;
int write_line(char string[MAX_LENGTH]){
FILE *fp;

char* array = strdup(string);
char* filename="vdn.txt";
char* dir=get_current_dir_name();

char fullpath[MAX_LENGTH];

strcat(fullpath, dir);
strcat(fullpath, "/");
strcat(fullpath, filename);
    fp=fopen(fullpath, "a");

        return -1 ;

    int i;

    for (i=0; i
Member 15992924 22-Jun-23 6:06am    

After putting this function to parse the socket information I encounter a problem, the data that the tcp socket client sends me I see that it takes a long time to arrive because it has to fill the buffer before sending me the data.

Can this not be removed so that it does not have to look at the buffer to complete the sending of data?

I have this Bug now in the application, the other end that sends me the data by TCP Socket has a sending every 10 seconds, and I see that it does not take until almost 1 minute to get the data because it establishes a connection and after 10 seconds is when it sends the data. data

Any idea how to fix this in this function?

Thanks in advance.

int i, r=1;
for(i=0; (i < sizeof(buf)) && (r >0); i++)
r = read_bytes(fd, &buf[i], 1));

write(stdout, buf, i);
write_line(fdout, buf i);
merano99 22-Jun-23 13:25pm    
If I understand it correctly, it is sent every 10 sec, but the connection setup as well as receiving the data takes almost 1 minute?

If you can not send and receive at least so fast that the data can be processed, it comes to data congestion.

I would copy the incoming packets to larger packets and send them only from a minimum size.

If this is not possible you have to work asynchronous and parallel (multithreaded). What kind of data is this?
Some questions run through my head: Do the data packets contain a date or a numbering? Is the order, as well as the runtime critical?

Note: 1 minute is an eternity in the IT world. Is there no way to speed it up?

Using mutltithreading under Linux with C would amount to thread generation (possibly Posix library) as well as synchronization with mutexes.

Is it possible that the very long time when filling the buffer only occurs because a timeout is waited for here?

Here a solution would be to work with a fixed packet length and not to wait until nothing more is received.
It would also help if the total packet length was included in the first received bytes.

It would be useful to add all helpful information to your problem above in the original question, so it can be found by all. Alternatively, create a new question with all the parameters.
Member 15992924 22-Jun-23 13:59pm    
thank for answer i here other answer with more datas and problem.
As you've mentioned '...the shell...' , I presume you're on a linux/unix platform.
Why not pipe the output to a file using the tee command?
This means you Won't need to change your existing code if its already sending output to stdout.

myprogram | tee somefile

myprogram | tee somefile someOtherFile # write to multiple files

myprogram | tee -a somefile  #append to somefile
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900