[C] How do I read a whole line from a client?

  • Thread starter zeion
  • Start date
  • Tags
    Line
In summary, the server needs to work in noncanonical mode while able to process a whole string. The suggestion is to use data structures for packets where the structure itself has a length parameter within the header (which is sent first). Then what you do is that as you receive more data, you basically check after you have acknowledged that you have a valid header, that you have the number of bytes that are specified in that header. If you have the number of bytes specified in the header then you take all this data and store it somewhere as a complete packet to be processed. What you can do with this model is that you have multiple packet types and each packet can have a call-back routine that handles an event of getting a packet
  • #1
zeion
466
1
Hi I'm trying to write a server that will ask for a client's name when they connect then store that name as a string with the client struct. I got everything set up but when the client types one letter it is immediately processed without waiting for a new line input. How can I make it wait for a new line and store all characters of the name in the buffer before processing?

Basically the server needs to work in noncanonical mode while able to process a whole string
 
Last edited:
Technology news on Phys.org
  • #2
Hey zeion.

The suggestion I have is to use data structures for packets where the structure itself has a length parameter within the header (which is sent first).

Then what you do is that as you receive more data, you basically check after you have acknowledged that you have a valid header, that you have the number of bytes that are specified in that header.

If you have the number of bytes specified in the header then you take all this data and store it somewhere as a complete packet to be processed.

What you can do with this model is that you have multiple packet types and each packet can have a call-back routine that handles an event of getting a packet.

You can associate packets with sessions and then when you get the data for a session, the session will update its status.

If you want to wait until you get the full name, you can basically use a mutex associated with the session and when the callback is called for the client name packet for that session, the mutex is changed in state and you can grab the client name from the appropriate data structure.

You can also in the mutex loop, put a timeout value that is checked periodically.

The reason why you would use callbacks for every kind of packet is because you can add as many network protocols as you want, and the mechanism to handle all of these is streamlined which means adding new packet types is really really simple.
 
  • #3
If you have Visual Studio version of C, you can use _cgetws(), which is a unicode version of _cgets() (_cgets() has been broken in Visual Studio for a long time, but _cgetws() should work). You'll need to include <conio.h> to use it.
 

1. What is the purpose of reading a whole line from a client in C?

The purpose of reading a whole line from a client in C is to receive input from the user in a single line, rather than reading individual characters or words. This makes it easier to interpret and use the input in your program.

2. How do I read a whole line from a client in C?

To read a whole line from a client in C, you can use the fgets() function. This function takes in the input stream, the maximum number of characters to be read, and the location to store the input. It will read characters from the input stream until it reaches the maximum number of characters or a newline character, whichever comes first.

3. What happens if the input line is longer than the maximum number of characters specified in fgets()?

If the input line is longer than the maximum number of characters specified in fgets(), the function will read up to the maximum number of characters and then stop. The remaining characters will still be present in the input stream and will need to be cleared before reading further input.

4. How do I handle errors while reading a whole line from a client in C?

You can handle errors while reading a whole line from a client in C by checking the return value of fgets(). If the function returns NULL, it signifies an error has occurred, and you can use the perror() function to print out an error message. It is important to handle errors to prevent your program from crashing or behaving unexpectedly.

5. Can I use other functions to read a whole line from a client in C?

Yes, there are other functions that can be used to read a whole line from a client in C, such as gets(), scanf(), and readline(). However, fgets() is the most commonly used function as it provides more control and prevents buffer overflow. It is also recommended to use fgets() with a specified maximum number of characters to read to avoid potential security risks.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
39
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Computing and Technology
Replies
5
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
3
Views
888
  • Programming and Computer Science
Replies
1
Views
1K
Replies
10
Views
958
  • Programming and Computer Science
Replies
6
Views
1K
Back
Top