473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ActiveX and CSocket

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();
}
Nov 16 '05 #1
1 2272
For anyone interested, the problem was solved by changing the CSocket.Create
with no parameters.

I was using:
int ErrCode = m_clientsock.Create(0,SOCK_STREAM, szHostName);

I changed it to:
int ErrCode = m_clientsock.Create();

Why did this make a difference?.. All well, I'll just accept it and go
on...

Thanks to anyone who tried to look into this.
Bryan
"Bryan" <br*********@hotmailX.comX> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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();
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: AnalogKid | last post by:
Short question: What's the difference between SingleUse and MultiUse ? Long question: I've been writing some sample code to see how different Instancing values and threading models work. I...
1
by: wang xiaoyu | last post by:
Hello: i want use activex in wxpython program,but when i use MakeActiveXClass an exception occurs. this is my source code dealing the DICOM ocx.I must note that in this program "hwtxcontrol" is...
2
by: Fie Fie Niles | last post by:
This one XP machine (with IE 6) is having a problem viewing any ActiveX controls (created on VB6) on the Internet Explorer browser. I put the same ActiveX control in a VB program, and when I run...
4
by: kids | last post by:
Can i post question related to CSocket here? Thanks
5
by: Brendan Grant | last post by:
The following describes a problem I have been having for the better part of 3 months, for approximately the last month however I have been focused solely on solving it and seem to be no nearer now...
18
by: DartmanX | last post by:
Is there a simple way to determine if someone using Internet Explorer has completely disabled ActiveX controls? Jason
7
by: Jarod_24 | last post by:
I just downloaded a activex control that was written in C# and tried to view it on my PDA's Internet Explorer. At my regular PC it displayed just fine, but nothing showed up on the pda. Do...
0
by: hsdas | last post by:
Hi, I started using CSocket as Winsock Control in VB6 do not allow to transfer UDTs. Can anybody who have used CSocket show me some light on how to use CSocket to Send and Receive UDTs....Quick help...
6
by: hufaunder | last post by:
I have an ActiveX component that I want to use in a library that I am writing. As a first test I used the ActiveX component in a windows form application. Adding the component created: Ax.dll...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.