Imports System.Text
Imports System.Net.Sockets
Imports System.Threading
Imports System.Windows.Forms
public class SocketServerUI
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 lblMessage As System.Windows.Forms.Label
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 txtContent As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents btnWrite As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnListener = New System.Windows.Forms.Button()
Me.lblMessage = New System.Windows.Forms.Label()
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.txtContent = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.btnWrite = 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, 28)
Me.btnListener.Name = "btnListener"
Me.btnListener.Size = New System.Drawing.Size(113, 32)
Me.btnListener.TabIndex = 0
Me.btnListener.Text = "Start Listening"
'
'lblMessage
'
Me.lblMessage.BackColor = System.Drawing.Color.White
Me.lblMessage.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.lblMessage.Location = New System.Drawing.Point(164, 80)
Me.lblMessage.Name = "lblMessage"
Me.lblMessage.Size = New System.Drawing.Size(389, 24)
Me.lblMessage.TabIndex = 1
'
'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(61, 80)
Me.lblConnection.Name = "lblConnection"
Me.lblConnection.Size = New System.Drawing.Size(93, 24)
Me.lblConnection.TabIndex = 2
Me.lblConnection.Text = "Connection Message"
'
'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(41, 33)
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(164, 31)
Me.txtPort.Name = "txtPort"
Me.txtPort.Size = New System.Drawing.Size(133, 26)
Me.txtPort.TabIndex = 4
Me.txtPort.Text = ""
'
'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(471, 27)
Me.btnClose.Name = "btnClose"
Me.btnClose.Size = New System.Drawing.Size(102, 32)
Me.btnClose.TabIndex = 5
Me.btnClose.Text = "Stop Connection"
'
'txtContent
'
Me.txtContent.Location = New System.Drawing.Point(164, 120)
Me.txtContent.Multiline = True
Me.txtContent.Name = "txtContent"
Me.txtContent.Size = New System.Drawing.Size(389, 88)
Me.txtContent.TabIndex = 6
Me.txtContent.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(61, 120)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(93, 24)
Me.Label1.TabIndex = 7
Me.Label1.Text = "Data"
'
'btnWrite
'
Me.btnWrite.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
Me.btnWrite.Location = New System.Drawing.Point(573, 120)
Me.btnWrite.Name = "btnWrite"
Me.btnWrite.Size = New System.Drawing.Size(123, 32)
Me.btnWrite.TabIndex = 8
Me.btnWrite.Text = "Write"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(716, 261)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnWrite, Me.Label1, Me.txtContent, Me.btnClose, Me.txtPort, Me.lblPort, Me.lblConnection, Me.lblMessage, Me.btnListener})
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
Dim intPort As Integer
Dim myTcpListener As TcpListener
Dim myNetworkStream As NetworkStream
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 StartListen()
intPort = Integer.Parse(txtPort.Text)
myTcpListener = New TcpListener(intPort)
Dim blnConection As Boolean = False
Try
myTcpListener.Start()
lblMessage.Text = "Waiting..."
Dim mySocket As Socket = myTcpListener.AcceptSocket()
Do
If mySocket.Connected = True Then
lblMessage.Text = "Port: " + txtPort.Text + "Connected"
myNetworkStream = New NetworkStream(mySocket)
Dim strContent As String
Dim lngByte As Long = myNetworkStream.ReadByte()
Dim myByte(lngByte) As Byte
myNetworkStream.Read(myByte, 0, lngByte)
strContent = Encoding.ASCII.GetString(myByte, 0, lngByte)
txtContent.Text = strContent
End If
Loop
Catch ex As SocketException
MessageBox.Show _
(ex.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
myTcpListener.Stop()
End Sub
Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
Dim strTest As String = txtContent.Text
Dim myBytes() As Byte = Encoding.ASCII.GetBytes(strTest)
lblMessage.Text = ("Message")
myNetworkStream.Write(myBytes, 0, myBytes.Length)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class