Send message
-
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; }
-
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; }
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++