Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i add a thread?
#1
Code:
Imports System.IO, System.Net, System.Net.Sockets
Public Class Form1

  Dim Client As TcpClient
  Dim sWriter As StreamWriter


  Sub Lees(ByVal ar As IAsyncResult)
    Try
      xUpdate(New StreamReader(Client.GetStream).ReadLine)
      Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Lees, Nothing)
    Catch
      xUpdate("Connectie verloren met de server.")
      Exit Sub
    End Try
  End Sub

  Private Sub Verzend(ByVal Str As String)
    Try
      sWriter = New StreamWriter(Client.GetStream)
      sWriter.WriteLine(Str)
      sWriter.Flush()
    Catch
      xUpdate("Er is iets mis met de server.")
    End Try
  End Sub

  Delegate Sub _xUpdate(ByVal Str As String)
  Sub xUpdate(ByVal Str As String)
    If InvokeRequired Then
      Invoke(New _xUpdate(AddressOf xUpdate), Str)
    Else
      If Str.Contains("URL") Then
        Process.Start(Deel("U", Str))
      Else
        TextBox1.AppendText(Str & Environment.NewLine)
      End If
    End If
  End Sub
  Public Function Deel(ByVal strLetter As String, ByVal str As String) As String
    Dim plaats As Integer = str.IndexOf(strLetter) + 3
    Dim lengte As Integer = str.Length - plaats
    Dim data As String = str.Substring(plaats, lengte)
    Return data
  End Function

  Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    If TextBox2.Text Is Nothing Then
      MessageBox.Show("Eh fluit, ge moet wel iets invullen")
    End If
    If e.KeyCode = Keys.Enter Then
      Dim strNickname As String = TextBox3.Text
      If strNickname = "" Then
        MessageBox.Show("Gelieve een naam in te vullen")
      Else
        Verzend(strNickname & " zegt: " & TextBox2.Text)
        TextBox2.Text = ""
      End If
    
    End If
  End Sub

  Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    Dim strNickname As String = TextBox3.Text
    Try
      Client = New TcpClient(txtIp.Text, CInt(3818))
      Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Lees), Nothing)
      'xUpdate("Je bent de chat binnengekomen")
      Verzend(strNickname & " is de chat binnengekomen.")

    Catch
      xUpdate("Geen verbinding met de server mogelijk.")
    End Try

  End Sub

  'Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  '    If Me.WindowState = FormWindowState.Minimized Then
  '        Dim flash As New FLASHWINFO
  '        flash.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(flash) '/// size of structure in bytes
  '        flash.hwnd = MyBase.Handle '/// Handle to the window to be flashed
  '        flash.dwFlags = FLASHW_ALL '/// to flash both the caption bar + the tray
  '        flash.uCount = 100 '/// the number of flashes
  '        flash.dwTimeout = 800 '/// speed of flashes in MilliSeconds ( can be left out )
  '        '/// flash the window you have specified the handle for...
  '        FlashWindowEx(flash)
  '    End If
  'End Sub
  'Public Structure FLASHWINFO
  '    Public cbSize As Int32
  '    Public hwnd As IntPtr
  '    Public dwFlags As Int32
  '    Public uCount As Int32
  '    Public dwTimeout As Int32
  'End Structure



  'Private Declare Function FlashWindowEx Lib "user32.dll" (ByRef pfwi As FLASHWINFO) As Int32

  'Private Const FLASHW_CAPTION As Int32 = &H1
  'Private Const FLASHW_TRAY As Int32 = &H2
  'Private Const FLASHW_ALL As Int32 = (FLASHW_CAPTION Or FLASHW_TRAY)
  Private Sub Form1_FormClosing1(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim strNickname As String = TextBox3.Text
    Verzend(strNickname & " heeft de chat verlaten.")
    End
  End Sub


End Class
This is my code for a chat application for school. It works very good this way. But my teacher wants that i add listener thread. So can someone help me to do this?

It has to we a thread with an endless loop that waits for connections.
Reply
#2
It's almost the same as the Client,
PHP Code:
Dim Listener as TcpListener 
Used for the Client that is sending messages, allowing it to both Receive and Send to the other Chat Program with a steady runtime.
Reply
#3
What milopeach said!

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What could i add to my browser? [Project] XDarkCoder 3 1,433 02-16-2013, 10:10 AM
Last Post: Swift Swim
  How to add EOF support to your crypter! ♱ RedTube ♱ 7 6,373 04-06-2010, 01:09 AM
Last Post: Jordan L.
  How do I add Textbox1.Text to a textfile? nevets04 4 1,012 11-17-2009, 03:33 AM
Last Post: Cyberknife

Forum Jump:


Users browsing this thread: 1 Guest(s)