CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2012
    Posts
    7

    form do not take any action when tcpListener is start

    hi
    i create a very simple server-client chat program.
    in the server form , i put a textbox to recieve msg from clients and a button to start tcpListener.when i click this button tcpListener is start but the form is freeze.i cant choose any thing in this form.what can i do to fix this problem?

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: form do not take any action when tcpListener is start

    Yoy will have to show some code, as the problem can be anything, and without seeing your code; it is way too difficult to exactly tell you where the problems are.

  3. #3
    Join Date
    Apr 2012
    Posts
    7

    Re: form do not take any action when tcpListener is start

    sure ...
    this is the server codes :
    Code:
    Imports System.Text
    Imports System.Net
    Imports System.Net.Sockets
    
    Public Class Server
        Public ip As IPAddress = GetMyIP()
        Public TCPlistener As TcpListener = New TcpListener(ip, 2500)
        Private Sub Chat()
            Dim Messages(7) As String
            Dim a As New AutoResetEvent(False)
            While CheckBox1.Checked = True
                Try
                    TCPlistener.Start()
                    ListBox1.Items.Clear()
                    ListBox1.Items.Add("The server is ruuning as port :" & Str(2500))
                    Dim F As New Form
                    Controls.Add(F)
                    F.Size = New Size(100, 100)
                    F.Refresh()
    
                    Dim MyEndpoint As IPEndPoint = CType(TCPlistener.LocalEndpoint, IPEndPoint)
                    ListBox1.Items.Add("The local EndPoint address is :" & MyEndpoint.Address.ToString)
                    ListBox1.Items.Add("Waiting for connectiom ...")
                    Dim sock As Socket = TCPlistener.AcceptSocket
                    MyEndpoint = CType(sock.RemoteEndPoint, IPEndPoint)
                    ListBox1.Items.Add("Connection accepted from :" & MyEndpoint.Address.ToString)
                    Dim ByteData(100) As Byte
                    Dim size As Integer = sock.Receive(ByteData)
                    ListBox1.Items.Add("Recieved")
                    Dim RecieveText As String = Nothing
                    For i = 0 To size
                        RecieveText = RecieveText & Convert.ToChar(ByteData(i))
                    Next
                    Dim AsciiEncod As New ASCIIEncoding
                    ListBox1.Items.Add("Sending alert to client !")
                    sock.Send(AsciiEncod.GetBytes("The string was recieved by the server !"))
                    sock.Close()
                    MsgBox(RecieveText)
                    TCPlistener.Stop()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End While
        End Sub
    End Class
    this is the client codes :
    Code:
    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Text
    Imports System.IO
    
    Public Class Form1
    
        Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
            Try
                txtMessages.Text = Nothing
                Dim TCPclient As New TcpClient
                txtMessages.Text = txtMessages.Text & "Connecting ..." & vbCrLf
                Dim MyIP As IPAddress = GetMyIP()
                Dim SysPort As Int32 = Convert.ToInt16(txtPort.Text)
                TCPclient.Connect(MyIP.ToString, SysPort)
                txtMessages.Text = txtMessages.Text & "Connected" & vbCrLf
                txtMessages.Text = txtMessages.Text & "Enter a text to send ..." & vbCrLf
                txtSendMess.Focus()
                Dim StrMessage As String = txtSendMess.Text
                Dim stm As Stream = TCPclient.GetStream
                Dim AsciiEncod As New ASCIIEncoding
                Dim ByteData() As Byte = AsciiEncod.GetBytes(StrMessage)
                txtMessages.Text = txtMessages.Text & "Sending" & vbCrLf
                stm.Write(ByteData, 0, ByteData.Length)
                Dim ReplyMessage(100) As Byte
                Dim size As Integer = stm.Read(ReplyMessage, 0, 100)
                txtMessages.Text = txtMessages.Text & "This msg is from server :"
                Dim str As String = Nothing
                For i = 0 To size
                    str = str & Convert.ToChar(ReplyMessage(i))
                Next
                txtMessages.Text = txtMessages.Text & str & vbCrLf
                TCPclient.Close()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    i searched in google and find something about threading . if it is possible for you show me the solution with code or with correct this code .

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: form do not take any action when tcpListener is start

    Look at working examples for your language choice. http://code.msdn.microsoft.com/

    That will explain the problems you are having.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured