Chat Tool : Chat « Socket Network « VB.Net Tutorial






Chat Tool
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text

Imports System.Windows.Forms

public class ChatTool
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(88, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(136, 20)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = ""
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(32, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(48, 24)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "Message"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(0, 64)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(80, 24)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Target IP"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(88, 64)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(136, 20)
        Me.TextBox2.TabIndex = 3
        Me.TextBox2.Text = ""
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 104)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 24)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "Record"
        '
        'TextBox3
        '
        Me.TextBox3.AutoSize = False
        Me.TextBox3.Location = New System.Drawing.Point(80, 104)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(184, 136)
        Me.TextBox3.TabIndex = 5
        Me.TextBox3.Text = ""
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(232, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(56, 24)
        Me.Button1.TabIndex = 6
        Me.Button1.Text = "Send"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.TextBox1)
        Me.ResumeLayout(False)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bytes(200) As Byte
        Dim MyIP As IPAddress
        Dim RemoteIP As IPAddress
        Dim str As String

        RemoteIP = IPAddress.Parse(TextBox2.Text)
        MyIP = IPAddress.Parse("912.168.1.100")

        Dim Port1 As Integer = 8080
        Dim port2 As Integer = 8081

        Dim myLocal As New IPEndPoint(MyIP, Port1)
        Dim myRemote As New IPEndPoint(RemoteIP, port2)
        Dim receive1 As New UdpClient(myRemote)
        Dim send1 As New UdpClient(myLocal)

        Try
            bytes = System.Text.Encoding.ASCII.GetBytes(TextBox1.Text.ToUpper)
            send1.Send(bytes, bytes.Length, myRemote)
            str = System.Text.Encoding.ASCII.GetString(bytes)
            TextBox3.Text = TextBox3.Text & vbCrLf & str

            bytes = receive1.Receive(myLocal)
            str = System.Text.Encoding.ASCII.GetString(bytes)
            TextBox3.Text = TextBox2.Text & vbCrLf & str

            
        Catch ex As Exception
            Console.WriteLine("")
        End Try
        send1.Close()
        receive1.Close()
    End Sub
End Class








22.19.Chat
22.19.1.Chat ToolChat Tool