L
Lespaul36
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 listening 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. I had a suggestiong using channels, but haven't found much to
help me or to find the advantage of it. It is just for chat on a LAN.
I have it working with Winsock, but would like to be able to use the socket
namespace instead.
'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
work. Here is the portions of my code that deal with the listening 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. I had a suggestiong using channels, but haven't found much to
help me or to find the advantage of it. It is just for chat on a LAN.
I have it working with Winsock, but would like to be able to use the socket
namespace instead.
'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