Mega Code Archive

 
Categories / VB.Net Tutorial / Socket Network
 

Using Async Socket Server

Imports System.Net.Sockets Imports System.Net Imports System.Threading Imports System.Text Imports System.Windows.Forms public class UsingAsyncSocketServer    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 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 txtAccept 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.txtAccept = New System.Windows.Forms.TextBox()         Me.Label1 = New System.Windows.Forms.Label()         Me.btnSend = New System.Windows.Forms.Button()         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, 24)         Me.btnListener.Name = "btnListener"         Me.btnListener.Size = New System.Drawing.Size(113, 32)         Me.btnListener.TabIndex = 0         Me.btnListener.Text = "Start"         '         '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, 72)         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, 32)         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, 24)         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, 24)         Me.btnClose.Name = "btnClose"         Me.btnClose.Size = New System.Drawing.Size(102, 32)         Me.btnClose.TabIndex = 5         Me.btnClose.Text = "Stop"         '         '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(276, 248)         Me.btnReceive.Name = "btnReceive"         Me.btnReceive.Size = New System.Drawing.Size(175, 32)         Me.btnReceive.TabIndex = 6         Me.btnReceive.Text = "Receive"         '         'txtMessage         '         Me.txtMessage.Location = New System.Drawing.Point(143, 72)         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 = ""         '         'txtAccept         '         Me.txtAccept.Location = New System.Drawing.Point(143, 168)         Me.txtAccept.Multiline = True         Me.txtAccept.Name = "txtAccept"         Me.txtAccept.ScrollBars = System.Windows.Forms.ScrollBars.Vertical         Me.txtAccept.Size = New System.Drawing.Size(430, 64)         Me.txtAccept.TabIndex = 9         Me.txtAccept.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, 168)         Me.Label1.Name = "Label1"         Me.Label1.Size = New System.Drawing.Size(144, 24)         Me.Label1.TabIndex = 8         Me.Label1.Text = "Data"         '         '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, 248)         Me.btnSend.Name = "btnSend"         Me.btnSend.Size = New System.Drawing.Size(112, 32)         Me.btnSend.TabIndex = 10         Me.btnSend.Text = "Data sent back"         '         '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.btnSend, Me.txtAccept, 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 myListener As Socket     Dim bteAccept(65536) As Byte     Dim bteSend(65536) As Byte     Dim EndSocket As Socket     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("10.2.3.127"), intPort)         myListener = New Socket _                      (AddressFamily.InterNetwork, _                      SocketType.Stream, _                      ProtocolType.Tcp)         Try             '  myListener.Bind(bidEndPoint)             Dim myAsyncCallBack As New AsyncCallback(AddressOf AcceptEnd)             myListener.Listen(intPort)             myListener.BeginAccept(myAsyncCallBack, myListener)             txtMessage.Text += vbCrLf + "Waiting"             strMessage += "//Head..." + vbCrLf             strMessage += "//Server Send Message ..." + vbCrLf             strMessage += "--------------------------------" + vbCrLf         Catch ex As SocketException             Console.WriteLine(ex.Message)         End Try     End Sub     Private Sub AcceptEnd(ByVal pIAsyncResult As IAsyncResult)         EndSocket = myListener.EndAccept(pIAsyncResult)     End Sub     Private Sub ReceiveStart()         Dim myAsyncCallBack As New AsyncCallback(AddressOf ReceiveData)         EndSocket.BeginReceive _           (bteAccept, 0, 65536, 0, _           myAsyncCallBack, EndSocket)     End Sub     Private Sub ReceiveData(ByVal pIAsyncResult As IAsyncResult)         Dim intByte As Integer         intByte = EndSocket.EndReceive(pIAsyncResult)         If intByte > 0 Then             strMessage += Encoding.ASCII.GetString(bteAccept)             txtAccept.Text = strMessage         End If     End Sub     Private Sub SendStart()         Dim myAsyncCallBack As New AsyncCallback(AddressOf SendData)         bteSend = Encoding.ASCII.GetBytes(strMessage)         EndSocket.BeginSend _           (bteSend, 0, bteSend.Length, _           SocketFlags.DontRoute, myAsyncCallBack, EndSocket)     End Sub     Private Sub SendData(ByVal pIAsyncResult As IAsyncResult)         Dim intSend As Integer         intSend = EndSocket.EndSend(pIAsyncResult)         txtMessage.Text += vbCrLf + "Data sent " + intSend.ToString + "bytes"     End Sub     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click         myListener.Close()     End Sub     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load     End Sub End Class