B
Bryan
I have created an MFC ActiveX control. If I connect to my local machine on
some port, everything works fine. If I try to connect to a different
computer, I get an error (from CSocket.Create) that says "The requested
address is not valid in its context."
I know for a fact that the address is valid because I can have one of my
Java Programs connect just fine...
Is there just something about using an ActiveX control to create a socket
connection to an external computer?..
Any help would be appreciated.
Bryan
Here is a code snip:
BSTR CServiceCommCtrl::SendReceiveData(LPCTSTR HostName, long PortNumber,
LPCTSTR ToServer)
{
CString strResult;
// TODO: Add your dispatch handler code here
// Convert HostName to an IP address
hostent *h = NULL;
h = gethostbyname(HostName);
if (h == NULL)
{
AfxMessageBox("Could not resolve Host Name to IP
Address!",MB_OK|MB_ICONERROR);
return strResult.AllocSysString();
}
CString szHostName = inet_ntoa(*(reinterpret_cast<in_addr*>(h->h_addr)));
// Create a socket connectionto HostName on Port Number. Send the ToServer
and return the response.
CSocket m_clientsock;
BOOL bError = false;
CString szErrorMsg = "";
CString szToServer = ToServer;
CString TV;
TV.Format(_T("%s%s"),szToServer, "\r\n\r\n");
szToServer = TV;
//szToServer.Format("%s%s",szToServer, "\r\n\r\n");
int iPortNumber = PortNumber;
szErrorMsg.Format("Establishing connection to: %s\nOn Port: %d",szHostName,
PortNumber);
AfxMessageBox(szErrorMsg);
// NOTE: This is where I keep getting the 10049 error
int ErrCode = m_clientsock.Create(0,SOCK_STREAM, szHostName);
if( ErrCode == 0 )
{
szErrorMsg.Format(_T("Socket Error!\nError Code:
%d\n%d"),ErrCode,GetLastError());
bError = true;
}
if (!bError)
{
// Connect to the Server on the specified port...
int connected = m_clientsock.Connect(szHostName,iPortNumber);
if (connected != 0)
{
// Send Data to Server
int iLen = szToServer.GetLength();
int iSent = m_clientsock.Send(szToServer,iLen);
if (iSent==SOCKET_ERROR)
{
szErrorMsg.Format(_T("Server send error!\nError:
%d"),m_clientsock.GetLastError());
bError = true;
} else {
int iBufSize=10240; // Max Record Size...
int iRecvd;
char pBuf[10240];
iRecvd = m_clientsock.Receive(pBuf,iBufSize); //get string from socket
if (iRecvd == SOCKET_ERROR)
{
szErrorMsg.Format(_T("Server receive error!\nError:
%d"),m_clientsock.GetLastError());
bError = true;
} else {
// Set our member variable to the data received from the stream
pBuf[iRecvd]=0;
strResult = pBuf;
}
}
// Close Socket Connection
m_clientsock.Close();
} else {
szErrorMsg.Format(_T("Error connecting to the Host Server on the
specified port!\nError: %d\n\nHost: %s\nPort: %d\n\nContact Technical
Support with this message."),m_clientsock.GetLastError(),HostName,
PortNumber);
bError = true;
}
}
if (bError)
{
AfxMessageBox(szErrorMsg,MB_OK|MB_ICONERROR);
strResult = szErrorMsg;
}
return strResult.AllocSysString();
}
some port, everything works fine. If I try to connect to a different
computer, I get an error (from CSocket.Create) that says "The requested
address is not valid in its context."
I know for a fact that the address is valid because I can have one of my
Java Programs connect just fine...
Is there just something about using an ActiveX control to create a socket
connection to an external computer?..
Any help would be appreciated.
Bryan
Here is a code snip:
BSTR CServiceCommCtrl::SendReceiveData(LPCTSTR HostName, long PortNumber,
LPCTSTR ToServer)
{
CString strResult;
// TODO: Add your dispatch handler code here
// Convert HostName to an IP address
hostent *h = NULL;
h = gethostbyname(HostName);
if (h == NULL)
{
AfxMessageBox("Could not resolve Host Name to IP
Address!",MB_OK|MB_ICONERROR);
return strResult.AllocSysString();
}
CString szHostName = inet_ntoa(*(reinterpret_cast<in_addr*>(h->h_addr)));
// Create a socket connectionto HostName on Port Number. Send the ToServer
and return the response.
CSocket m_clientsock;
BOOL bError = false;
CString szErrorMsg = "";
CString szToServer = ToServer;
CString TV;
TV.Format(_T("%s%s"),szToServer, "\r\n\r\n");
szToServer = TV;
//szToServer.Format("%s%s",szToServer, "\r\n\r\n");
int iPortNumber = PortNumber;
szErrorMsg.Format("Establishing connection to: %s\nOn Port: %d",szHostName,
PortNumber);
AfxMessageBox(szErrorMsg);
// NOTE: This is where I keep getting the 10049 error
int ErrCode = m_clientsock.Create(0,SOCK_STREAM, szHostName);
if( ErrCode == 0 )
{
szErrorMsg.Format(_T("Socket Error!\nError Code:
%d\n%d"),ErrCode,GetLastError());
bError = true;
}
if (!bError)
{
// Connect to the Server on the specified port...
int connected = m_clientsock.Connect(szHostName,iPortNumber);
if (connected != 0)
{
// Send Data to Server
int iLen = szToServer.GetLength();
int iSent = m_clientsock.Send(szToServer,iLen);
if (iSent==SOCKET_ERROR)
{
szErrorMsg.Format(_T("Server send error!\nError:
%d"),m_clientsock.GetLastError());
bError = true;
} else {
int iBufSize=10240; // Max Record Size...
int iRecvd;
char pBuf[10240];
iRecvd = m_clientsock.Receive(pBuf,iBufSize); //get string from socket
if (iRecvd == SOCKET_ERROR)
{
szErrorMsg.Format(_T("Server receive error!\nError:
%d"),m_clientsock.GetLastError());
bError = true;
} else {
// Set our member variable to the data received from the stream
pBuf[iRecvd]=0;
strResult = pBuf;
}
}
// Close Socket Connection
m_clientsock.Close();
} else {
szErrorMsg.Format(_T("Error connecting to the Host Server on the
specified port!\nError: %d\n\nHost: %s\nPort: %d\n\nContact Technical
Support with this message."),m_clientsock.GetLastError(),HostName,
PortNumber);
bError = true;
}
}
if (bError)
{
AfxMessageBox(szErrorMsg,MB_OK|MB_ICONERROR);
strResult = szErrorMsg;
}
return strResult.AllocSysString();
}