buffer filled with trash [modified]
-
Hey I googled for this kinda stuff but only useless stuff came up.. Anyway
char buffer\[250\]; send(sock, "hello?\\n", 8, 0); recv(sock, buffer, sizeof(buffer), 0); cout << buffer << endl; cin.get();
is my code. Now I obviously do not know how big the buffer will be, the problem is if i set buffer[250], everything after the actual content is trash. How would I fix that? Like I wanna see only the actual content and not the jibberish after it. Thanks. e// nevermind, I just found out that recv returns the size..
modified on Tuesday, November 16, 2010 7:57 PM
-
Hey I googled for this kinda stuff but only useless stuff came up.. Anyway
char buffer\[250\]; send(sock, "hello?\\n", 8, 0); recv(sock, buffer, sizeof(buffer), 0); cout << buffer << endl; cin.get();
is my code. Now I obviously do not know how big the buffer will be, the problem is if i set buffer[250], everything after the actual content is trash. How would I fix that? Like I wanna see only the actual content and not the jibberish after it. Thanks. e// nevermind, I just found out that recv returns the size..
modified on Tuesday, November 16, 2010 7:57 PM
The documentation for recv[^] shows the prototype looks something like this:
int recv(
__in SOCKET s,
__out char *buf,
__in int len,
__in int flags
);It describes the return value like this:
If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
So what you have to do is add a
NULL
terminator and where you add it depends on how much data you recieved:char buffer[250];
char hello[] = "hello?\n";
send(sock, hello, sizeof(hello), 0);
int res = recv(sock, buffer, sizeof(buffer)-1, 0); // -1 so we'll always have room for NULL terminator.
if (res != SOCKET_ERROR)
{
buffer[res] = 0; // NULL terminate.
cout << buffer << endl;
}
cin.get();Steve
-
The documentation for recv[^] shows the prototype looks something like this:
int recv(
__in SOCKET s,
__out char *buf,
__in int len,
__in int flags
);It describes the return value like this:
If no error occurs, recv returns the number of bytes received and the buffer pointed to by the buf parameter will contain this data received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.
So what you have to do is add a
NULL
terminator and where you add it depends on how much data you recieved:char buffer[250];
char hello[] = "hello?\n";
send(sock, hello, sizeof(hello), 0);
int res = recv(sock, buffer, sizeof(buffer)-1, 0); // -1 so we'll always have room for NULL terminator.
if (res != SOCKET_ERROR)
{
buffer[res] = 0; // NULL terminate.
cout << buffer << endl;
}
cin.get();Steve