经过跟踪,发现是getprotobyname()总出错,通过查msdn是因为没有调用WSAStartup(),加上注释部分就可以了。
难道还要手动的WSAStartup()?代码的问题在哪儿?
CODE:
#include "ace/INET_Addr.h"
#include "ace/SOCK_Connector.h"
#include "ace/SOCK_Stream.h"
#include <iostream>
using namespace std;
int main (int argc, char *argv[])
{
// WORD wVersionRequested;
// WSADATA wsaData;
// int err;
// wVersionRequested = MAKEWORD( 2, 2 );
// err = WSAStartup( wVersionRequested, &wsaData );
// if ( err != 0 ) {
// return 2;
// }
const char *pathname =
argc > 1 ? argv[1] : "index.html";
const char *server_hostname =
argc > 2 ? argv[2] : "ace.ece.uci.edu";
ACE_SOCK_Connector connector;
ACE_SOCK_Stream peer;
ACE_INET_Addr peer_addr;
if (peer_addr.set (80, server_hostname) == -1)
{
cout << "addr set error" << endl;
return 1;
}
else if (connector.connect (peer, peer_addr) == -1)
{
cout << "connect error" << endl;
return 1;
}
else
{
cout << "success" << endl;
}
return 0;
}请高手指教,谢谢。

最新回复
D:\work\ace\ACE_wrappers\examples\C++NPv1\C++NPv1_Logging_Client.vcproj
ace 下面的例子程序,你看他都不要这样,要这样,还要 ace 搞什么???
第一,用int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) 代替你的main即可。
这里面已经自动初始化了ACE。
第二,使用ACE::init()和ACE::fini()。前者在使用任何ACE功能前声明,后者在程序结束时候调用。
我试了一下
使用ACE::init()和ACE::fini(),是可以的
用int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) ,总是有其他错误。
e:\ace\test\test1\test1\main.cpp(29) : error C2446: ':' : no conversion from 'const char *' to 'ACE_TCHAR *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
查看ACE源码,ACE_TCHAR就是char。直接用char代替又会出现连接错误
main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl ace_os_wmain_i(class ACE_Main_Base &,int,wchar_t * * const)" (__imp_?ace_os_wmain_i@@YAHAAVACE_Main_Base@@HQAPA_W@Z) referenced in function _wmain
main.obj : error LNK2019: unresolved external symbol "int __cdecl ace_wmain_i(int,wchar_t * * const)" (?ace_wmain_i@@YAHHQAPA_W@Z) referenced in function "private: virtual int __thiscall ACE_Main::run_i(int,wchar_t * * const)" (?run_i@ACE_Main@@EAEHHQAPA_W@Z)
查看ACE源码,ACE_TMAIN就是main,可是怎么会出现ace_os_wmain_i的连接错误?
另外,hzhxxx说的C++NPv1_Logging_Client.vcproj里好像也没有用上述方法。
1、是动态链接还是静态链接ACE
2、是否填入了正确的链接库
3、是否是UNICODE
你看看你的工程就知道了。
可是,看ACE的代码,根据编译前的设置,int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) 和int main(...)是一样的啊
而且C++NPv1_Logging_Client.vcproj里也没有用上述方法,好像是可以的。
For use cases in which you are writing a regular C++ application that has the standard int main (int argc, char *argv[]) entry point or the wide-character-enabled ACE_TMAIN entry point shown in Section 1.7, ACE magically redefines your main() function and inserts its own, which instantiates the Object Manager on the runtime stack and then calls your main() function. When your main() function returns, the Object Manager is run down as well.
但是他是如何实现“magically redefines”的呢?
“wide-character-enabled ACE_TMAIN entry point”,那有没有unicode-enabled的入口呢?
如果在 ace中的config.h中定义了
#define ACE_USE_WCHAR
则编译为支持unicode的ace版本