Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Unlimited Socket receive

Unlimited Socket receive

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kristian33
    wrote on last edited by
    #1

    I am making server socket with CAsyncSocket. But I don't get how to unlimited buffer for receive messages. I don't know data length before I get it, and I don't want to create constant Huge buffer, but just buffer sized data length. This migth be simple question, but i don't know how to do it.

    L J A 4 Replies Last reply
    0
    • K Kristian33

      I am making server socket with CAsyncSocket. But I don't get how to unlimited buffer for receive messages. I don't know data length before I get it, and I don't want to create constant Huge buffer, but just buffer sized data length. This migth be simple question, but i don't know how to do it.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Hi,,, i have been writing a chat program both server side and client side by using MFC CAsyncSocket Class and i had succeded to implement and chat on our LAN at job. It is really a good experience for me in both learning C++ and Visual C6.0 media. i have just wanted to consult ur precious knowledge about something i could not implement in my server side App. Let me talk about a little bit about how Apps work: İn My Server App, i create a Server Socket and make it listen for connection requests from clients. Clients ("these are my job friends.:)..") make connections using my client App to my server App. so, i really know what happens at both side. When people connect to server, all they can talk to the same room and everybody sees what others said and they can whisper special messages to special users if they want like ordinary chat programs as well. Server handles every user information ( user name, IP_Address, OnOff time, Incoming Messages...etc,) in a structure that is a linked list created dynamically when each user connects and disconnects. Server adds and deletes all users to a dynamically created Socket Linked List. i also thought to try to create individual threads for each user connecting but since Linked list worked i gave up. What is my problem is that. When someone writes a message while in room chat, Server takes this message from that user and sends it back to all users that are online ONE BY ONE. By this way everybody sees the written message in their Client App. It is not efficient for Server Processing time to distribute one message to all users one by one. It would be efficient for Server App to SEND BROADCAST messages to all users at one time. But since i don't use Windows Sockets but MFC i could not find my way. Is it possible to send BROADCAST message while my server Socket and Client sockets are STREAM_SOCKET type ( i created my sockets from that type) or is BROADCASTING a subject of only DATAGRAM_SOCKET type. i don't know what will i do. Should i change my Application structure or is there another way that solves my problem. i really think the future when lots of people connects and sends messages simultaneously. Server also handles a database that keeps special user information ( name, age, gender, height, weight, email adds...etc) and adds deletes and updates and sends them back whenever a user wants to see his/her user details like on ICQ. So for my server App, dealing with all these tasks takes time and i want to use my Server Processing as efficient as poss

      1 Reply Last reply
      0
      • K Kristian33

        I am making server socket with CAsyncSocket. But I don't get how to unlimited buffer for receive messages. I don't know data length before I get it, and I don't want to create constant Huge buffer, but just buffer sized data length. This migth be simple question, but i don't know how to do it.

        J Offline
        J Offline
        Jon Hulatt
        wrote on last edited by
        #3

        If you work like this:-

        BYTE buff[4096];

        int nRead;
        nRead = Receive(buff, 4096);

        you will read up to 4096 bytes. Then process those 4096 bytes, and try to read again... eg:

        // CFile f is an open CFile object
        BYTE buff[4096];
        int nRead=0;

        do {
        nRead = Receive(buff, 4096);
        f.Write(buff,nRead);
        } while (nRead == 4096);

        that will get all data waiting on the socket and write it to a file. Sorry to dissapoint you all with my lack of a witty or poignant signature.

        1 Reply Last reply
        0
        • K Kristian33

          I am making server socket with CAsyncSocket. But I don't get how to unlimited buffer for receive messages. I don't know data length before I get it, and I don't want to create constant Huge buffer, but just buffer sized data length. This migth be simple question, but i don't know how to do it.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          or read a byte at a time

          1 Reply Last reply
          0
          • K Kristian33

            I am making server socket with CAsyncSocket. But I don't get how to unlimited buffer for receive messages. I don't know data length before I get it, and I don't want to create constant Huge buffer, but just buffer sized data length. This migth be simple question, but i don't know how to do it.

            A Offline
            A Offline
            Albert Pascual
            wrote on last edited by
            #5

            What about a simple way like that: Check for the size of the buffer return, if it is as big as your buffer, read the socket again and append it at the end of the buffer, keep doing that until the size returned is smaller than your buffer. That's the end of the packet! Take a look at the code below: unsigned char *BigBuff = NULL; unsigned char buf[4096]; ULONG size = 4096; ULONG rc = 0; BOOL bMoreData = false; ULONG TotalSize = 0; do { bMoreData = true; memset(&buf[0], 0, 4096); rc = TcpRecv(SocAccepted, buf, size); if ( rc == 0 ) { goto end; } if ( rc == (ULONG)SOCKET_ERROR ) goto end; TotalSize = TotalSize + rc; if ( BigBuff == NULL ) { BigBuff = (unsigned char *) malloc (rc+10); if ( !BigBuff ) return NULL; memcpy(BigBuff, buf, rc); } else { unsigned char *p = NULL; p = (unsigned char *)realloc(BigBuff, TotalSize); if ( !p ) { free( BigBuff ); return NULL; } BigBuff = p; memcpy(&BigBuff[TotalSize-rc], buf, rc); } if ( rc == size ) bMoreData = true; }while(bMoreData); end: return (&BigBuff[0]);

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups