I have tried many things. I still have not found anything that seems to
work. Here is the portions of my code that deal with the socket maybe you
have a better idea? The form still seems to hang. I got it to not hang if
I used a loop with a application.doevents, but it was not CPU friendly.
'Declarations
Private SocketChatS As Socket
Private SocketChatC As TcpClient
Private marrClients As New ArrayList
Private allDone As New System.Threading.ManualResetEvent(True)
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
If Not designmode Then
Dim ipAdd() As System.Net.IPAddress =
System.Net.Dns.Resolve("localhost").AddressList
mthrListen = New System.Threading.Thread(AddressOf DoListen)
mthrListen.Start()
End If
End Sub
Private Sub DoListen()
Dim Iphe As IPHostEntry = Dns.Resolve(Dns.GetHostName)
Dim Ipep As New IPEndPoint(IPAddress.Any, MYPORT)
SocketChatS = New Socket(Ipep.Address.AddressFamily, SocketType.Stream,
ProtocolType.Tcp)
Dim blnFirst As Boolean = True
Dim pobjFRM As frmServerChatUI
With SocketChatS
.Blocking = False
.Bind(Ipep)
.Listen(100)
End With
While True
allDone.Reset()
SocketChatS.BeginAccept(New AsyncCallback(AddressOf AcceptClient),
SocketChatS)
allDone.WaitOne()
End While
End Sub
Private Sub AcceptClient(ByVal ar As IAsyncResult)
allDone.Set()
Dim pobjFRM As New frmServerChatUI
pobjFRM.Show()
Dim pintIndex As Integer = marrClients.Add(New HabibSocket.HabibsSocket)
marrClients(pintIndex).tcpsocket = SocketChatS.EndAccept(ar)
End Sub
"Tom Shelton" <ts******@YOUKNOWTHEDRILLcomcast.net> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
On 2005-02-08, Lespaul36 <le*******@none.net> wrote: I am working on a chat program. I have a socket set up to listen, opens
a
new thread and loops waiting for a connection. Problem is that it is
using
like 98% of my CPU...any ideas on a way that will still be effective, but
won't bog down the system?
Yes, use asnyc sockets - then you don't have to loop. Or if you don't
want to do that - then put a sleep in the loop
(system.threading.thread.sleep).
If you decide to go with an async socket (which is really the best way)
then here is a link on .NET sockets - both syncronous and asyncronous...
http://msdn.microsoft.com/library/de...consockets.asp
HTH
--
Tom Shelton [MVP]