Click here to Skip to main content
13,198,563 members (44,639 online)
Rate this:
Please Sign up or sign in to vote.
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 9-Apr-12 11:49am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
she-programmer 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 9 Apr 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100