如何从使用 winsock 从套接字接收的函数中返回 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);
}
My application is crashing when I try printing the buffer. Otherwise, it works fine.
This is the code:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想打印第一个字符,请使用
如果你想打印整个,那么 sockread 应该返回整个缓冲区,而不是第一个字符。为此,您需要返回缓冲区第一个元素的地址,在这种情况下应该已经动态分配。
编辑经过思考,我认为您希望后者通过以下方式更改
sockread()
函数:的返回类型>char
到char*
或更好的const char*
char buffer[DEFAULT_BUFLEN];
到char* buffer = new char[DEFAULT_BUFLEN];
return *buffer
到return buffer
另外,在这种情况下不要忘记删除缓冲区
hth
If you want to print the first character use
If you want to print the whole then sockread should return the whole buffer, not the first character. For that you will need to return the address of the first element of the buffer which in this case should already be dynamically allocated.
Edit After thinking I think you want the latter for that change the
sockread()
function in the following way:char
tochar*
or betterconst char*
char buffer[DEFAULT_BUFLEN];
tochar* buffer = new char[DEFAULT_BUFLEN];
return *buffer
toreturn buffer
Also, in this case don't forget to delete the buffer
hth
您需要使用 std::string。您无法返回该缓冲区 - 它位于本地堆栈上。即使您成功返回指向它的实际指针而不是仅返回单个字符(这就是您所做的),那么它也会超出范围并且其中的数据无效。
std::string 可以解决所有这些问题。你只要使用它,就完成了。您还存在其他问题,例如当实际失败时缓冲区的返回无效。这就是例外的用途。
You need to use std::string. You can't return that buffer- it's on the local stack. Even if you succeeded in returning an actual pointer to it instead of just a single character, which is what you have done, then it would be out of scope and the data within it invalid.
std::string takes care of all of these problems. You just use it, and it's done. You have other problems too, like invalid returning of a buffer when actually it's failed. This is what exceptions are for.