Click here to Skip to main content
15,867,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am (still) working on converting some linux code to run on windows. My one remaining bit to sort involves putting everything received on the socket into a buffer and just getting a line out at a time to process, rather than getting several lines at once and then having to seperate them in order to process them seperately. I'm not sure if I've explained that clearly enough but I am hoping that anyone who know about this stuff will know what I'm on about.

In the linux code:
// create the stream
FILE* m_Rx = fdopen(clientSocket, "r");
// set stream to line buffered mode.
setlinebuf( m_Rx );


Then after using select() to trigger when there is something received on the socket:
fgets( m_RxMsgBuf, RX_MSGBUF_LEN, m_Rx )

and then m_RxMsgBuf contains just one line from whatever is received on the socket so that just one line can be processed at a time.

Please could somebody help me find the simplest way to do this in windows. _fdopen() cannot be used with sockets, so I don't know how to link a socket with a stream. I also guess I would need to use setvbuf() instead of setlinebuf() in order to set the stream to line buffered mode.

Failing this, I will just try to write a method to call recv() on the socket without bothering with a stream, and then just grab a line from the socket's buffer at a time.

Many thanks for any help.
Posted

1 solution

Right, Windows doesn't provide a File I/O style interface for sockets.

You'll need to parse those strings yourself.

How you do it, will depend on how you're processing the strings.

One way is to walk the string using strchr() to find the '\n' characters and extract the substring from those positions.

If your are using CString or std::string, then those classes have find() functions that can be leveraged to do this.
 
Share this answer
 
Comments
Jackie Lloyd 9-Apr-12 18:11pm    
Thankyou very much for that info. I thought there would be a cleverer way more akin to the linux method. Now I know there isn't I shall happilly plod on with walking the strings as you suggest :)

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