server and client
-
I have a server and client source code and I not very understand the send and receive part of the program. After I execute the program, it show Bytes send: 21. I don't know why it is 21. Can somebody tell me? Below is the code for the server: #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; } // Bind the socket. sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr( "127.0.0.1" ); service.sin_port = htons( 60000 ); if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) { printf( "bind() failed.\n" ); closesocket(m_socket); return; } // Listen on the socket. if ( listen( m_socket, 1 ) == SOCKET_ERROR ) { printf( "Error listening on socket.\n"); } // Accept connections. SOCKET AcceptSocket; printf( "Waiting for a client to connect...\n" ); while (1) { AcceptSocket = SOCKET_ERROR; while ( AcceptSocket == SOCKET_ERROR ) { AcceptSocket = accept( m_socket, NULL, NULL ); } printf( "Client Connected.\n"); m_socket = AcceptSocket; break; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; char sendbuf[32] = "Server: Sending Data."; char recvbuf[32] = ""; bytesRecv = recv( m_socket, recvbuf, 32, 0 ); printf( "Bytes Recv: %ld\n", bytesRecv ); bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); return; }
-
I have a server and client source code and I not very understand the send and receive part of the program. After I execute the program, it show Bytes send: 21. I don't know why it is 21. Can somebody tell me? Below is the code for the server: #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; } // Bind the socket. sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr( "127.0.0.1" ); service.sin_port = htons( 60000 ); if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) { printf( "bind() failed.\n" ); closesocket(m_socket); return; } // Listen on the socket. if ( listen( m_socket, 1 ) == SOCKET_ERROR ) { printf( "Error listening on socket.\n"); } // Accept connections. SOCKET AcceptSocket; printf( "Waiting for a client to connect...\n" ); while (1) { AcceptSocket = SOCKET_ERROR; while ( AcceptSocket == SOCKET_ERROR ) { AcceptSocket = accept( m_socket, NULL, NULL ); } printf( "Client Connected.\n"); m_socket = AcceptSocket; break; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; char sendbuf[32] = "Server: Sending Data."; char recvbuf[32] = ""; bytesRecv = recv( m_socket, recvbuf, 32, 0 ); printf( "Bytes Recv: %ld\n", bytesRecv ); bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); return; }
( sorry, I had two windows open, typed in the wrong one ). Because the string you're passing in is that length, and you're passing in the result of strlen, which gives you that value. I get the feeling you copied and pasted this from somewhere and have no real idea what it does, or even how C++ string handling works. If this is true, stop worrying about what you can cut and paste, and concentrate on learning the fundamentals of the language. Christian Graus - Microsoft MVP - C++ -- modified at 22:04 Monday 5th September, 2005
-
I have a server and client source code and I not very understand the send and receive part of the program. After I execute the program, it show Bytes send: 21. I don't know why it is 21. Can somebody tell me? Below is the code for the server: #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; } // Bind the socket. sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr( "127.0.0.1" ); service.sin_port = htons( 60000 ); if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) { printf( "bind() failed.\n" ); closesocket(m_socket); return; } // Listen on the socket. if ( listen( m_socket, 1 ) == SOCKET_ERROR ) { printf( "Error listening on socket.\n"); } // Accept connections. SOCKET AcceptSocket; printf( "Waiting for a client to connect...\n" ); while (1) { AcceptSocket = SOCKET_ERROR; while ( AcceptSocket == SOCKET_ERROR ) { AcceptSocket = accept( m_socket, NULL, NULL ); } printf( "Client Connected.\n"); m_socket = AcceptSocket; break; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; char sendbuf[32] = "Server: Sending Data."; char recvbuf[32] = ""; bytesRecv = recv( m_socket, recvbuf, 32, 0 ); printf( "Bytes Recv: %ld\n", bytesRecv ); bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); return; }
-
I have a server and client source code and I not very understand the send and receive part of the program. After I execute the program, it show Bytes send: 21. I don't know why it is 21. Can somebody tell me? Below is the code for the server: #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; } // Bind the socket. sockaddr_in service; service.sin_family = AF_INET; service.sin_addr.s_addr = inet_addr( "127.0.0.1" ); service.sin_port = htons( 60000 ); if ( bind( m_socket, (SOCKADDR*) &service, sizeof(service) ) == SOCKET_ERROR ) { printf( "bind() failed.\n" ); closesocket(m_socket); return; } // Listen on the socket. if ( listen( m_socket, 1 ) == SOCKET_ERROR ) { printf( "Error listening on socket.\n"); } // Accept connections. SOCKET AcceptSocket; printf( "Waiting for a client to connect...\n" ); while (1) { AcceptSocket = SOCKET_ERROR; while ( AcceptSocket == SOCKET_ERROR ) { AcceptSocket = accept( m_socket, NULL, NULL ); } printf( "Client Connected.\n"); m_socket = AcceptSocket; break; } // Send and receive data. int bytesSent; int bytesRecv = SOCKET_ERROR; char sendbuf[32] = "Server: Sending Data."; char recvbuf[32] = ""; bytesRecv = recv( m_socket, recvbuf, 32, 0 ); printf( "Bytes Recv: %ld\n", bytesRecv ); bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 ); printf( "Bytes Sent: %ld\n", bytesSent ); return; }
-
( sorry, I had two windows open, typed in the wrong one ). Because the string you're passing in is that length, and you're passing in the result of strlen, which gives you that value. I get the feeling you copied and pasted this from somewhere and have no real idea what it does, or even how C++ string handling works. If this is true, stop worrying about what you can cut and paste, and concentrate on learning the fundamentals of the language. Christian Graus - Microsoft MVP - C++ -- modified at 22:04 Monday 5th September, 2005
Christian Graus wrote: Because the string you're passing in is that length, and you're passing in the result of strlen, which gives you that value. The length of the string he is sending is 21 (32 is the size of the array), I think that's why he is confsued when send returns 32? -- modified at 7:19 Tuesday 6th September, 2005 oh forget it, he really wonders why the string length is 21. :)