CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764

    Question Read from socket line by line

    Hi,

    I want to read data from socket, line by line. Like we read lines from a file (with CStdioFile::ReadString). I'm using winsock (No MFC).

    i want to replace "recv" function with something that reads a line.

    Any ideas without MFC?

    Thanks,

    John.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  2. #2
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    No one???
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  3. #3
    Join Date
    Oct 2003
    Location
    Reading, UK
    Posts
    46
    There are a whole load of socket functions within the Platform SDK, search for Windows Sockets 2.

    -- Raj
    Parampreet Sidhu

  4. #4
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    There are a whole load of socket functions within the Platform SDK, search for Windows Sockets 2.
    In those whole lot of socket functions, i couldn't find something i wanted (without MFC). You know any function?

    -John.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  5. #5
    Join Date
    Dec 2000
    Location
    Slovakia
    Posts
    1,043
    What is the line? It is a string that ends by "\r\n"? Or did you mean something else?

    Data transmitted through sockets are binary data. So, there is nothing like one line. You need to define this behavior yourself.

    What do you want your "recv" function to do if there is still no "line-separator" in incoming data? Should the function returns control to the caler or should it block the tread until the whole line is received?

    Generally, you need to build your own function, say GetSockLine(). That function will use recv function. It will read the data byte by byte and will store it into some internal buffer. Then, when the recv reads the "line-separator" characters, it returns the whole buffer.

    martin

  6. #6
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    Yes, I meant "\r\n".
    Generally, you need to build your own function, say GetSockLine(). That function will use recv function. It will read the data byte by byte and will store it into some internal buffer. Then, when the recv reads the "line-separator" characters, it returns the whole buffer.
    I thought about it. But what I was looking for was like CStdioFile::ReadString, where ReadString function does this internally.


    And is it advisable to read single byte at a time?

    "reading a 1000 bytes buffer at a time" and "reading a byte 1000 times", which one is better in terms of speed?

    Thanks.

    -John.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  7. #7
    Join Date
    Feb 2002
    Posts
    5,757
    One solution is to store incoming data to temporary memory the program finds the "\r\n." After find it, process the stored data and continue receiving data from socket buffer.

    Kuphryn

  8. #8
    Join Date
    Jan 2000
    Location
    Sweden
    Posts
    197
    Originally posted by Indian_Techie
    "reading a 1000 bytes buffer at a time" and "reading a byte 1000 times", which one is better in terms of speed?
    The first, but remember that recv will only read as many character as have been received on the socket, so when you try to revc 1000 bytes you may get less. (This of course also depends on how often you call select.)

  9. #9
    Join Date
    Sep 2000
    Location
    Munich (Germany)
    Posts
    764
    The first, but remember that recv will only read as many character as have been received on the socket, so when you try to revc 1000 bytes you may get less. (This of course also depends on how often you call select.)
    Actually my problem is the folowing..

    1. I have a proxy running on a remote machine.
    2. I have my application (say myapp) running on client machine.
    3. myapp sets the browser's proxy server settings to "localhost" and port to 420.
    4. And my app starts listening at 420 for any requests from browser (i.e., myapp waits on accept).
    5. As soon as it gets request from the browser, it appends authentication information to the request and sends it to proxy.
    6. And waits for response from proxy (This wait for response is in a loop where recv is).
    7. And I getout of the loop when recv returns less than 1.

    Ok, My problem here is... when ever data is there in the socket, recv returns quite fast. But when there is no more data, recv gets stuck for a while and returns 0 or error (less than zero). This is taking time. If I just know that there is no more data, I dont want to do recv (and spend un-necessary time there).

    I tried with select before recv also (to see if there is any data left to send), but select is taking time too. In cases where recv returns data, I dont need select (which is time consuming).

    Is there any better way of doing this? I heard it cound be made faster by using non-blocking sockets. I dont know how. Any hints?

    Thanks,

    -John.
    Do you develop for PocketPCs? Try this tool for CeDatabases

    CeDatabase Manager 2.0

  10. #10
    Join Date
    Oct 2003
    Location
    China.Asia.Earth.Solar.Galaxy
    Posts
    10

    Get packet firstly, then divide them line by line.

    From socket, You only can get packet. the packet is made of bytes. from 1 byte to 65490 or so. no line in them.

    So if both sides (Server and Client) are write by yourself. you can add any tag in send packet. this tag express "\r\n". and receive socket can distinguish line by tag.
    if only one side write by you, you can receive the packet, and them copy it to a buffer, then split them by searching "\r\n".
    Unique is unique.

  11. #11
    Join Date
    Oct 2003
    Location
    China.Asia.Earth.Solar.Galaxy
    Posts
    10

    below

    memset(RecvBuf,0,sizeof(RecvBuf));
    g_bStop=FALSE;
    while (;
    {
    if (g_bStop==TRUE)
    break;
    rect=recvfrom(sock,RecvBuf,sizeof(RecvBuf),0,(LPSOCKADDR)&addr_remote,&buflen);
    DoEvent();
    if (rect==SOCKET_ERROR)
    {....}
    else
    {....}
    }
    Unique is unique.

  12. #12
    Join Date
    Oct 2003
    Location
    China
    Posts
    6
    if u using tcp, u can recv all data to a buffer, then search "\r\n".

    ...
    char buffer[32000];
    char *p = buffer;

    while (1) {
    ret = recv(sock, p, len, 0);
    if (ret != -1) {
    p += ret;
    for (... search "\r\n" from buffer to p)
    }
    }
    deep water, high sky

    SunKW

  13. #13
    Join Date
    Feb 2002
    Posts
    5,757
    Check out CodeProject code section for examples of winsock non-blocking I/O models.

    Kuphryn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured