socket connection problem between C++ ActiveX and C# listening port

socket connection problem between C++ ActiveX and C# listening port

Post by Hitchka » Sun, 04 Jun 2006 15:30:28


don't know if there is discrepancy between straight C code socket
and C# counterpart in newer pocket PC versions and appreciate it if
somebody can shed a light on this.

Few years ago I wrote an ATL ActiveX and a C++ daemon running locally
on a PocketPC. The daemon opens a port on "localhost" and listens
to incoming packets. The ActiveX connects to ("localhost", port)
and send messages to the listening daemon. Both the daemon and ActiveX
were written in C++ and used straight C socket code and both were
working perfectly.

I rewrote the daemon in C# and of course run it on a newer version of
PocketPC and now the same ActiveX fails to connect to the socket with
the error 10061 (WSAECONNREFUSED, no connection could be made because
the target machine actively refused it). This basically tells me
ActiveX's ("localhost", port) combination is somehow different
from C#'s ("localhost", port) combination and connection can not
be made.

To verify this I wrote a test program in C# that connects to the
daemons ("localhost", port) successfully and confirms port is open
and listening.

I have been pulling my hair for few days not being able to find out
were the problem is. Somehow either definition of ("localhost",
port) is different from C and C# codes, or there are some security or
permission differences between the C# and C libraries. I really
appreciate it if you can provide a clue at what possibly could be wrong
here.

Thanks in advance

////////////////////////////////////////////////////
/// Non working C++ ActiveX code
// fails to connect to C# listening socket on
// "localhost", port 65525 (connection refused)
/// for simplicity some portions deleted
///////////////////////////////////////////////////

bool XComp::SendRecSocket()
{
sockaddr_in a;
hostent *h;
SOCKET m_s;

int SendRes;

m_s = socket(AF_INET, SOCK_STREAM, 0);
if(m_s == INVALID_SOCKET)
{
return false;
}

a.sin_family = AF_INET;
a.sin_port = htons(65525);
sin_addr.s_addr = inet_addr("localhost");
if (a.sin_addr.s_addr == INADDR_NONE || a.sin_addr.s_addr == 0)
{
h = gethostbyname("localhost");
if (h == NULL)
{
closesocket(m_s);
return false;
}
memcpy(&(a.sin_addr), h->h_addr_list[0], h->h_length);
}

if(connect(m_s, (const sockaddr *)&a, sizeof(a)) != 0)
{
// Fails here with error code WSAECONNREFUSED
// connection actively refused

closesocket(m_s);
return false;
}

// reset of code deleted for simplicity

}



////////////////////////////////////////////////////
/// C# listener code snippet
/// for simplicity some portions deleted
/// Otherwise, working code
//// opening and listening "localhost", port 65525
///////////////////////////////////////////////////

public void ListenThread()
{
IPHostEntry host = Dns.GetHostEntry("localhost");
TcpListener tcpl = new TcpListener(host.AddressList[0], 65525);
try
{
tcpl.Start();

while (run)
{
if (tcpl.Pending())
{
TcpClient NewClient = tcpl.AcceptTcpClient();
NetworkStream stream = NewClient.GetStream();
int RecSize = stream.Read(ReceiveBuffer, 0,
ReceiveBuffer.Length);

if (RecSize > 1)
{
// process the buffer and send answer by
stream.Write(SendBuffer, 0, SendBuffer.Length);

}
NewClient.Cl
 
 
 

1. Socket connection to port fails despite port open / listening !

2. Listening socket still listen after first connection

Hi, i have a socket that i use to listen for a connection. there can be any
number of connections. when testing my socket, it sees the first connection,
then no others. i call the BeginAccept() method again, but it doesnt see any
other connections. heres the code...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim ep As New IPEndPoint(ipHome, PORT)

Try

socListen.Bind(ep)
socListen.Listen(50)
socListen.BeginAccept(AddressOf UserLogingIn, Nothing)

Catch ex As Exception

Call WriteError("Button1_Click", ex)

End Try

End Sub

Private Sub UserLogingIn(ByVal ar As IAsyncResult)

Try

Dim soc As New Connection

soc.soc = socListen.EndAccept(ar)
ReDim soc.buffer(4095)

socListen.BeginAccept(AddressOf UserLogingIn, Nothing)

soc.soc.BeginReceive(soc.buffer, 0, soc.buffer.Length,
SocketFlags.None, AddressOf UserSentSignOnInfo, soc)

lstConnected.Add(soc)

Catch ex As Exception

Call WriteError("UserLogingIn", ex)

End Try

End Sub


Connection is a structure of a byte array, socket, and name. all variables
have values, etc. VS2005. thanks

--
-iwdu15

3. IO::Socket Stop listening for new connections after initial connection

4. Porting Unix sockets code: socket connection to a child

5. problem using an ActiveX object written in C# in native C++

6. [sockets] Listen on several UDP ports ?

7. Listen on multiple ports with one socket?

8. IO Completion Ports - VS C++ 6/VS .NET C++/C#

9. sockets - how to listen on multiple ports

10. what does cant create listen socket on port 110.unknown error at ?

11. [sockets] Listen on several UDP ports ?

12. one question about socket listening port

13. List socket based servers listening at a particular port

14. How do I listen to a random number of ports with one socket?