stringsend?
-
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;
-
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;
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?
-
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?
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?
-
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?
Replace:
if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); }
by:if (bytesRecv < 0) return; printf( "String Recv: %s\n", recvbuf); }
-
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;
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.
-
Replace:
if (bytesRecv < 0) return; printf( "Bytes Recv: %ld\n", bytesRecv ); }
by:if (bytesRecv < 0) return; printf( "String Recv: %s\n", recvbuf); }
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.