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



I 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.Close();
}
Thread.Sleep(10);
}
}
catch (Exception)
{
}

tcpl.Stop();
}


////////////////////////////////////////////////////
/// C# test code opens "localhost", port 65525
/// and send successfully a packet to above
// C# listener
/// for simplicity some portions deleted
/// Otherwise, working code
///////////////////////////////////////////////////
public int SocketSendReceive()
{
try
{
TcpClient Client = new TcpClient("localhost", 65525);
NetworkStream nc = Client.GetStream();
nc.Write(SendBuffer, 0, SendBuffSize);
nc.Flush();

int ReceivedSize = nc.Read(ReceiveBuffer, 0, BuffSize);

nc.Close();
Client.Close();
}
catch (Exception e)
{
//process exception
}
return ReceivedSize;
}

.



Relevant Pages

  • Re: monitoring traffic on a port?
    ... > application or daemon that's listening to the port. ... > You could also start looking at external network monitoring hardware to ...
    (comp.sys.sun.misc)
  • Re: monitoring traffic on a port?
    ... > application or daemon that's listening to the port. ... > You could also start looking at external network monitoring hardware to ...
    (comp.unix.solaris)
  • Re: monitoring traffic on a port?
    ... > application or daemon that's listening to the port. ... > You could also start looking at external network monitoring hardware to ...
    (comp.sys.sun.admin)
  • Re: monitoring traffic on a port?
    ... > application or daemon that's listening to the port. ... > You could also start looking at external network monitoring hardware to ...
    (comp.sys.sun.hardware)
  • Re: Socket and cycle problem
    ... listening this port countinously. ... So I need make some loop to print data from 3883 port permanent. ... the data it receives from each connection after the remote side drops ... If you were to use the socket module, then it would look something like this: ...
    (comp.lang.python)

Quantcast