ActiveX control written in C# Socket problem

G

Guest

I have been working on an ActiveX control in C#. It is packaged in a Windows Control library and the code is in a user control. The control is used as an automation receiver that is used to update a page dynamically through the use of event listeners in javascript
That works, the problem is that when I run the test webpage that runs the ActiveX control, the socket does not start listening for about a minute and a half, after which it runs fine. My initial thoughts are that this is an iis problem. When I run the test webpage, I check netstat to see if my socket is listening, and it is, but I get all these time_wait connections, these eventually die out and are completely closed, and I am finally able to use my control properly

netstat -

Active Connection

Proto Local Address Foreign Address Stat
â€
TCP *:http localhost:4444 ESTABLISHE
TCP *:http localhost:4446 TIME_WAI
TCP *:http localhost:4448 TIME_WAI
TCP *:http localhost:4450 TIME_WAI
TCP *:http localhost:4452 TIME_WAI
TCP *:http localhost:4454 TIME_WAI
TCP *:http localhost:4456 TIME_WAI
TCP *:http localhost:4458 TIME_WAI
TCP *:http localhost:4460 TIME_WAI
TCP *:http localhost:4462 TIME_WAI
TCP *:http localhost:4465 TIME_WAI
TCP *:http localhost:4466 ESTABLISHE
TCP *:4421 localhost:http TIME_WAI
TCP *:4444 localhost:http ESTABLISHE
TCP *:4466 localhost:http ESTABLISHE
â€
UDP *:55557 *:
…
Does anyone have an idea why this is happening, I need this control to listen instantly. I am not completely sure that the time_waits have anything to do with this problem but I am at a loss to explain it, and I need to get this completed
The code for the ActiveX Control is
using System
using System.ComponentModel
using System.Drawing
using System.Windows.Forms
using System.Runtime.InteropServices
using System.Net
using System.Net.Sockets
using System.Threading

namespace LiveContro

public delegate void EventHandler(string Message)

[GuidAttribute("47209E59-A736-4c14-AD57-201E5162C297")
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)
public interface ILiveContro

[DispIdAttribute(0x60020000)
void MessageNotify(string Message);


public interface ILiveControlPubli

string MultiCastIP {get; set;
int MultiCastPort { get; set;
int MultiCastTTL { get; set;
int CharLimit { get; set;
void CloseSocket();

[ClassInterface(ClassInterfaceType.AutoDispatch),ComSourceInterfaces(typeof(ILiveControl))
public class MultiCast : System.Windows.Forms.UserControl, ILiveControlPubli

private string _multiCastIP
private int _multiCastPort
private int _multiCastTTL;
private Socket _receiveSocket
private string _multiCastMessage
private int _charLimit = 25000
private Thread trd = null

public string MultiCastI

get { return _multiCastIP;
set {_multiCastIP = value;

public int MultiCastPor

get { return _multiCastPort;
set {_multiCastPort = value;

public int MultiCastTT

get { return _multiCastTTL;
set {_multiCastTTL = value;

public string MultiCastMessag

get { return _multiCastMessage;
set {_multiCastMessage = value;

public int CharLimi

get { return _charLimit;
set {_charLimit = value;

event EventHandler MessageNotify

public MultiCast(

trd = new Thread(new ThreadStart(this.StartListening))
trd.IsBackground = true
trd.Start()


#region Socket Method
public void CloseSocket(

trd.Suspend()
trd.Abort()
_receiveSocket.Shutdown(SocketShutdown.Both)
_receiveSocket.Close()

private void StartListening(

tr
{
// initialize socke
_receiveSocket = new Socket(AddressFamily.InterNetwork,SocketType.Dgram,ProtocolType.Udp)

IPEndPoint ipept = new IPEndPoint(IPAddress.Any, _multiCastPort)

//bind socket to IPEndPoint
_receiveSocket.Bind(ipept);

IPAddress ip = IPAddress.Parse(_multiCastIP);

_receiveSocket.SetSocketOption(SocketOptionLevel.IP,SocketOptionName.AddMembership,
new MulticastOption(ip));
byte[] MessageBuffer = new byte[_charLimit];
//listening loop
while(true)
{
_receiveSocket.Receive(MessageBuffer);

//transform received byte[] to string
_multiCastMessage = System.Text.Encoding.ASCII.GetString(MessageBuffer);
if(_multiCastMessage.Length > 0)
{
// Broadcast Message to event listeners if they exist
if (MessageNotify != null)
{
//fire event MessageNotify(_multiCastMessage);
}
}
}
}
catch (Exception ex)
{
// Broadcast Message to event listeners if they exist
if (MessageNotify != null)
{
//fire event
MessageNotify(ex.Message);
}
}
finally
{
//Should never get here but just in case close the socket
_receiveSocket.Close();
}
}
#endregion
}
}

This is called in the webpage;

<body onunload="Unload();"><form id="Form1" method="post" runat="server"><object id=LiveControl classid=LiveControl.dll#LiveControl.MultiCast VIEWASTEXT><param name="MultiCastIP" value="*.*.*.*"><param name="MultiCastPort" value=55557><param name="MultiCastTTL" value=16><param name="CharLimit" value=50000></object><script language="javascript">
function Unload()
{
document.Form1.LiveControl.CloseSocket();
}
</script><script language=javascript for=LiveControl event="MessageNotify(a)">
alert(a);
</script>
 
G

Guest

after further examination of the problem. I have found that when the multicast is sent the receiving socket automatically receives the datagram, the problem is that 3 new tcp process are created

iexplore.exe:process# tcp 0.0.0.0:2961 0.0.0.0:0 listenin
inetinfo.exe:inetinfoprocess# tcp 127.0.0.1:80 127.0.0.1:2961 establishe
iexplore.exe:process# tcp 127.0.0.1:2961 127.0.0.1:80 establishe

until these close out after a minute and forty seconds everytime, i dont 'receive' the packet. any time after that i receive immediately. I believe this has something to do with the fact that the listener is run in a Com objectand that i may have declared it incorrectly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top