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. buffer filled with trash [modified]

buffer filled with trash [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
3 Posts 3 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.
  • A Offline
    A Offline
    ALLERSLIT
    wrote on last edited by
    #1

    Hey I googled for this kinda stuff but only useless stuff came up.. Anyway

    char buffer\[250\];
    send(sock, "hello?\\n", 8, 0);
    recv(sock, buffer, sizeof(buffer), 0);
    cout << buffer << endl;
    cin.get();
    

    is my code. Now I obviously do not know how big the buffer will be, the problem is if i set buffer[250], everything after the actual content is trash. How would I fix that? Like I wanna see only the actual content and not the jibberish after it. Thanks. e// nevermind, I just found out that recv returns the size..

    modified on Tuesday, November 16, 2010 7:57 PM

    S 1 Reply Last reply
    0
    • A ALLERSLIT

      Hey I googled for this kinda stuff but only useless stuff came up.. Anyway

      char buffer\[250\];
      send(sock, "hello?\\n", 8, 0);
      recv(sock, buffer, sizeof(buffer), 0);
      cout << buffer << endl;
      cin.get();
      

      is my code. Now I obviously do not know how big the buffer will be, the problem is if i set buffer[250], everything after the actual content is trash. How would I fix that? Like I wanna see only the actual content and not the jibberish after it. Thanks. e// nevermind, I just found out that recv returns the size..

      modified on Tuesday, November 16, 2010 7:57 PM

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      The documentation for recv[^] shows the prototype looks something like this:

      int recv(
      __in SOCKET s,
      __out char *buf,
      __in int len,
      __in int flags
      );

      It describes the return value like this:

      If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero.   Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

      So what you have to do is add a NULL terminator and where you add it depends on how much data you recieved:

      char buffer[250];
      char hello[] = "hello?\n";
      send(sock, hello, sizeof(hello), 0);
      int res = recv(sock, buffer, sizeof(buffer)-1, 0); // -1 so we'll always have room for NULL terminator.
      if (res != SOCKET_ERROR)
      {
          buffer[res] = 0; // NULL terminate.
          cout << buffer << endl;
      }
      cin.get();

      Steve

      L 1 Reply Last reply
      0
      • S Stephen Hewitt

        The documentation for recv[^] shows the prototype looks something like this:

        int recv(
        __in SOCKET s,
        __out char *buf,
        __in int len,
        __in int flags
        );

        It describes the return value like this:

        If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero.   Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

        So what you have to do is add a NULL terminator and where you add it depends on how much data you recieved:

        char buffer[250];
        char hello[] = "hello?\n";
        send(sock, hello, sizeof(hello), 0);
        int res = recv(sock, buffer, sizeof(buffer)-1, 0); // -1 so we'll always have room for NULL terminator.
        if (res != SOCKET_ERROR)
        {
            buffer[res] = 0; // NULL terminate.
            cout << buffer << endl;
        }
        cin.get();

        Steve

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

        Well put together answer; an example to us all.

        Just say 'NO' to evaluated arguments for diadic functions! Ash

        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