我也用了Create了,否则根本编译通不过,我的程序已经编译过了,只是执行的时候连接不上远程主机,错误信息是Server can not connected,但是服务器的确可以连接上呀,我用的也是IP,不是DNS名字呀。
在连接之前,请使用套接字(CSocket)初始化函数(AfxSocketInit()),便成功。
Nonzero if the function is successful; otherwise 0, and a specific error code can be retrieved by calling GetLastError. If this indicates an error code of WSAEWOULDBLOCK, and your application is using the overridable callbacks, your application will receive an OnConnect message when the connect operation is complete. The following errors apply to this member function: WSANOTINITIALISED A successful AfxSocketInit must occur before using this API. WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed. WSAEADDRINUSE The specified address is already in use. WSAEINPROGRESS A blocking Windows Sockets call is in progress. WSAEADDRNOTAVAIL The specified address is not available from the local machine. WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket. WSAECONNREFUSED The attempt to connect was rejected. WSAEDESTADDRREQ A destination address is required. WSAEFAULT The nSockAddrLen argument is incorrect. WSAEINVAL Invalid host address. WSAEISCONN The socket is already connected. WSAEMFILE No more file descriptors are available. WSAENETUNREACH The network cannot be reached from this host at this time. WSAENOBUFS No buffer space is available. The socket cannot be connected. WSAENOTSOCK The descriptor is not a socket. WSAETIMEDOUT Attempt to connect timed out without establishing a connection. WSAEWOULDBLOCK The socket is ed as nonblocking and the connection cannot be completed immediately.
CSocket只能工作在GUI thread中
如果是十进制表示的IP地址:202.38.64.1,那应该是可以的。
如果是域名:www.263.net,要做域名转化,查查DNS。 或者在你的子网里不允许连接外边的地址,与防火墙或网关有关。
如果没有选,和选了的工程有两点区别:
1。没有包含afxsock.h头文件
2。sock没有初始化,一个初始化sock的函数,不先运行的话可以创建,就不能连接了。
你用GetLastError()看看返回什么错误
WSAENETDOWN The Windows Sockets implementation detected that the network subsystem failed.
WSAEADDRINUSE The specified address is already in use.
WSAEINPROGRESS A blocking Windows Sockets call is in progress.
WSAEADDRNOTAVAIL The specified address is not available from the local machine.
WSAEAFNOSUPPORT Addresses in the specified family cannot be used with this socket.
WSAECONNREFUSED The attempt to connect was rejected.
WSAEDESTADDRREQ A destination address is required.
WSAEFAULT The nSockAddrLen argument is incorrect.
WSAEINVAL Invalid host address.
WSAEISCONN The socket is already connected.
WSAEMFILE No more file descriptors are available.
WSAENETUNREACH The network cannot be reached from this host at this time.
WSAENOBUFS No buffer space is available. The socket cannot be connected.
WSAENOTSOCK The descriptor is not a socket.
WSAETIMEDOUT Attempt to connect timed out without establishing a connection.
WSAEWOULDBLOCK The socket is ed as nonblocking and the connection cannot be completed immediately.
CSocket m_Server;
CString Host;
bool blnConnected;
Host = "某个服务器";
m_Server.Create(.....) '参数不太清楚
blnConnected = m_Server.Connect(Host,80);
--------> 添上去保证可以!!!!!!!!!!!