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. Send message

Send message

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpsysadmin
2 Posts 2 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

    I just new in visual c++ and have a copy of server and client source code. The result after I compile show Bytes recv: 21, but I want to change it so that I can send what I type. Can help me...Below is the client source code: #include "stdafx.h" #include "winsock2.h" void main() { // Initialize Winsock. WSADATA wsaData; int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); if ( iResult != NO_ERROR ) { printf("Error at WSAStartup()\n"); } // Create a socket. SOCKET m_socket; m_socket = socket( AF_INET, SOCK_STREAM, 0 ); if ( m_socket == INVALID_SOCKET ) { printf( "Error at socket(): %ld\n", WSAGetLastError() ); WSACleanup(); return; } // Connect to a server. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" ); clientService.sin_port = htons( 60000 ); if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) { printf( "Failed to connect.\n" ); WSACleanup(); return; } // 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 1 Reply Last reply
    0
    • U User 2159113

      I just new in visual c++ and have a copy of server and client source code. The result after I compile show Bytes recv: 21, but I want to change it so that I can send what I type. Can help me...Below is the client source code: #include "stdafx.h" #include "winsock2.h" void main() { // Initialize Winsock. WSADATA wsaData; int iResult = WSAStartup( MAKEWORD(2,2), &wsaData ); if ( iResult != NO_ERROR ) { printf("Error at WSAStartup()\n"); } // Create a socket. SOCKET m_socket; m_socket = socket( AF_INET, SOCK_STREAM, 0 ); if ( m_socket == INVALID_SOCKET ) { printf( "Error at socket(): %ld\n", WSAGetLastError() ); WSACleanup(); return; } // Connect to a server. sockaddr_in clientService; clientService.sin_family = AF_INET; clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" ); clientService.sin_port = htons( 60000 ); if ( connect( m_socket, (SOCKADDR*) &clientService, sizeof(clientService) ) == SOCKET_ERROR) { printf( "Failed to connect.\n" ); WSACleanup(); return; } // 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
      Christian Graus
      wrote on last edited by
      #2

      I'm at a loss why you keep pasting all of this code here. I'd also reiterate that it's obvious that you're using code when you have no idea what it does. Copy and paste without examining the code is no way to learn. TTjen wrote: char sendbuf[300] = "Client: Sending data."; char recvbuf[300] = ""; bytesSent = send( m_socket, sendbuf, lstrlen(sendbuf), 0 ); As I said yesterday, it doesn't matter how big or small you make sendbuf, lstrlen will return the length of the string you assigned to it, which is 21. The rest of the variable just never gets sent, because it's beyond the null value which causes lstrlen to ( correctly ) stop counting. You can set this string to be any value you like, up to 300 characters, and it will send it and give you back it's length. A better approach would be to use a string class like std::string or, if you're using MFC, CString. Both give you access to the underlying char * without having to allocate any memory, or impose a length limit. then you can assign the text you type to this variable, and pass the buffer in to the send method. Christian Graus - Microsoft MVP - C++

      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