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. stringsend?

stringsend?

Scheduled Pinned Locked Moved C / C++ / MFC
sysadmintutorialquestion
6 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.
  • U Offline
    U Offline
    User 2159113
    wrote on last edited by
    #1

    If I don't want the length of the string to be sent but I just want the sting that I type send to server. How to I change the code? Bytesend to strsend? how to change the lstrlen?:confused: // Send and receive data. long bytesSent; long bytesRecv = SOCKET_ERROR; char sendbuf[300] = "Client: Sending data."; char recvbuf[300] = ""; bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, recvbuf, 300, 0 ); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { printf( "Connection Closed.\n"); break; } if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); } return;

    C K 2 Replies Last reply
    0
    • U User 2159113

      If I don't want the length of the string to be sent but I just want the sting that I type send to server. How to I change the code? Bytesend to strsend? how to change the lstrlen?:confused: // Send and receive data. long bytesSent; long bytesRecv = SOCKET_ERROR; char sendbuf[300] = "Client: Sending data."; char recvbuf[300] = ""; bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, recvbuf, 300, 0 ); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { printf( "Connection Closed.\n"); break; } if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); } return;

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      What's your problem ? For me, this code looks fine (except that I don't know what send, recv, ... do). Your send function needs to know how many bytes to send so you have to specify the number of bytes in your string. Is there a problem with that?

      U 1 Reply Last reply
      0
      • C Cedric Moonen

        What's your problem ? For me, this code looks fine (except that I don't know what send, recv, ... do). Your send function needs to know how many bytes to send so you have to specify the number of bytes in your string. Is there a problem with that?

        U Offline
        U Offline
        User 2159113
        wrote on last edited by
        #3

        The code don't have any problem. After I compile, it will show bytesrecv: 21 which is the length of the string. I don't want the length of the string to print out but is the string itself print out. how do I program it?

        C 1 Reply Last reply
        0
        • U User 2159113

          The code don't have any problem. After I compile, it will show bytesrecv: 21 which is the length of the string. I don't want the length of the string to print out but is the string itself print out. how do I program it?

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          Replace: if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); } by: if (bytesRecv < 0) return; printf( "String Recv: %s\n", recvbuf); }

          U 1 Reply Last reply
          0
          • U User 2159113

            If I don't want the length of the string to be sent but I just want the sting that I type send to server. How to I change the code? Bytesend to strsend? how to change the lstrlen?:confused: // Send and receive data. long bytesSent; long bytesRecv = SOCKET_ERROR; char sendbuf[300] = "Client: Sending data."; char recvbuf[300] = ""; bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, recvbuf, 300, 0 ); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { printf( "Connection Closed.\n"); break; } if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); } return;

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            It's possible that you have a problem with this line: bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 ); Since you use the length of sendbuf, you won't transmit the terminating NULL-byte of sendbuf. This could present a problem in the receiving end. If you wan't the original data (a char array) in you have got two options: 1. Send the terminating NULL-character by doing this: bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf) + 1, 0 ); 2. At the receiving end, add a '\0' in the receive buffer at offset . For instance: bytesRecv = recv( m_socket, recvbuf, 300, 0 ); recvbuf[bytesRecv] = '\0'; Else, the code looks fine to me.

            1 Reply Last reply
            0
            • C Cedric Moonen

              Replace: if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); } by: if (bytesRecv < 0) return; printf( "String Recv: %s\n", recvbuf); }

              U Offline
              U Offline
              User 2159113
              wrote on last edited by
              #6

              Its work! How do I change it so that I can type on the dos and send it. The code now is when I type what I want to send in this: char sendbuf[300] = " "; and after I compile, it will automatically send to the server. But what I want is when I compile it, I can type on it and then send it to the server.

              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