为什么我用CSocket来进行连接都连不上

talentboy 2001-12-19 09:42:39
我的用法如下:
CSocket m_Server;
CString Host;
bool blnConnected;
Host = "某个服务器";
blnConnected = m_Server.Connect(Host,80);

这个服务器的80端口的确可以访问的,但是为什么blnConnected总返回false;
...全文
184 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
love_phy 2001-12-20
  • 打赏
  • 举报
回复
没初始化嘛
solomon 2001-12-20
  • 打赏
  • 举报
回复
少写了一句话!!!!!!!!!!!!
CSocket m_Server;
CString Host;
bool blnConnected;
Host = "某个服务器";
m_Server.Create(.....) '参数不太清楚
blnConnected = m_Server.Connect(Host,80);
--------> 添上去保证可以!!!!!!!!!!!
mengxihe 2001-12-20
  • 打赏
  • 举报
回复
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 marked as nonblocking and the connection cannot be completed immediately.
Developerljm 2001-12-20
  • 打赏
  • 举报
回复
老兄,请给分吧?
Developerljm 2001-12-20
  • 打赏
  • 举报
回复
在连接之前,请使用套接字(CSocket)初始化函数(AfxSocketInit()),便成功。
talentboy 2001-12-20
  • 打赏
  • 举报
回复
我也用了Create了,否则根本编译通不过,我的程序已经编译过了,只是执行的时候连接不上远程主机,错误信息是Server can not connected,但是服务器的确可以连接上呀,我用的也是IP,不是DNS名字呀。
zzh 2001-12-20
  • 打赏
  • 举报
回复
先PING一下试试能不能拼通,然后再调用GetLastError看返回值是什么。
luckyegg 2001-12-20
  • 打赏
  • 举报
回复
如果Host就是"某个服务器",那肯定连不上了,应是目的机的IP。还有,目的机也有可能对连接有限制,你可以用返回值看看,到底是什么原因呀。
kamphkb 2001-12-19
  • 打赏
  • 举报
回复
必须先不带参数的调用Create(),然后Connect()。俺最近正在用CSocket。详情看msdn
tiyuan 2001-12-19
  • 打赏
  • 举报
回复
我只用过CAsyncSocket,没用过CSocket,如果是CAsyncSocket必须先Create,否则会返回WSAENOTSOCKET
你用GetLastError()看看返回什么错误
David_lee 2001-12-19
  • 打赏
  • 举报
回复
不好意思,原来楼上的楼上说过了 :)
David_lee 2001-12-19
  • 打赏
  • 举报
回复
有可能是这样的。在wizzard里面有一项,需要socket支持吗?
如果没有选,和选了的工程有两点区别:
1。没有包含afxsock.h头文件
2。sock没有初始化,一个初始化sock的函数,不先运行的话可以创建,就不能连接了。
fzheng 2001-12-19
  • 打赏
  • 举报
回复
你的HOST里面填的是什么东西呀?
如果是十进制表示的IP地址:202.38.64.1,那应该是可以的。
如果是域名:www.263.net,要做域名转化,查查DNS。

或者在你的子网里不允许连接外边的地址,与防火墙或网关有关。
masterz 2001-12-19
  • 打赏
  • 举报
回复
是在GUI thread中吗?AfxSocketInit调用过吗
CSocket只能工作在GUI thread中
babytiger 2001-12-19
  • 打赏
  • 举报
回复
aaaaaaaaaaaaaaaaaaaaaazzzzzzzzzzzzzzzzzzzzzzzz
不会吧
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

16,551

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Creator Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧