sock_stream

This is a discussion on sock_stream within the Networking/Device Communication forums, part of the General Programming Boards category; I must admit i am lost, or at least confused. When i am using TCP with sock_stream the protocol does ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    10

    sock_stream

    I must admit i am lost, or at least confused.

    When i am using TCP with sock_stream the protocol does not limit the messages (thats what i understood) so if i send a stream with let say

    [int][int] [[int][int][int]]*

    where the first would be the type of message, the second would be the size of the last "chunk", hipoteticaly a string converted in a array of ints.

    so lets say the message is:

    Code:
    //one side
    
    int msg[] = {101,3,123,321,123};
    
    if(send(sock,msg,sizeof(int)*5,0)<0)ERR("Error while sending...\n");
    now my question is, on the other side, since this is a stream, can i do:

    Code:
    int type;
    
    if(recv(sock,&type,sizeof(int), 0)<0)ERR("Dint recieve type of message...\n");
    
    if(type==101)somefunction();
    
    void somefunctio(){
    int size,*data;
    
    data=(int*)malloc(sizeof(int));
    
    
    if(recv(sock,&size,sizeof(int), 0)<0)ERR("Dint recieve size of data...\n");
    
    data=(int*)realloc(data,sizeof(int)*size);
    
    if(recv(sock,data,sizeof(int)*size,0)<o)ERR("dint recieve data...\n");
    }
    so can i use lots of recv to subdivide the info i sent? like on the exemple above, i send 1 message but use 3 recv to treat it, one for the type, one for the size and one to get the data (since now i know the size).

    If this is not possible is there another way to do it? i cant send alot of messages so i would need to use something like describe above.


    Thanks
    Rykaerdoe <(^.^<)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > so can i use lots of recv to subdivide the info i sent?
    Yes you can do that.

    Not only that, the system may fragment the message anyway, so you should be prepared for that in all your recv calls.
    The return result of recv() indicates how many bytes have been received, so use this to determine whether you've received a whole message or not.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21