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

  • Thread starter Thread starter zeion
  • Start date Start date
  • Tags Tags
    Line
AI Thread Summary
To read a whole line from a client in a server application, it's essential to implement a noncanonical mode that processes input only after a newline is received. Utilizing data structures for packets can help manage incoming data by including a length parameter in the header, allowing the server to confirm the complete packet before processing. This approach enables the handling of multiple packet types with associated callback routines, which can streamline the addition of new protocols. Implementing a mutex for session management can help synchronize access to the client name data, ensuring it is only processed once fully received. Using the Visual Studio version of C, the function _cgetws() can be employed to facilitate this process.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top