Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Go Back  Xtreme Visual Basic Talk > > > Windows Form Hangs or freezes how come?


Reply
 
Thread Tools Display Modes
  #1  
Old 01-09-2011, 10:31 PM
Brocke Brocke is offline
Freshman
 
Join Date: Oct 2010
Posts: 41
Default Windows Form Hangs or freezes how come?


I put this Code into a module and try to call it to start the Server side of the the applaction there also a Client but the Server side is done in a Console app but when i try to do it in a Windows Form it hangs once called to start listening.

Code:
Imports System.Net.Sockets
Imports System.Text


Module Module1

    Dim clientsList As New Hashtable

    Sub Main()

        Dim strInput = InputBox("Enter Port Number")

        Dim serverSocket As New TcpListener(strInput)


        Dim clientSocket As TcpClient
        Dim counter As Integer

        serverSocket.Start()
        msg("Server Started ....")
        counter = 0

        While (True)
            counter += 1
            clientSocket = serverSocket.AcceptTcpClient()

            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String

            Dim networkStream As NetworkStream = _
            clientSocket.GetStream()
            networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
            dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
            dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))

            clientsList(dataFromClient) = clientSocket

            broadcast(dataFromClient, dataFromClient, False)

            msg(dataFromClient)
            Dim client As New handleClinet
            client.startClient(clientSocket, dataFromClient, clientsList)
        End While

        clientSocket.Close()
        serverSocket.Stop()
        msg("exit")
        Console.ReadLine()
    End Sub

    Sub msg(ByVal mesg As String)
        mesg.Trim()
        Console.WriteLine(" >> " + mesg)
    End Sub
    Private Sub broadcast(ByVal msg As String, _
    ByVal uName As String, ByVal flag As Boolean)
        Dim Item As DictionaryEntry
        For Each Item In clientsList
            Dim broadcastSocket As TcpClient
            broadcastSocket = CType(Item.Value, TcpClient)
            Dim broadcastStream As NetworkStream = _
                   broadcastSocket.GetStream()
            Dim broadcastBytes As [Byte]()

            If flag = True Then
                broadcastBytes = Encoding.ASCII.GetBytes(uName + "" + msg)
            Else
                broadcastBytes = Encoding.ASCII.GetBytes(msg)
            End If

            broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length)
            broadcastStream.Flush()
        Next
    End Sub

    Public Class handleClinet
        Dim clientSocket As TcpClient
        Dim clNo As String
        Dim clientsList As Hashtable

        Public Sub startClient(ByVal inClientSocket As TcpClient, _
        ByVal clineNo As String, ByVal cList As Hashtable)
            Me.clientSocket = inClientSocket
            Me.clNo = clineNo
            Me.clientsList = cList
            Dim ctThread As Threading.Thread = New Threading.Thread(AddressOf doChat)
            ctThread.Start()
        End Sub

        Private Sub doChat()
            'Dim infiniteCounter As Integer
            Dim requestCount As Integer
            Dim bytesFrom(10024) As Byte
            Dim dataFromClient As String
            Dim sendBytes As [Byte]()
            Dim serverResponse As String
            Dim rCount As String
            requestCount = 0

            While (True)
                Try
                    requestCount = requestCount + 1
                    Dim networkStream As NetworkStream = _
                            clientSocket.GetStream()
                    networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom)
                    dataFromClient = _
            dataFromClient.Substring(0, dataFromClient.IndexOf("$"))
                    msg("" + clNo + " : " + dataFromClient)
                    rCount = Convert.ToString(requestCount)

                    broadcast(dataFromClient, clNo, True)
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
            End While
        End Sub

    End Class
End Module
i found this code on the internet that i was just playing with. its pretty neat but i cant figure out the program of the Windows form hanging issue.

Thank you All
Brock
Reply With Quote
  #2  
Old 01-10-2011, 04:34 AM
PlausiblyDamp's Avatar
PlausiblyDampWindows Form Hangs or freezes how come? PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

AcceptTcpClient is a blocking call (http://msdn.microsoft.com/en-us/libr...tcpclient.aspx) and will stop execution until there is a connected client. If you check the link I posted it gives a suggestion on what you can do to avoid this.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come? Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
 
Windows Form Hangs or freezes how come?
Windows Form Hangs or freezes how come?
 
-->