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

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Line
Click For Summary
SUMMARY

This discussion focuses on implementing a server in C that requires waiting for a newline input from a client before processing the client's name. The solution involves using noncanonical mode and data structures for packets, which include a length parameter in the header. The server can handle multiple packet types with callback routines for each, allowing for streamlined processing and session management. The use of the _cgetws() function from Visual Studio is recommended for reading wide character strings, as it is a reliable alternative to the deprecated _cgets().

PREREQUISITES
  • Understanding of noncanonical mode in network programming
  • Familiarity with C programming and data structures
  • Knowledge of mutexes for thread synchronization
  • Experience with Visual Studio C and its libraries, specifically
NEXT STEPS
  • Research how to implement noncanonical mode in C network applications
  • Learn about packet structure design and header management in network protocols
  • Explore the use of mutexes for managing shared resources in concurrent programming
  • Investigate the differences between _cgetws() and other input functions in Visual Studio
USEFUL FOR

Software developers, particularly those working on network applications in C, and anyone interested in implementing robust client-server communication protocols.

zeion
Messages
455
Reaction score
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
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.
 
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.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 39 ·
2
Replies
39
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
Replies
2
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
10
Views
2K