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;
}
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: