Reputation: 20068
I want to send a string "Hello there", but I only get "re". Why is that?
void Accept()
{
SOCKADDR_IN sock;
int intsock = sizeof(sock);
remoteSocket = ::accept(desc, (LPSOCKADDR)&sock, &intsock);
if(remoteSocket == -1)
{
cout << "Error in Accept()" << endl;
}
HandleConnection();
}
void HandleConnection()
{
cout << "You are connected !!!" << endl;
char* temp = new char[20];
Recv(temp);
cout << temp << endl;
}
void Send(const char* buffer)
{
if((::send(remoteSocket, buffer, strlen(buffer), 0)) < 0)
{
cout << "Error in Send()" << endl;
}
}
void Recv(char* buffer)
{
int n = 0;
while((n = ::recv(remoteSocket, buffer, strlen(buffer), 0)) 0)
{
buffer[n] = 0;
}
}
~Server()
{
WSACleanup();
}
};
int main()
{
Server s;
s.Initialize();
s.Socket();
s.Bind();
s.Listen();
while(1)
{
s.Accept();
}
return 0;
}
Upvotes: 1
Views: 16655
Reputation: 32240
Despite the destructor problem pointed by @Billy ONeal, you're doing recv()
in a loop, but each time you're overwriting the received buffer. I believe you want something like this:
// Pass buffer and its real size. This function takes care of NULL termination.
size_t Recv(char* buffer, size_t size) {
size_t total = 0, n = 0;
while((n = ::recv(remoteSocket, buffer+total, size-total-1, 0)) > 0) {
total += n;
}
buffer[total] = 0;
return total;
}
int main() {
char buffer[128];
// Connect or whatever (and set your global remoteSocket)
Recv(buffer, sizeof(buffer));
cout << buffer << endl;
return 0;
}
Upvotes: 6
Reputation: 753725
You need to specify how big the buffer that receives the data is - it is not strlen(buffer)
.
You can use sizeof(buffer)
if the buffer array is defined locally as an array (not in the parameter list), or if the buffer is a global or file scope array whose definition is visible in the function. Otherwise, you need to use an extra buffer size parameter that you pass to the Recv()
function - that is, if the buffer is defined in another function, or if it is dynamically allocated. (In the code, the array definition is not visible in Recv()
, so you need to ensure that Recv()
knows the size somehow - either as an explicit extra argument, or because you wrap the buffer up in an appropriate class that includes a method that tells you how much space is allocated to the buffer it holds.
Of course, the code as shown doesn't compile because buffer
isn't actually defined or declared anywhere.
Upvotes: 2
Reputation: 20312
I think the problem is this line of code while((n = ::recv(remoteSocket, buffer, strlen(buffer), 0)) 0)
You are using strlen(buffer)
to get the size of the buffer which is incorrect, you should be passing sizeof(buffer)
to your Recv
function.
If that is not the problem then it is one of the problems :P
Edit:
As pointed out by Kitsune and Mark, sizeof(buffer) would return 4 or 8 bytes since it is allocated on the heap and is simply a pointer to a block of memory. If you choose to use the stack (char buffer[20]
instead of new char[20]
), you could pass sizeof(buffer) to your Recv function. Otherwise, just use a hardcoded 20.
This is what your code should look like:
void HandleConnection()
{
cout << "You are connected !!!" << endl;
char temp[20]; // <-- now we have an array
Recv(temp, sizeof(temp)); // <-- sizeof(temp) will give us 20, not 4 anymore
cout << temp << endl;
}
Recv(char* buffer, size_t buffer_size)
{
recv(remoteSocket, buffer, buffer_size, 0);
}
Upvotes: 3