如何从使用 winsock 从套接字接收的函数中返回 char?

发布于 2024-10-05 01:24:30 字数 1356 浏览 7 评论 0原文

当我尝试打印缓冲区时,我的应用程序崩溃了。否则,它工作得很好。 这是代码:

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

   }

ma​​in.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心安伴我暖 2024-10-12 01:24:30

如果你想打印第一个字符,请使用

printf ("Buffer: %c\n",buffer);

如果你想打印整个,那么 sockread 应该返回整个缓冲区,而不是第一个字符。为此,您需要返回缓冲区第一个元素的地址,在这种情况下应该已经动态分配。

printf ("Buffer: %s\n",buffer);

编辑经过思考,我认为您希望后者通过以下方式更改 sockread() 函数:

  • 更改 的返回类型>charchar* 或更好的 const char*
  • char buffer[DEFAULT_BUFLEN];char* buffer = new char[DEFAULT_BUFLEN];
  • return *bufferreturn buffer

另外,在这种情况下不要忘记删除缓冲区

const char* buffer = client.sockread(); //not char buffer as in your code
printf ("Buffer: %s\n",buffer);
delete [] buffer;

hth

If you want to print the first character use

printf ("Buffer: %c\n",buffer);

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.

printf ("Buffer: %s\n",buffer);

Edit After thinking I think you want the latter for that change the sockread() function in the following way:

  • change the return type from char to char* or better const char*
  • char buffer[DEFAULT_BUFLEN]; to char* buffer = new char[DEFAULT_BUFLEN];
  • return *buffer to return buffer

Also, in this case don't forget to delete the buffer

const char* buffer = client.sockread(); //not char buffer as in your code
printf ("Buffer: %s\n",buffer);
delete [] buffer;

hth

巨坚强 2024-10-12 01:24:30

您需要使用 std::string。您无法返回该缓冲区 - 它位于本地堆栈上。即使您成功返回指向它的实际指针而不是仅返回单个字符(这就是您所做的),那么它也会超出范围并且其中的数据无效。

std::string 可以解决所有这些问题。你只要使用它,就完成了。您还存在其他问题,例如当实际失败时缓冲区的返回无效。这就是例外的用途。

std::string IRC::sockread()
{
    std::string s;
    s.resize(DEFAULT_BUFLEN);
    int result = recv(m_socket, &s[0], DEFAULT_BUFLEN, 0);

    if (result > 0) {
        return s;
    } else if (result == 0) {
        connected = false;
    } else {
        std::cout << "recv failed with error " << WSAGetLastError() << "\n";
    }
    throw std::runtime_error("Socket connection failed!");
}

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.

std::string IRC::sockread()
{
    std::string s;
    s.resize(DEFAULT_BUFLEN);
    int result = recv(m_socket, &s[0], DEFAULT_BUFLEN, 0);

    if (result > 0) {
        return s;
    } else if (result == 0) {
        connected = false;
    } else {
        std::cout << "recv failed with error " << WSAGetLastError() << "\n";
    }
    throw std::runtime_error("Socket connection failed!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文