H
http
Unregistered / Unconfirmed
GUEST, unregistred user!
各位,有个VC写的SERVER SOCKET,我用TCLIENTSOCKET怎么也连不上去
(但用他自己的CSOCKET切可以连接),请问是否得自己调用API的SOCKET?
如果非得自己写,是不是有什么好的控件呢?
// MYSocket.h : header file
//
#if !defined(AFX_MYSOCKET_H__E47A8F48_D083_4BC6_B184_B5478CA484A1__INCLUDED_)
#define AFX_MYSOCKET_H__E47A8F48_D083_4BC6_B184_B5478CA484A1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "AppServerDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CMYSocket command target
class CMYSocket : public CAsyncSocket
{
// Attributes
public:
// Operations
public:
BOOL GetConnectState();
void SetConnectState(BOOL bConnect = TRUE);
CMYSocket(CAppServerDlg *pDlg);
virtual ~CMYSocket();
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMYSocket)
public:
virtual void OnReceive(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnClose(int nErrorCode);
//}}AFX_VIRTUAL
// Generated message map functions
//{{AFX_MSG(CMYSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
// Implementation
protected:
CAppServerDlg *m_pDlg;
BOOL m_bConnect;
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYSOCKET_H__E47A8F48_D083_4BC6_B184_B5478CA484A1__INCLUDED_)
// MYSocket.cpp : implementation file
//
#include "stdafx.h"
#include "AppServer.h"
#include "MYSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMYSocket
CMYSocket::CMYSocket(CAppServerDlg *pDlg)
: m_pDlg(pDlg)
{
m_bConnect = FALSE;
}
CMYSocket::~CMYSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CMYSocket, CAsyncSocket)
//{{AFX_MSG_MAP(CMYSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CMYSocket member functions
void CMYSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnReceive(nErrorCode);
if (nErrorCode == 0)
{
m_pDlg->OnReceiveFromMYServer(this);
}
}
void CMYSocket::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if (nErrorCode == 0)
{
m_bConnect = TRUE;
m_pDlg->OnConnectToMYServer(this);
}
else
{
m_pDlg->PostMessage(WM_CLOSESOCKET, MYSOCKET_TYPE_ID);
}
CAsyncSocket::OnConnect(nErrorCode);
}
void CMYSocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_bConnect = FALSE;
m_pDlg->PostMessage(WM_CLOSESOCKET, MYSOCKET_TYPE_ID);
CAsyncSocket::OnClose(nErrorCode);
}
BOOL CMYSocket::GetConnectState()
{
return m_bConnect;
}
void CMYSocket::SetConnectState(BOOL bConnect)
{
m_bConnect = bConnect;
}
// AppServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AppServer.h"
#include "ListenSocket.h"
#include "SeatSocket.h"
#include "MYSocket.h"
#include "AppServerDlg.h"
..................
BOOL CAppServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
................
if (ConnectToMYServer() == FALSE)
{
EndDialog(2);
return FALSE;
}
..................
SetTimer(IDT_SECOND, 1000, NULL);
.................
}
void CAppServerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent == IDT_SECOND)
{
if (m_nWaitConnect > 0)
{
m_nWaitConnect --;
CString str;
str.Format(_T("重连接服务器"), m_nWaitConnect);
((CButton *)GetDlgItem(IDC_CONNECT))->SetWindowText((LPCTSTR)str);
}
if (m_nWaitConnect == 0)
{
if (ConnectToMYServer() == FALSE || m_pMYSocket->GetConnectState() == FALSE)
{
m_nWaitConnect = WAIT_CONNECT_SECONDS;
}
}
}
CDialog::OnTimer(nIDEvent);
}
void CAppServerDlg::OnConnect()
{
// TODO: Add your control notification handler code here
if (ConnectToMYServer() == FALSE)
{
m_nWaitConnect = WAIT_CONNECT_SECONDS;
CString str;
str.Format(_T("重连接服务器"), m_nWaitConnect);
((CButton *)GetDlgItem(IDC_CONNECT))->SetWindowText((LPCTSTR)str);
((CButton *)GetDlgItem(IDC_CONNECT))->EnableWindow();
}
}
void CAppServerDlg::OnConnectToMYServer(CMYSocket *pSocket)
{
m_nWaitConnect = -1;
((CStatic *)GetDlgItem(IDC_CONNECTSTATE))->SetIcon(m_hIconConnect);
((CButton *)GetDlgItem(IDC_CONNECT))->SetWindowText(_T("重连接服务器"));
SendToMYServer(&MYMessage);
}
BOOL CAppServerDlg::ConnectToMYServer()
{
if (m_pMYSocket == NULL || m_pMYSocket->GetConnectState() == FALSE)
{
((CButton *)GetDlgItem(IDC_CONNECT))->EnableWindow(FALSE);
CString sErrorMessage;
INT nError;
if (m_pMYSocket == NULL)
{
m_pMYSocket = new CMYSocket(this);
if (m_pMYSocket == NULL)
{
AfxMessageBox(_T("分配内存出错!"));
return FALSE;
}
if (m_pMYSocket->Create(0, SOCK_STREAM, FD_READ | FD_CONNECT | FD_CLOSE, (LPCTSTR)theApp.m_sAppIpAddress) == FALSE)
{
nError = m_pMYSocket->GetLastError();
sErrorMessage.Format(_T("Create(MYSocket)错误! 错误号:%d!"), nError);
// AfxMessageBox((LPCTSTR)sErrorMessage);
return FALSE;
}
}
if (m_pMYSocket->Connect((LPCTSTR)(theApp.m_sMYIpAddress), htons(theApp.m_nMYPort)) == FALSE)
{
nError = m_pMYSocket->GetLastError();
if (nError != WSAEWOULDBLOCK)
{
sErrorMessage.Format(_T("Connect(MYSocket)错误! 错误号:%d!"), nError);
// AfxMessageBox((LPCTSTR)sErrorMessage);
return FALSE;
}
else
{
m_nWaitConnect = WAIT_CONNECT_SECONDS;
}
}
else
{
m_pMYSocket->SetConnectState();
}
}
return TRUE;
}
(但用他自己的CSOCKET切可以连接),请问是否得自己调用API的SOCKET?
如果非得自己写,是不是有什么好的控件呢?
// MYSocket.h : header file
//
#if !defined(AFX_MYSOCKET_H__E47A8F48_D083_4BC6_B184_B5478CA484A1__INCLUDED_)
#define AFX_MYSOCKET_H__E47A8F48_D083_4BC6_B184_B5478CA484A1__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "AppServerDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CMYSocket command target
class CMYSocket : public CAsyncSocket
{
// Attributes
public:
// Operations
public:
BOOL GetConnectState();
void SetConnectState(BOOL bConnect = TRUE);
CMYSocket(CAppServerDlg *pDlg);
virtual ~CMYSocket();
// Overrides
public:
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMYSocket)
public:
virtual void OnReceive(int nErrorCode);
virtual void OnConnect(int nErrorCode);
virtual void OnClose(int nErrorCode);
//}}AFX_VIRTUAL
// Generated message map functions
//{{AFX_MSG(CMYSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
// Implementation
protected:
CAppServerDlg *m_pDlg;
BOOL m_bConnect;
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MYSOCKET_H__E47A8F48_D083_4BC6_B184_B5478CA484A1__INCLUDED_)
// MYSocket.cpp : implementation file
//
#include "stdafx.h"
#include "AppServer.h"
#include "MYSocket.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMYSocket
CMYSocket::CMYSocket(CAppServerDlg *pDlg)
: m_pDlg(pDlg)
{
m_bConnect = FALSE;
}
CMYSocket::~CMYSocket()
{
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CMYSocket, CAsyncSocket)
//{{AFX_MSG_MAP(CMYSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0
/////////////////////////////////////////////////////////////////////////////
// CMYSocket member functions
void CMYSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
CAsyncSocket::OnReceive(nErrorCode);
if (nErrorCode == 0)
{
m_pDlg->OnReceiveFromMYServer(this);
}
}
void CMYSocket::OnConnect(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
if (nErrorCode == 0)
{
m_bConnect = TRUE;
m_pDlg->OnConnectToMYServer(this);
}
else
{
m_pDlg->PostMessage(WM_CLOSESOCKET, MYSOCKET_TYPE_ID);
}
CAsyncSocket::OnConnect(nErrorCode);
}
void CMYSocket::OnClose(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_bConnect = FALSE;
m_pDlg->PostMessage(WM_CLOSESOCKET, MYSOCKET_TYPE_ID);
CAsyncSocket::OnClose(nErrorCode);
}
BOOL CMYSocket::GetConnectState()
{
return m_bConnect;
}
void CMYSocket::SetConnectState(BOOL bConnect)
{
m_bConnect = bConnect;
}
// AppServerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AppServer.h"
#include "ListenSocket.h"
#include "SeatSocket.h"
#include "MYSocket.h"
#include "AppServerDlg.h"
..................
BOOL CAppServerDlg::OnInitDialog()
{
CDialog::OnInitDialog();
................
if (ConnectToMYServer() == FALSE)
{
EndDialog(2);
return FALSE;
}
..................
SetTimer(IDT_SECOND, 1000, NULL);
.................
}
void CAppServerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent == IDT_SECOND)
{
if (m_nWaitConnect > 0)
{
m_nWaitConnect --;
CString str;
str.Format(_T("重连接服务器"), m_nWaitConnect);
((CButton *)GetDlgItem(IDC_CONNECT))->SetWindowText((LPCTSTR)str);
}
if (m_nWaitConnect == 0)
{
if (ConnectToMYServer() == FALSE || m_pMYSocket->GetConnectState() == FALSE)
{
m_nWaitConnect = WAIT_CONNECT_SECONDS;
}
}
}
CDialog::OnTimer(nIDEvent);
}
void CAppServerDlg::OnConnect()
{
// TODO: Add your control notification handler code here
if (ConnectToMYServer() == FALSE)
{
m_nWaitConnect = WAIT_CONNECT_SECONDS;
CString str;
str.Format(_T("重连接服务器"), m_nWaitConnect);
((CButton *)GetDlgItem(IDC_CONNECT))->SetWindowText((LPCTSTR)str);
((CButton *)GetDlgItem(IDC_CONNECT))->EnableWindow();
}
}
void CAppServerDlg::OnConnectToMYServer(CMYSocket *pSocket)
{
m_nWaitConnect = -1;
((CStatic *)GetDlgItem(IDC_CONNECTSTATE))->SetIcon(m_hIconConnect);
((CButton *)GetDlgItem(IDC_CONNECT))->SetWindowText(_T("重连接服务器"));
SendToMYServer(&MYMessage);
}
BOOL CAppServerDlg::ConnectToMYServer()
{
if (m_pMYSocket == NULL || m_pMYSocket->GetConnectState() == FALSE)
{
((CButton *)GetDlgItem(IDC_CONNECT))->EnableWindow(FALSE);
CString sErrorMessage;
INT nError;
if (m_pMYSocket == NULL)
{
m_pMYSocket = new CMYSocket(this);
if (m_pMYSocket == NULL)
{
AfxMessageBox(_T("分配内存出错!"));
return FALSE;
}
if (m_pMYSocket->Create(0, SOCK_STREAM, FD_READ | FD_CONNECT | FD_CLOSE, (LPCTSTR)theApp.m_sAppIpAddress) == FALSE)
{
nError = m_pMYSocket->GetLastError();
sErrorMessage.Format(_T("Create(MYSocket)错误! 错误号:%d!"), nError);
// AfxMessageBox((LPCTSTR)sErrorMessage);
return FALSE;
}
}
if (m_pMYSocket->Connect((LPCTSTR)(theApp.m_sMYIpAddress), htons(theApp.m_nMYPort)) == FALSE)
{
nError = m_pMYSocket->GetLastError();
if (nError != WSAEWOULDBLOCK)
{
sErrorMessage.Format(_T("Connect(MYSocket)错误! 错误号:%d!"), nError);
// AfxMessageBox((LPCTSTR)sErrorMessage);
return FALSE;
}
else
{
m_nWaitConnect = WAIT_CONNECT_SECONDS;
}
}
else
{
m_pMYSocket->SetConnectState();
}
}
return TRUE;
}