Re: Winsock Control
Well, after investigating in the morning I could write this code to
try the CAsyncSocket.
I'm blocked in receiving the data, CAsyncSocket.receive returns a -1
error and calling GetLastError can't
get any details of what is failing.
I've created a new class that wraps CAsyncSocket (MySocket).
In my app header ( CUart2Dlg ) I create two instances of MySocket
( m_SocketServer and m_SocketListener ).
Inside MySocket I throw the events to the CUart2Dlg to write there the
code.
Here the definition of my class Dialog:
<CODE>
class CUart2Dlg : public CDialog
{
// Construction
public:
CUart2Dlg(CWnd* pParent = NULL); // standard constructor
void OnAccept();
void OnClose();
void OnRecieve();
// [...]
private:
MySocket m_SocketServer;
MySocket m_SocketListener;
// [...]
</CODE>
This is my initDialog application:
<CODE>
BOOL CUart2Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this
automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
//MINE
m_SocketServer.SetParentDlg( this );
m_SocketListener.SetParentDlg( this );
return TRUE; // return TRUE unless you set the focus to a control
}
</CODE>
And the event handlers:
<CODE>
void CUart2Dlg::OnClickOpenServer()
{
// TODO: Add your control notification handler code here
UpdateData(true);
m_SocketServer.Create( m_iTcpPort );
//Empezamos a escuchar
if( m_SocketServer.Listen()==FALSE )
{
AfxMessageBox( "Unable to listen on that port, please try another
port" );
m_SocketServer.Close();
return;
}
addString( IDC_TCPDEBUG, _T("Waiting for conection...\r\n") );
}
void CUart2Dlg::OnAccept()
{
CString strIP;
UINT port;
if(m_SocketServer.Accept( m_SocketListener ) )
{
m_SocketServer.GetSockName( strIP, port );
addString( IDC_TCPDEBUG, _T("Client connected: " + strIP+"\r\n" ) );
m_SocketServer.Send("Connected To Server",strlen("Connected To
Server"));
}
else
{
AfxMessageBox("Cannoot Accept Connection");
}
}
//Al recibir datos
void CUart2Dlg::OnRecieve()
{
//char *pBuf =new char [1025];
unsigned char* pBuf[100];
CString strData;
int iLen;
iLen = m_SocketServer.Receive( pBuf,100 );
/*
strData.Format( _T("Data length recieved:%d"), iLen );
MessageBox( strData );
*/
if(iLen==SOCKET_ERROR)
{
AfxMessageBox("Could not Recieve");
strData.Format( _T("Error: %d"),m_SocketServer.GetLastError() ); //
<---Returns 0
}
else
{
AfxMessageBox( "Yes");
}
}
</CODE>