當我嘗試打印緩衝區時,我的應用程序崩潰。否則,它工作正常。 這是代碼:如何從使用winsock從套接字recv的函數返回char?
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;
}
}
main.cc
IRC client;
while (client.connected == true) {
char buffer = client.sockread();
if (buffer == NULL)
break;
printf ("Buffer: %s\n",buffer);
}
對不起,我不明白。那不是我現在正在做的事情嗎? – thorvald 2010-11-28 19:28:07
@thorvald:不,實際上你只返回第一個字符。查看我的編輯 – 2010-11-28 19:29:49