当我尝试打印缓冲区时,我的应用程序崩溃了。否则,它工作正常。这是代码:
irc.h
class IRC
{
public:
void sockconnect(char * hName, int portNum);
void sockwrite(char* sendbuf);
char sockread(void);
bool connected;
private:
WSADATA wsaData;
SOCKET m_socket;
sockaddr_in clientService;
LPHOSTENT hostEntry;
};
irc.cc
char IRC::sockread(void)
{
int result;
char buffer[DEFAULT_BUFLEN];
result = recv(m_socket, buffer, DEFAULT_BUFLEN, 0);
if (result > 0) {
return *buffer;
}
else if (result == 0)
{
connected = false;
return *buffer;
}
else {
printf("recv failed with error: %d\n", WSAGetLastError());
return *buffer;
}
}
主文件
IRC client;
while (client.connected == true) {
char buffer = client.sockread();
if (buffer == NULL)
break;
printf ("Buffer: %s\n",buffer);
}