The reading method of the examples used in this chapter works perfectly with a client such as the Linux version of the telnet command, as this client sends (by default) line by line, in the TCP communication channel, what is entered. On the other hand, this does not work with the Windows version of the telnet command, as this other client sends character by character as it is entered.
It must be assumed that a reception of a TCP stream can be fragmented into several pieces of size (all of which are smaller than the buffer passed to the read function). A line must therefore be read through a read loop that puts each piece together until the end of line character is received.
For completeness, it is also necessary to provide for the case where you would start receiving some characters from the next line, which should be preserved and isolated from the previous line.
Without stream support | With stream support |
---|---|
In multi-processSee the improved solutions of LAB n°2 question n°2. In multi-threadSee the improved solutions of LAB n°2 question n°3. In single-taskSee the improved solutions of LAB n°2 question n°4. |
In the C language, there is the FILE type defined in stdio.h that offers several possibilities including the following:
A file descriptor can be associated with a stream management using the fdopen(int fd, const char *mode) function, which returns a pointer to a FILE if the association was successful. The first argument of the function is the file descriptor. The second argument is a string indicating the mode of use, which must be compatible with the file descriptor's operation. Example: #include <stdio.h> void server_dialog(int fd) { FILE *stream; char buffer[1024]; stream=fdopen(fd,"rb"); if (stream!=NULL) { while (!feof(stream)) { fgets(buffer,sizeof(buffer),stream); write(fd,buffer,strlen(buffer)); } } } Note: Even if this method may be enjoyable to use and works very well in single-client and multi-client when using multi-process or multi-thread, it should not be forgotten that stream reading functions such as fgets are, by default, blocking because they use read loops and can therefore pose problems in a multi-client/single-task programming using the select function. |