473,623 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 CServiceCommCtr l::SendReceiveD ata(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(H ostName);
if (h == NULL)
{
AfxMessageBox(" Could not resolve Host Name to IP
Address!",MB_OK |MB_ICONERROR);
return strResult.Alloc SysString();
}
CString szHostName = inet_ntoa(*(rei nterpret_cast<i n_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"),szToServe r, "\r\n\r\n") ;

szToServer = TV;
//szToServer.Form at("%s%s",szToS erver, "\r\n\r\n") ;
int iPortNumber = PortNumber;

szErrorMsg.Form at("Establishin g connection to: %s\nOn Port: %d",szHostNam e,
PortNumber);
AfxMessageBox(s zErrorMsg);

// NOTE: This is where I keep getting the 10049 error
int ErrCode = m_clientsock.Cr eate(0,SOCK_STR EAM, szHostName);
if( ErrCode == 0 )
{
szErrorMsg.Form at(_T("Socket Error!\nError Code:
%d\n%d"),ErrCod e,GetLastError( ));

bError = true;
}

if (!bError)
{
// Connect to the Server on the specified port...
int connected = m_clientsock.Co nnect(szHostNam e,iPortNumber);
if (connected != 0)
{
// Send Data to Server
int iLen = szToServer.GetL ength();
int iSent = m_clientsock.Se nd(szToServer,i Len);

if (iSent==SOCKET_ ERROR)
{
szErrorMsg.Form at(_T("Server send error!\nError:
%d"),m_clientso ck.GetLastError ());
bError = true;
} else {
int iBufSize=10240; // Max Record Size...
int iRecvd;
char pBuf[10240];

iRecvd = m_clientsock.Re ceive(pBuf,iBuf Size); //get string from socket
if (iRecvd == SOCKET_ERROR)
{
szErrorMsg.Form at(_T("Server receive error!\nError:
%d"),m_clientso ck.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.Cl ose();
} else {
szErrorMsg.Form at(_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_cl ientsock.GetLas tError(),HostNa me,
PortNumber);
bError = true;
}
}

if (bError)
{
AfxMessageBox(s zErrorMsg,MB_OK |MB_ICONERROR);
strResult = szErrorMsg;
}

return strResult.Alloc SysString();
}
Nov 16 '05 #1
1 2288
For anyone interested, the problem was solved by changing the CSocket.Create
with no parameters.

I was using:
int ErrCode = m_clientsock.Cr eate(0,SOCK_STR EAM, szHostName);

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

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*********@ho tmailX.comX> wrote in message
news:%2******** ********@TK2MSF TNGP11.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 CServiceCommCtr l::SendReceiveD ata(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(H ostName);
if (h == NULL)
{
AfxMessageBox(" Could not resolve Host Name to IP
Address!",MB_OK |MB_ICONERROR);
return strResult.Alloc SysString();
}
CString szHostName = inet_ntoa(*(rei nterpret_cast<i n_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"),szToServe r, "\r\n\r\n") ;

szToServer = TV;
//szToServer.Form at("%s%s",szToS erver, "\r\n\r\n") ;
int iPortNumber = PortNumber;

szErrorMsg.Form at("Establishin g connection to: %s\nOn Port: %d",szHostNam e, PortNumber);
AfxMessageBox(s zErrorMsg);

// NOTE: This is where I keep getting the 10049 error
int ErrCode = m_clientsock.Cr eate(0,SOCK_STR EAM, szHostName);
if( ErrCode == 0 )
{
szErrorMsg.Form at(_T("Socket Error!\nError Code:
%d\n%d"),ErrCod e,GetLastError( ));

bError = true;
}

if (!bError)
{
// Connect to the Server on the specified port...
int connected = m_clientsock.Co nnect(szHostNam e,iPortNumber);
if (connected != 0)
{
// Send Data to Server
int iLen = szToServer.GetL ength();
int iSent = m_clientsock.Se nd(szToServer,i Len);

if (iSent==SOCKET_ ERROR)
{
szErrorMsg.Form at(_T("Server send error!\nError:
%d"),m_clientso ck.GetLastError ());
bError = true;
} else {
int iBufSize=10240; // Max Record Size...
int iRecvd;
char pBuf[10240];

iRecvd = m_clientsock.Re ceive(pBuf,iBuf Size); //get string from socket
if (iRecvd == SOCKET_ERROR)
{
szErrorMsg.Form at(_T("Server receive error!\nError:
%d"),m_clientso ck.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.Cl ose();
} else {
szErrorMsg.Form at(_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_cl ientsock.GetLas tError(),HostNa me,
PortNumber);
bError = true;
}
}

if (bError)
{
AfxMessageBox(s zErrorMsg,MB_OK |MB_ICONERROR);
strResult = szErrorMsg;
}

return strResult.Alloc SysString();
}

Nov 16 '05 #2

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

Similar topics

8
4555
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 tried all main combinations of Instancing (Multiuse/Singleuse) and of Threading models (per object/pool). Here's what I found: MultiUse - Thread per Object
1
5979
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 a ocx developed my me use vc6,this ocx works fine in wxpython. but you can see i only change this ocx with a new DICOM ocx and set up eventClass,
2
7563
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 the VB program on that same machine, I can view the ActiveX control fine. He is using Citrix. The same ActiveX controls can be viewed on other machines. When trying to view 1 of the ActiveX control, he got an error "Visual Basic Run-time redist...
4
2062
by: kids | last post by:
Can i post question related to CSocket here? Thanks
5
1794
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 than a month ago. Any thoughts or suggestions that might lead to a fix would be greatly appreciated. I have to VB based applications on two different PC's, a client and a server which use a pair of OCX's to provide a communication method...
18
8402
by: DartmanX | last post by:
Is there a simple way to determine if someone using Internet Explorer has completely disabled ActiveX controls? Jason
7
4391
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 ActiveX controls that are to be used by a pda need to be written in the ..net compact framework, or am i missing something else here? i have a HP iPaq 2490 with Windows Mobile Premium installed While i'm at it; does a activex control allow you to...
0
1119
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 is appreciated. Thanks in advance.
6
8043
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 .dll I can not call the functions in the ActiveX component. In the next step I tried to use the ActiveX component in a class library. I simply added a reference to the corresponding COM component. This this only
1
8326
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7150
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4074
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4164
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2602
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1778
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1473
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.