![]()
|
|
Visual Basic Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
Imports System.IO Imports System.Net Imports System.Net.Sockets Public Class TCPServer #Region "Tcp Deklaration" Dim Port As Integer = 9000 Dim Server As TcpListener Dim Client As New TcpClient Dim serverThread As Threading.Thread Dim List As New List(Of Connection) Private Structure Connection Dim nick As String Dim pass As String Dim stream As NetworkStream Dim streamw As StreamWriter Dim streamr As StreamReader End Structure #End Region #Region "Tcp Funktionen" Private Sub mainServer(ByVal port As Integer) While True Try Client = Server.AcceptTcpClient Dim conn As New Connection conn.stream = Client.GetStream conn.streamr = New StreamReader(conn.stream) conn.streamw = New StreamWriter(conn.stream) List.Add(conn) Dim listen As New Threading.Thread(AddressOf ListenToConnection) listen.Start(conn) Catch ex As Exception MsgBox(ex.Message) End Try End While End Sub Private Sub ListenToConnection(ByVal conn As Connection) Do Try Dim tmp As String = conn.streamr.ReadLine Select Case tmp Case "add" Dim nick As String = conn.streamr.ReadLine conn.nick = nick ListBox1.Items.Add(nick) Dim str As String = conn.nick + " has joined." ListBox2.Items.Add(str) TCPSend("msg") TCPSend(str) Case "msg" Dim str As String = conn.nick + ": " + conn.streamr.ReadLine ListBox2.Items.Add(str) TCPSend("msg") TCPSend(str) Case "exit" Dim str As String = conn.nick + " has left." ListBox2.Items.Add(str) ListBox1.Items.Remove(conn.nick) List.Remove(conn) TCPSend("msg") TCPSend(str) Exit Do End Select Catch Exit Do End Try Loop End Sub Public Sub TCPSend(ByVal text As String) For Each conn In List conn.streamw.WriteLine(text) conn.streamw.Flush() Next End Sub Private Sub CloseServer() Try serverThread.Abort() For Each conn In List conn.stream.Close() conn.streamr.Close() conn.streamw.Close() Next Client.Close() Server.Stop() Catch End Try End Sub #End Region Private Sub TCPServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Server = New TcpListener(New IPEndPoint(IPAddress.Any, Port)) Server.Start() serverThread = New Threading.Thread(AddressOf mainServer) serverThread.Start(Port) End Sub Private Sub TCPServer_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing CloseServer() End Sub End Class |
|
|
Visual Basic Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
Imports System.IO Imports System.Net Imports System.Net.Sockets Public Class TCP_Client #Region "Tcp Dekleration" Dim stream As NetworkStream Dim streamw As StreamWriter Dim streamr As StreamReader Dim Client As New TcpClient Dim listenThread As Threading.Thread #End Region #Region "Tcp Funktionen" Public Sub connectToServer(ByVal ip As String, ByVal port As Integer) Client.Connect(ip, port) If Client.Connected = True Then stream = Client.GetStream streamw = New StreamWriter(stream) streamr = New StreamReader(stream) Else MsgBox("Error by connecting.") Exit Sub End If Try listenThread = New Threading.Thread(AddressOf mainClient) listenThread.Start() Catch ex As Exception MsgBox(ex.Message) End Try End Sub Private Sub mainClient() While Client.Connected Try Select Case streamr.ReadLine Case "msg" ListBox1.Items.Add(streamr.ReadLine) End Select Catch Exit While End Try End While End Sub Public Sub TCPSend(ByVal text As String) streamw.WriteLine(text) streamw.Flush() End Sub Public Sub closeClient() Try listenThread.Abort() TCPSend("exit") Client.Close() Catch End Try End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try connectToServer("127.0.0.1", 9000) TCPSend("add") TCPSend(My.Computer.Name) Catch ex As Exception MsgBox("Could not connect to the host.") Me.Close() Exit Sub End Try End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing closeClient() End Sub Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown If e.KeyCode = Keys.Enter Then If TextBox1.Text <> "" Then TCPSend("msg") TCPSend(TextBox1.Text) TextBox1.Text = "" End If End If End Sub End Class |

Button
|
|
Visual Basic Quellcode |
1 2 3 4 5 6 7 8 9 |
Case "exit" Dim str As String = conn.nick + " has left." ListBox2.Items.Add(str) ListBox1.Items.Remove(conn.nick) List.Remove(conn) TCPSend("msg") TCPSend(str) Exit Do End Select |

Button

Button
.