我刚开始学网络编程,想写一个C/S程序。为了测试,我把代码写到accept()后就运行程序,
然后在Win2K的命令提示符中用telnet 127.0.0.1 2626,(2626是我在程序中监听的端口号)
但总是显示“正在连接到127.0.0.1...无法打开到主机的连接 在端口 2626 : 连接失败”
而总是无法连接到服务端。程序代码如下,程序能够顺利地执行到accept()方法内。请教我哪里错了,谢谢,急。BOOL CNetTalk1Dlg::OnInitDialog()
{
// …… this->m_serv.sin_family = AF_INET; //sockaddr_in m_serv;
this->m_serv.sin_port = 2626;
this->m_serv.sin_addr.s_addr = htonl(INADDR_ANY);
this->m_nAddLen = sizeof(this->m_serv); //int m_nAddLen; this->m_sock = socket(AF_INET, SOCK_STREAM, 0); //SOCKET m_sock; if (bind(this->m_sock, (sockaddr*)&(this->m_serv), this->m_nAddLen))
{
this->ShowMsg("系统消息:绑定错误!");
}
else
{
this->ShowMsg("系统消息:OK!服务器创建成功!");
} listen(this->m_sock, 5);
AfxBeginThread(SubThread, 0);
}UINT SubThread(LPVOID p)
{
CNetTalk1Dlg* mainWnd = (CNetTalk1Dlg*)AfxGetApp()->GetMainWnd(); // SOCKET m_sockMsg;
mainWnd->m_sockMsg = 
accept(mainWnd->m_sock, (sockaddr*)&(mainWnd->m_serv), &(mainWnd->m_nAddLen)); this->ShowMsg("系统消息:OK!已经有客户端连接!");
//……
}