Thread: char problem

  1. #16
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    lol sorry for rushing things and asking too many questions at a time i just have alot of problems to solve and msdn isn't of such a big help to me anyway getting the DataSize is all that i need everything else is simple
    Quote Originally Posted by novacain
    The number of bytes read is returned (as an int) by Receive()
    [/code]
    i really tried this and all i get is "
    thanks alot for replying back

    :P wait im wrong it does work only problem is how do i get that DataSize if i use
    Code:
    DataSize=wsClient.Receive(NULL,NULL,MSG_PEEK);
    this wont receive the size probably because i put NULL there so lol.. i still gotta put a char with the len needed.. else it wont return me the lenght of the data or is there another way to get the lenght without putting any variable there ?
    Last edited by eXistenZ; 02-14-2005 at 11:53 AM.

  2. #17
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    This is the way I do my own communication and it seems robust enough but I am no expert either.

    When you are getting the data, I would say stick with a static sized buffer and then get a dynamically changing object to add to.

    So you would have...

    Code:
    #define SIZE 100
    ...
    CString msgBuffer;
    
    char data[SIZE];
    int received = 0;
    
    received = wsclient.receive( (void *)data, SIZE );
    
    //we have a problem here check the WSGetLastError();
    if(received == 0)
                 ...
    
    data[ received ] = '\0';
    msgBuffer += data;
    ...
    Something like this... I realize this is kinda messy and probably not the best thought out, but I would say try this out. For me it works very well and the performance is not all that bad(right now).

    Basically as was mentioned before check everything from connection on down to make sure to find where it is failing. It could be at that point an error was thrown and there is no socket available....

  3. #18
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    thx hmm what im basically trying to do is get the data lenght before i put the data in a variable so after that i can split all the data by ¬ character and get everything i need from that, i just need all the data in a variable thats all i will have to try more stuff maybe i can get it done somehow and there has to be way to get that done right, if anybody has some other ideas please post here i just need all the data in a variable thats all and by doing that i think i really need the data lenght

  4. #19
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    There is no way to guarantee all the data in one single packet. The only thing guaranteed by TCP/IP (unless you are using Datagrams) is the order. There could be packets which contain 1 single char or 5 chars or the like. Accumulation is the key, keep accumulating until you see that special char then send off the completed message. This is behavior that is specified in the TCP/IP manual(more or less anyway).

  5. #20
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    i see but how will i know when more data is awaiting to be put in the main buffer ? when received==0 ?

  6. #21
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    This is an asynchronous socket right? Or is this a blocking socket? I suppose I should have asked that before. If this is blocking (which is not all that helpful) then the recevived will tell you the length when its done with its blocking phase. If its asynch then it will trigger a windows message where you can check to see if more data has arrived.

  7. #22
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    to be honest i have no idea what it is lol i just used CAsyncSocket wsClient; and wsClient.Create();
    i guess it works at that way anyway i get all the data but i get extra characters check this out:
    Code:
    void CChateXDlg::OnReceive(int nErrorCode)
    {
        if(nErrorCode==0)
        {
            int rcv;
            char dta[100];
            CString txt;
            rcv=wsClient.Receive(dta,100,0);
            if(rcv==0)
            {
                Data+=dta;
            }
            else
            {
                Data=dta;
                m_re1.GetWindowText(txt);
                m_re1.SetWindowText(txt+CString("\n")+Data);
            }
        }
    }
    i used to get extra weird chars before too.. maybe i did something wrong in the code ?
    btw Data is declared outside this OnReceive function in the dialog.cpp file so it wont end

  8. #23
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by eXistenZ
    to be honest i have no idea what it is lol i just used CAsyncSocket wsClient; and wsClient.Create();
    i guess it works at that way anyway i get all the data but i get extra characters check this out:
    Code:
    void CChateXDlg::OnReceive(int nErrorCode)
    {
        if(nErrorCode==0)
        {
            int rcv;
            char dta[100];
            CString txt;
            rcv=wsClient.Receive(dta,100,0);
            if(rcv==0)
            {
                Data+=dta;
            }
            else
            {
                Data=dta;
                m_re1.GetWindowText(txt);
                m_re1.SetWindowText(txt+CString("\n")+Data);
            }
        }
    }
    i used to get extra weird chars before too.. maybe i did something wrong in the code ?
    btw Data is declared outside this OnReceive function in the dialog.cpp file so it wont end

    Ok so it looks like you are using async sockets... since well you are using the CasyncSocket class here. If you noticed "Data" after the first iteration is exactly 100 characters long. Its whatever you received + the rest of the "dta" buffer. You must NULL terminate the buffer when you receive so you can correctly add it to "Data". You know the number of characters received, since "rcv" has that information.

    So after this line...

    Code:
    rcv=wsClient.Receive(dta,100,0);
    add in this...

    Code:
    dta[ rcv ] = '\0';
    then you shouldn't be getting those funky characters...

    Sometimes it helps if you explain the whole problem first, explain what you are using, maybe your justification as to why and add in as much information as possible...


    Incidentally... this line is wrong...


    Code:
    if(rcv==0)
            {
                Data+=dta;
            }
    if rcv is 0 then check the WSAGetLastError() function you can do this by doing something like this...


    Code:
    if( rcv == 0 )
    {
          int error = WSAGetLastError( );
          //then find the error and print in out
          TRACE1("The error I got is %d", error);
           //or
           CString errormsg;
           errormsg.format("I found the error %d", error);
           AfxMessageBox(errormsg);
    }
    Last edited by dpro; 02-14-2005 at 02:16 PM.

  9. #24
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    thx alot man that did it yea i was thinking that since the its declared with 100 chars 50 may be used but what happens to the others :P so i guess that solves the problem
    edit: lol one sec last problem:
    Code:
    if(rcv==0)
    {
    Data+=dta;
    MessageBox("wee");
    }
    this code never fires, what value could rcv have when data is still in the queue without this i cannot know if its all the data or not
    edit2: :P sorry looks like you already said that more up if rcv==0 then its an error, k but how would i find out if the data is still in the queue or not?
    Last edited by eXistenZ; 02-14-2005 at 02:23 PM.

  10. #25
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by eXistenZ
    thx alot man that did it yea i was thinking that since the its declared with 100 chars 50 may be used but what happens to the others :P so i guess that solves the problem
    edit: lol one sec last problem:
    Code:
    if(rcv==0)
    {
    Data+=dta;
    MessageBox("wee");
    }
    this code never fires, what value could rcv have when data is still in the queue without this i cannot know if its all the data or not
    edit2: :P sorry looks like you already said that more up if rcv==0 then its an error, k but how would i find out if the data is still in the queue or not?

    If you get an error, you wont have any data in the buffer. If rcv is 0, you won't get any data, and dont worry about the buffer. If rcv > 0 then you have data....

    So basically rcv == 0, is an error.

    rcv > 0, that means you got data and then check the buffer.

  11. #26
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    yea but before i use that buffer i gotta put all data from the queue in it what if the server sends more than 100 bytes then it will split up and ill have to add it to the main buffer until its completed but err i wasn't thinking right at this problem how will i get the main buffer completed and when will i clear it.. more problems lol..
    i need the full buffer because i have important data there that should be all in 1 piece, bah all these problems.. if only i could just get the Data lenght of the whole buffer its weird there must be something to get it atleast in visual basic there was "TotalBytes as long" there and that helped very much
    Last edited by eXistenZ; 02-14-2005 at 03:01 PM.

  12. #27
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Quote Originally Posted by eXistenZ
    yea but before i use that buffer i gotta put all data from the queue in it what if the server sends more than 100 bytes then it will split up and ill have to add it to the main buffer until its completed but err i wasn't thinking right at this problem how will i get the main buffer completed and when will i clear it.. more problems lol..
    i need the full buffer because i have important data there that should be all in 1 piece, bah all these problems.. if only i could just get the Data lenght of the whole buffer its weird there must be something to get it atleast in visual basic there was "TotalBytes as long" there and that helped very much

    Its in the defintion of TCP/IP, it may not seem the best at times, but overall TCP/IP is very reliable. Basically the concept I am trying to point out is accumulation. Make it send a separator ";" or whatever so you know when you hit the end of the buffer. Just keep on getting information , once when you hit the separator, send out the message then clear it, you cannot do much more than that.

  13. #28
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >> if only i could just get the Data lenght of the whole buffer

    its not the size of the buffer you need, its the size of the MESSAGE.

    All the data (from one message) could arrive in one recv OR the data could be spread over a number of recv's.

    You have to wait until you get the whole message.


    In a chat program the packet header contains (at least)

    text length
    user name
    format (font colour ect)
    error check



    With a rich edit you don't have to add all the text again just set the cursor position to the last text.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  14. #29
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In a chat program the packet header contains (at least)

    text length
    user name
    format (font colour ect)
    error check
    Actually, you don't even need that much. Since it's a chat program, you can send null terminated strings, thus making it unnecessary to send text length. Formatting is optional, and dependent upon how pretty you want the chat program to be. Error checking is redundant since TCP has a built in checksum, and the ethernet frame does CRC.

    When I first started network programming, I made a simple chat program that had messages in the form of:
    <to>!<from>!<message>\0
    Where to is the user name the message is intended for, from is the user name of the message sender, and message is the actual message. In this sort of simple setup, user names cannot contain the exclamation character of course.

  15. #30
    Registered User
    Join Date
    Dec 2004
    Posts
    103
    Quote Originally Posted by novacain
    With a rich edit you don't have to add all the text again just set the cursor position to the last text.
    i totally agree but im a n00b in c++ to be honest and all i see there for adding text is SetWindowText() and this replaces all the text when calling it then adds the new text right ? and about that SetSel you said i have no idea how to use it and also have no idea how to format the text color, font etc..

    and as for the casyncsocket problem using a character to know the begining and the end of the message isnt such a good idea because some other guys could connect with a telnet client and send any data and this will probably cause errors + i will have to disable that character in my application and this isnt such a good thing either.. cant i use something else to know that begining and the end ?
    thanks
    Last edited by eXistenZ; 02-15-2005 at 06:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  2. char problem
    By ForlornOdium in forum C++ Programming
    Replies: 10
    Last Post: 10-29-2003, 02:39 PM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM