VB.Net Tutorial/Socket Network/Socket Client

Материал из VB Эксперт
Перейти к: навигация, поиск

Using Async Socket Client

Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Imports System.Text
Imports System.Windows.Forms
public class UsingAsyncSocketClient
   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.ruponentModel.IContainer
    Friend WithEvents btnListener As System.Windows.Forms.Button
    Friend WithEvents lblConnection As System.Windows.Forms.Label
    Friend WithEvents lblPort As System.Windows.Forms.Label
    Friend WithEvents txtPort As System.Windows.Forms.TextBox
    Friend WithEvents btnClose As System.Windows.Forms.Button
    Friend WithEvents btnReceive As System.Windows.Forms.Button
    Friend WithEvents txtMessage As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents btnSend As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtAddress As System.Windows.Forms.TextBox
    Friend WithEvents txtSend As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnListener = New System.Windows.Forms.Button()
        Me.lblConnection = New System.Windows.Forms.Label()
        Me.lblPort = New System.Windows.Forms.Label()
        Me.txtPort = New System.Windows.Forms.TextBox()
        Me.btnClose = New System.Windows.Forms.Button()
        Me.btnReceive = New System.Windows.Forms.Button()
        Me.txtMessage = New System.Windows.Forms.TextBox()
        Me.txtSend = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.btnSend = New System.Windows.Forms.Button()
        Me.txtAddress = New System.Windows.Forms.TextBox()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        "
        "btnListener
        "
        Me.btnListener.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnListener.Location = New System.Drawing.Point(338, 48)
        Me.btnListener.Name = "btnListener"
        Me.btnListener.Size = New System.Drawing.Size(113, 32)
        Me.btnListener.TabIndex = 0
        Me.btnListener.Text = "Connect"
        "
        "lblConnection
        "
        Me.lblConnection.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblConnection.Location = New System.Drawing.Point(41, 96)
        Me.lblConnection.Name = "lblConnection"
        Me.lblConnection.Size = New System.Drawing.Size(92, 24)
        Me.lblConnection.TabIndex = 2
        Me.lblConnection.Text = "Info"
        "
        "lblPort
        "
        Me.lblPort.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblPort.Location = New System.Drawing.Point(20, 56)
        Me.lblPort.Name = "lblPort"
        Me.lblPort.Size = New System.Drawing.Size(113, 24)
        Me.lblPort.TabIndex = 3
        Me.lblPort.Text = "Port"
        "
        "txtPort
        "
        Me.txtPort.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtPort.Location = New System.Drawing.Point(143, 48)
        Me.txtPort.Name = "txtPort"
        Me.txtPort.Size = New System.Drawing.Size(133, 26)
        Me.txtPort.TabIndex = 4
        Me.txtPort.Text = "36000"
        "
        "btnClose
        "
        Me.btnClose.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnClose.Location = New System.Drawing.Point(461, 48)
        Me.btnClose.Name = "btnClose"
        Me.btnClose.Size = New System.Drawing.Size(102, 32)
        Me.btnClose.TabIndex = 5
        Me.btnClose.Text = "Disconnect"
        "
        "btnReceive
        "
        Me.btnReceive.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnReceive.Location = New System.Drawing.Point(338, 272)
        Me.btnReceive.Name = "btnReceive"
        Me.btnReceive.Size = New System.Drawing.Size(113, 32)
        Me.btnReceive.TabIndex = 6
        Me.btnReceive.Text = "Receive"
        "
        "txtMessage
        "
        Me.txtMessage.Location = New System.Drawing.Point(143, 96)
        Me.txtMessage.Multiline = True
        Me.txtMessage.Name = "txtMessage"
        Me.txtMessage.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtMessage.Size = New System.Drawing.Size(430, 88)
        Me.txtMessage.TabIndex = 7
        Me.txtMessage.Text = ""
        "
        "txtSend
        "
        Me.txtSend.Location = New System.Drawing.Point(143, 192)
        Me.txtSend.Multiline = True
        Me.txtSend.Name = "txtSend"
        Me.txtSend.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtSend.Size = New System.Drawing.Size(430, 64)
        Me.txtSend.TabIndex = 9
        Me.txtSend.Text = ""
        "
        "Label1
        "
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label1.Location = New System.Drawing.Point(10, 192)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(144, 24)
        Me.Label1.TabIndex = 8
        Me.Label1.Text = "Content"
        "
        "btnSend
        "
        Me.btnSend.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnSend.Location = New System.Drawing.Point(461, 272)
        Me.btnSend.Name = "btnSend"
        Me.btnSend.Size = New System.Drawing.Size(112, 32)
        Me.btnSend.TabIndex = 10
        Me.btnSend.Text = "Send"
        "
        "txtAddress
        "
        Me.txtAddress.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtAddress.Location = New System.Drawing.Point(143, 16)
        Me.txtAddress.Name = "txtAddress"
        Me.txtAddress.Size = New System.Drawing.Size(420, 26)
        Me.txtAddress.TabIndex = 12
        Me.txtAddress.Text = "192.168.0.70"
        "
        "Label2
        "
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label2.Location = New System.Drawing.Point(20, 21)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(113, 24)
        Me.Label2.TabIndex = 11
        Me.Label2.Text = "Address"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(665, 309)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtAddress, Me.Label2, Me.btnSend, Me.txtSend, Me.Label1, Me.txtMessage, Me.btnReceive, Me.btnClose, Me.txtPort, Me.lblPort, Me.lblConnection, Me.btnListener})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub

    Dim strMessage
    Dim myConnect As Socket
    Dim bteAccept(65536) As Byte
    Dim bteSend(65536) As Byte
    Private Sub btnListener_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListener.Click
        Dim myThread As New Thread(New ThreadStart(AddressOf StartListen))
        myThread.Start()
    End Sub
    Private Sub btnReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceive.Click
        Dim myThread As New Thread(New ThreadStart(AddressOf ReceiveStart))
        myThread.Start()
    End Sub
    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim myThread As New Thread(New ThreadStart(AddressOf SendStart))
        myThread.Start()
    End Sub

    Private Sub StartListen()
        Dim intPort As Integer
        Dim bidEndPoint As IPEndPoint
        intPort = Integer.Parse(txtPort.Text)
        bidEndPoint = New IPEndPoint(IPAddress.Parse(txtAddress.Text), intPort)
        myConnect = New Socket _
                     (AddressFamily.InterNetwork, _
                     SocketType.Stream, _
                     ProtocolType.Tcp)
        Try
            Dim myAsyncCallBack As New AsyncCallback(AddressOf ConnectEnd)
            myConnect.BeginConnect(bidEndPoint, myAsyncCallBack, myConnect)
            txtMessage.Text += vbCrLf + "Waiting..."
            Thread.Sleep(500)
            txtSend.Text += "Begin ----" + vbCrLf
            Thread.Sleep(500)
            txtSend.Text += "This is the first string ..." + vbCrLf
            Thread.Sleep(500)
            txtSend.Text += "This is the second string ...." + vbCrLf
            Thread.Sleep(500)
            txtSend.Text += "End-------" + vbCrLf
        Catch ex As SocketException
            Console.WriteLine(ex.Message)
        End Try
    End Sub
    Private Sub ConnectEnd(ByVal pIAsyncResult As IAsyncResult)
        myConnect.EndConnect(pIAsyncResult)
        txtMessage.Text += vbCrLf + "Successful"
    End Sub
    Private Sub ReceiveStart()
        Dim myAsyncCallBack As New AsyncCallback(AddressOf ReceiveData)
        myConnect.BeginReceive _
          (bteAccept, 0, 65536, 0, _
          myAsyncCallBack, myConnect)
        txtMessage.Text += vbCrLf + "Receive"
    End Sub
    Private Sub ReceiveData(ByVal pIAsyncResult As IAsyncResult)
        Dim intByte As Integer
        intByte = myConnect.EndReceive(pIAsyncResult)
        If intByte > 0 Then
            strMessage += Encoding.ASCII.GetString(bteAccept)
            txtSend.Text = "" + strMessage
        End If
    End Sub
    Private Sub SendStart()
        Dim myAsyncCallBack As New AsyncCallback(AddressOf SendData)
        bteSend = Encoding.ASCII.GetBytes(txtSend.Text)
        myConnect.BeginSend _
          (bteSend, 0, bteSend.Length, _
          SocketFlags.DontRoute, myAsyncCallBack, myConnect)
    End Sub
    Private Sub SendData(ByVal pIAsyncResult As IAsyncResult)
        Dim intSend As Integer
        intSend = myConnect.EndSend(pIAsyncResult)
        txtMessage.Text += vbCrLf + "Message sent" + intSend.ToString + " bytes "
    End Sub
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        myConnect.Close()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
End Class