Advertisement
Guest User

Verbindung.vb

a guest
Dec 11th, 2011
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.22 KB | None | 0 0
  1. Public Class Verbindung
  2.  
  3.     Dim IpAdresse As String
  4.     Dim Port As Integer
  5.     Dim Tcp_client As System.Net.Sockets.TcpClient
  6.     Dim STW As System.IO.StreamWriter
  7.     Dim STR As System.IO.StreamReader
  8.     Dim isVerbunden As Boolean = False
  9.  
  10.     Public Chatname As String
  11.     Public Event NeueNachricht(ByVal sNachricht As String)
  12.     Public Event VerbindungUnterbrochen(ByVal sError As String)
  13.  
  14.     Public Sub starte(ByVal ip As String, ByVal Port As Integer)
  15.         Me.IpAdresse = ip
  16.         Me.Port = Port
  17.         Dim t As New System.Threading.Thread(AddressOf Verbinden)
  18.         t.IsBackground = True
  19.         t.Start()
  20.     End Sub
  21.  
  22.     Private Sub Verbinden()
  23.         Tcp_client = New System.Net.Sockets.TcpClient
  24.         While isVerbunden = False
  25.             Try
  26.                 Tcp_client.Connect(IpAdresse, Port)
  27.                 STW = New System.IO.StreamWriter(Tcp_client.GetStream)
  28.                 STR = New System.IO.StreamReader(Tcp_client.GetStream)
  29.                 STW.WriteLine(Chatname)
  30.                 STW.Flush()
  31.                 Dim t As New System.Threading.Thread(AddressOf Listener)
  32.                 t.IsBackground = True
  33.                 t.Start()
  34.                 isVerbunden = True
  35.             Catch ex As Exception
  36.                 isVerbunden = False
  37.                 Console.WriteLine("Server ist offline...")
  38.             End Try
  39.         End While
  40.     End Sub
  41.  
  42.     Private Sub Listener()
  43.         Try
  44.             While isVerbunden = True
  45.                 Dim sNachricht As String = STR.ReadLine
  46.                 RaiseEvent NeueNachricht(sNachricht)
  47.             End While
  48.         Catch ex As Exception
  49.             stopp()
  50.             RaiseEvent VerbindungUnterbrochen("Fehler bei Listener " & vbCr & ex.Message)
  51.         End Try
  52.  
  53.  
  54.     End Sub
  55.  
  56.     Public Sub stopp()
  57.         On Error Resume Next
  58.         isVerbunden = False
  59.         STR.Close()
  60.         STW.Close()
  61.  
  62.  
  63.     End Sub
  64.  
  65.     Public Sub schreiben(ByVal sText As String)
  66.         Try
  67.             STW.WriteLine(sText)
  68.             STW.Flush()
  69.         Catch ex As Exception
  70.  
  71.             stopp()
  72.             RaiseEvent VerbindungUnterbrochen("Fehler bei Schreiben Sub :" & vbCr & ex.Message)
  73.         End Try
  74.     End Sub
  75. End Class
  76.  
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement