Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;winsock客户端无法使用主机IP的文本框进行连接_C++_Textbox_Ip Address_Winsock_Connect - Fatal编程技术网

C++ c++;winsock客户端无法使用主机IP的文本框进行连接

C++ c++;winsock客户端无法使用主机IP的文本框进行连接,c++,textbox,ip-address,winsock,connect,C++,Textbox,Ip Address,Winsock,Connect,当我从文本框使用主机IP时,我无法让它连接到我的服务器。 请参阅我的代码: char *bufhost; int bufhostlen; bufhostlen = GetWindowTextLength(hwndTextBox_ip) + 1; GetWindowText(hwndTextBox_ip, bufhost, bufhostlen);

当我从文本框使用主机IP时,我无法让它连接到我的服务器。 请参阅我的代码:

                    char *bufhost;
                int bufhostlen;
                bufhostlen = GetWindowTextLength(hwndTextBox_ip) + 1;
                GetWindowText(hwndTextBox_ip, bufhost, bufhostlen);
                sockaddr_in sin;
                sin.sin_family=AF_INET;
                sin.sin_port=htons(5060);
                sin.sin_addr.s_addr=inet_addr(bufhost);
                connect(sock,(LPSOCKADDR)(&sin),sizeof(sin));
如果我使用

sin.sin_addr.s_addr=inet_addr("127.0.0.1");
它连接起来没有问题

我真的不知道如何得到这份工作(搜索了几个小时…) 谢谢你的帮助:-)

解决方案:

正如PermanentGuest告诉我的,我必须为缓冲区分配内存:

                    char *bufhost;
                int bufhostlen;
                bufhostlen = GetWindowTextLength(hwndTextBox_ip) + 1;
                bufhost = (char*) malloc(bufhostlen);
                GetWindowText(hwndTextBox_ip, bufhost, bufhostlen);
                sockaddr_in sin;
                sin.sin_family=AF_INET;
                sin.sin_port=htons(5060);
                sin.sin_addr.s_addr=inet_addr(bufhost);
                connect(sock,(LPSOCKADDR)(&sin),sizeof(sin));

您需要为bufhost分配内存。不幸的是,的文档没有明确提到这一点。但是,对于所有win32 API,这是常见的行为