Still Can't Get Form.show with socket

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
 
T

Tom Shelton

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'm not sure why your having this issue... At a basic glance, I don't
really see anything that stands out as wrong. I notice that your
setting the server sockets blocking property to false... That is
unnecessary, since it has no effect when using Async methods - such as
BeginAccept.

You wouldn't happen to be accessing your forms from any of the async
procedures or the thread would you? This can cause some really strange
behavior. I suspect the issue is something other then your socket code.
But, if you would like to email me the complete project, I would be
happy to look at it and see if I can figure it out. You know - the
extra pair of eyes thing :)
 
L

Lespaul36

Actually yes...I needed to load the forms in the async code. I ended using
a timer instead. Seems to work OK.
but would be nice to async and load forms on the fly.
 

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