Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class Verbindung
- Dim IpAdresse As String
- Dim Port As Integer
- Dim Tcp_client As System.Net.Sockets.TcpClient
- Dim STW As System.IO.StreamWriter
- Dim STR As System.IO.StreamReader
- Dim isVerbunden As Boolean = False
- Public Chatname As String
- Public Event NeueNachricht(ByVal sNachricht As String)
- Public Event VerbindungUnterbrochen(ByVal sError As String)
- Public Sub starte(ByVal ip As String, ByVal Port As Integer)
- Me.IpAdresse = ip
- Me.Port = Port
- Dim t As New System.Threading.Thread(AddressOf Verbinden)
- t.IsBackground = True
- t.Start()
- End Sub
- Private Sub Verbinden()
- Tcp_client = New System.Net.Sockets.TcpClient
- While isVerbunden = False
- Try
- Tcp_client.Connect(IpAdresse, Port)
- STW = New System.IO.StreamWriter(Tcp_client.GetStream)
- STR = New System.IO.StreamReader(Tcp_client.GetStream)
- STW.WriteLine(Chatname)
- STW.Flush()
- Dim t As New System.Threading.Thread(AddressOf Listener)
- t.IsBackground = True
- t.Start()
- isVerbunden = True
- Catch ex As Exception
- isVerbunden = False
- Console.WriteLine("Server ist offline...")
- End Try
- End While
- End Sub
- Private Sub Listener()
- Try
- While isVerbunden = True
- Dim sNachricht As String = STR.ReadLine
- RaiseEvent NeueNachricht(sNachricht)
- End While
- Catch ex As Exception
- stopp()
- RaiseEvent VerbindungUnterbrochen("Fehler bei Listener " & vbCr & ex.Message)
- End Try
- End Sub
- Public Sub stopp()
- On Error Resume Next
- isVerbunden = False
- STR.Close()
- STW.Close()
- End Sub
- Public Sub schreiben(ByVal sText As String)
- Try
- STW.WriteLine(sText)
- STW.Flush()
- Catch ex As Exception
- stopp()
- RaiseEvent VerbindungUnterbrochen("Fehler bei Schreiben Sub :" & vbCr & ex.Message)
- End Try
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement