By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,529 Members | 899 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,529 IT Pros & Developers. It's quick & easy.

ActiveX control written in C# Socket problem

P: n/a
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)

[InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIDispatch)
public interface ILiveContro

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),Co mSourceInterfaces(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
#region Socket Method
public void CloseSocket(


private void StartListening(

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

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

//bind socket to IPEndPoint

IPAddress ip = IPAddress.Parse(_multiCastIP);

_receiveSocket.SetSocketOption(SocketOptionLevel.I P,SocketOptionName.AddMembership,
new MulticastOption(ip));
byte[] MessageBuffer = new byte[_charLimit];
//listening loop

//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
//Should never get here but just in case close the socket

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()
</script><script language=javascript for=LiveControl event="MessageNotify(a)">
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply

P: n/a
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 listenin
inetinfo.exe:inetinfoprocess# tcp establishe
iexplore.exe:process# tcp 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.
Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.