Imports System.Net.Sockets
Public Class Form1
    
Inherits System.Windows.Forms.Form
#Region " Vom Windows Form Designer generierter Code "
    
Public Sub New()
        
MyBase.
New()
        
' Dieser Aufruf ist fьr den Windows Form-Designer erforderlich.
        InitializeComponent()
        
' Initialisierungen nach dem Aufruf InitializeComponent() hinzufьgen
    End Sub
    ' Die Form ьberschreibt den Lцschvorgang der Basisklasse, um Komponenten zu bereinigen.
    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
    ' Fьr Windows Form-Designer erforderlich
    Private components 
As System.ComponentModel.IContainer
    
'HINWEIS: Die folgende Prozedur ist fьr den Windows Form-Designer erforderlich
    'Sie kann mit dem Windows Form-Designer modifiziert werden.
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    Friend WithEvents Button1 
As System.Windows.Forms.Button
    
Friend WithEvents Label1 
As System.Windows.Forms.Label
    
Friend WithEvents Label2 
As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> 
Private Sub InitializeComponent()
        
Me.Button1 = 
New System.Windows.Forms.Button
        
Me.Label1 = 
New System.Windows.Forms.Label
        
Me.Label2 = 
New System.Windows.Forms.Label
        
Me.SuspendLayout()
        
'
        'Button1
        '
        Me.Button1.Location = 
New System.Drawing.Point(72, 200)
        
Me.Button1.
Name = "Button1"
        
Me.Button1.Size = 
New System.Drawing.Size(144, 32)
        
Me.Button1.TabIndex = 0
        
Me.Button1.Text = "Button1"
        
'
        'Label1
        '
        Me.Label1.Location = 
New System.Drawing.Point(80, 24)
        
Me.Label1.
Name = "Label1"
        
Me.Label1.Size = 
New System.Drawing.Size(144, 24)
        
Me.Label1.TabIndex = 1
        
Me.Label1.Text = "Label1"
        
'
        'Label2
        '
        Me.Label2.Location = 
New System.Drawing.Point(80, 72)
        
Me.Label2.
Name = "Label2"
        
Me.Label2.Size = 
New System.Drawing.Size(144, 32)
        
Me.Label2.TabIndex = 2
        
Me.Label2.Text = "Label2"
        
'
        'Form1
        '
        Me.AutoScaleBaseSize = 
New System.Drawing.Size(5, 13)
        
Me.ClientSize = 
New System.Drawing.Size(292, 273)
        
Me.Controls.Add(
Me.Label2)
        
Me.Controls.Add(
Me.Label1)
        
Me.Controls.Add(
Me.Button1)
        
Me.
Name = "Form1"
        
Me.Text = "Form1"
        
Me.ResumeLayout(
False)
    
End Sub
#
End Region
    
Sub Connect(
ByVal server 
As [
String], 
ByVal message 
As [
String]
        Try
            ' Create a TcpClient.
            ' Note, for this client to work you need to have a TcpServer 
            ' connected to the same address as specified by the server, port
            ' combination.
            Dim port 
As Int32 = 13000
            
Dim client 
As New TcpClient(server, port)
            
' Translate the passed message into ASCII and store it as a Byte array.
            Dim data 
As [
Byte]

) = System.Text.Encoding.ASCII.GetBytes(message)
            
' Get a client stream for reading and writing.
            '  Stream stream = client.GetStream();
            Dim stream 
As NetworkStream = client.GetStream()
            
' Send the message to the connected TcpServer. 
            stream.
Write(data, 0, data.Length)
            
'Console.WriteLine("Sent: {0}", message)
            Label1.Text = "Sent: " + message
            
' Receive the TcpServer.response.
            ' Buffer to store the response bytes.
            data = 
New [
Byte]

256) {}
            
' String to store the response ASCII representation.
            Dim responseData 
As [
String] = [
String].Empty
            
' Read the first batch of the TcpServer response bytes.
            Dim bytes 
As Int32 = stream.
Read(data, 0, data.Length)
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
            
'Console.WriteLine("Received: {0}", responseData)
            Label2.Text = "Received:" + responseData
            
' Close everything.
            client.Close()
        
Catch e 
As ArgumentNullException
            Console.WriteLine("ArgumentNullException: {0}", e)
        
Catch e 
As SocketException
            Console.WriteLine("SocketException: {0}", e)
        
End Try
        'Console.WriteLine(ControlChars.Cr + " Press Enter to continue..."
        'Console.Read()
    End Sub 'Connect
    Private Sub Button1_Click(
ByVal sender 
As System.
Object, 
ByVal e 
As System.EventArgs) 
Handles Button1.Click
        Connect("127.0.0.1", "Clien hochet Connect"
    End Sub
End Class
  
Imports System.Net
Imports System.Net.Sockets
Public Class Form1
    
Inherits System.Windows.Forms.Form
    
Dim port 
As Int32 = 13000
    
Dim localAddr 
As IPAddress = IPAddress.Parse("127.0.0.1"
    Dim server 
As New TcpListener(localAddr, port)
#Region " Vom Windows Form Designer generierter Code "
    
Public Sub New()
        
MyBase.
New()
        
' Dieser Aufruf ist fьr den Windows Form-Designer erforderlich.
        InitializeComponent()
        
' Initialisierungen nach dem Aufruf InitializeComponent() hinzufьgen
    End Sub
    ' Die Form ьberschreibt den Lцschvorgang der Basisklasse, um Komponenten zu bereinigen.
    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
    ' Fьr Windows Form-Designer erforderlich
    Private components 
As System.ComponentModel.IContainer
    
'HINWEIS: Die folgende Prozedur ist fьr den Windows Form-Designer erforderlich
    'Sie kann mit dem Windows Form-Designer modifiziert werden.
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    Friend WithEvents Button1 
As System.Windows.Forms.Button
    
Friend WithEvents Label1 
As System.Windows.Forms.Label
    
Friend WithEvents Label2 
As System.Windows.Forms.Label
    
Friend WithEvents Label3 
As System.Windows.Forms.Label
    
Friend WithEvents Button2 
As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> 
Private Sub InitializeComponent()
        
Me.Button1 = 
New System.Windows.Forms.Button
        
Me.Label1 = 
New System.Windows.Forms.Label
        
Me.Label2 = 
New System.Windows.Forms.Label
        
Me.Label3 = 
New System.Windows.Forms.Label
        
Me.Button2 = 
New System.Windows.Forms.Button
        
Me.SuspendLayout()
        
'
        'Button1
        '
        Me.Button1.Location = 
New System.Drawing.Point(56, 160)
        
Me.Button1.
Name = "Button1"
        
Me.Button1.Size = 
New System.Drawing.Size(104, 24)
        
Me.Button1.TabIndex = 0
        
Me.Button1.Text = "Button1"
        
'
        'Label1
        '
        Me.Label1.Location = 
New System.Drawing.Point(40, 24)
        
Me.Label1.
Name = "Label1"
        
Me.Label1.Size = 
New System.Drawing.Size(184, 32)
        
Me.Label1.TabIndex = 1
        
Me.Label1.Text = "Label1"
        
'
        'Label2
        '
        Me.Label2.Location = 
New System.Drawing.Point(32, 72)
        
Me.Label2.
Name = "Label2"
        
Me.Label2.Size = 
New System.Drawing.Size(192, 40)
        
Me.Label2.TabIndex = 2
        
Me.Label2.Text = "Label2"
        
'
        'Label3
        '
        Me.Label3.Location = 
New System.Drawing.Point(32, 120)
        
Me.Label3.
Name = "Label3"
        
Me.Label3.Size = 
New System.Drawing.Size(192, 32)
        
Me.Label3.TabIndex = 3
        
Me.Label3.Text = "Label3"
        
'
        'Button2
        '
        Me.Button2.Location = 
New System.Drawing.Point(56, 200)
        
Me.Button2.
Name = "Button2"
        
Me.Button2.Size = 
New System.Drawing.Size(96, 24)
        
Me.Button2.TabIndex = 4
        
Me.Button2.Text = "Button2"
        
'
        'Form1
        '
        Me.AutoScaleBaseSize = 
New System.Drawing.Size(5, 13)
        
Me.ClientSize = 
New System.Drawing.Size(292, 273)
        
Me.Controls.Add(
Me.Button2)
        
Me.Controls.Add(
Me.Label3)
        
Me.Controls.Add(
Me.Label2)
        
Me.Controls.Add(
Me.Label1)
        
Me.Controls.Add(
Me.Button1)
        
Me.
Name = "Form1"
        
Me.Text = "Form1"
        
Me.ResumeLayout(
False)
    
End Sub
#
End Region
    
Private Sub Button1_Click(
ByVal sender 
As System.
Object, 
ByVal e 
As System.EventArgs) 
Handles Button1.Click
        
Try
            ' Set the TcpListener on port 13000.
            ' Start listening for client requests.
            server.Start()
            
' Buffer for reading data
            Dim bytes(1024) 
As [
Byte]
            
Dim data 
As [
String] = 
Nothing
            ' Enter the listening loop.
            While True
                Label1.Text = "Waiting for a connection... "
                
' Perform a blocking call to accept requests.
                ' You could also user server.AcceptSocket() here.
                Dim client 
As TcpClient = server.AcceptTcpClient()
                Connect("127.0.0.1", "Clien hochet Connect"
                Label1.Text = "Connected!"
                data = 
Nothing
                ' Get a stream object for reading and writing
                Dim stream 
As NetworkStream = client.GetStream()
                
Dim i 
As Int32
                
' Loop to receive all the data sent by the client.
                i = stream.
Read(bytes, 0, bytes.Length)
                
While (i <> 0)
                    
' Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
                    Label2.Text = "Received: {0}" + data
                    
' Process the data sent by the client.
                    data = data.ToUpper()
                    data = "Sam pridurok. Otwet ot servera"
                    
Dim msg 
As [
Byte]

) = System.Text.Encoding.ASCII.GetBytes(data)
                    
' Send back a response.
                    stream.
Write(msg, 0, msg.Length)
                    Label3.Text = "Sent: " + data
                    i = stream.
Read(bytes, 0, bytes.Length)
                
End While
                ' Shutdown and end connection
                client.Close()
            
End While
        Catch ex 
As SocketException
            
'Label1.Text = ([String].Format("SocketException: {0}", ex))
        End Try
       
    End Sub
    Private Sub Button2_Click(
ByVal sender 
As System.
Object, 
ByVal e 
As System.EventArgs) 
Handles Button2.Click
        server.
Stop()
    
End Sub
    Sub Connect(
ByVal server 
As [
String], 
ByVal message 
As [
String]
        Try
            ' Create a TcpClient.
            ' Note, for this client to work you need to have a TcpServer 
            ' connected to the same address as specified by the server, port
            ' combination.
            Dim port 
As Int32 = 13000
            
Dim client 
As New TcpClient(server, port)
            
' Translate the passed message into ASCII and store it as a Byte array.
            Dim data 
As [
Byte]

) = System.Text.Encoding.ASCII.GetBytes(message)
            
' Get a client stream for reading and writing.
            '  Stream stream = client.GetStream();
            Dim stream 
As NetworkStream = client.GetStream()
            
' Send the message to the connected TcpServer. 
            stream.
Write(data, 0, data.Length)
            
'Console.WriteLine("Sent: {0}", message)
            Label1.Text = "Sent: " + message
            
' Receive the TcpServer.response.
            ' Buffer to store the response bytes.
            data = 
New [
Byte]

256) {}
            
' String to store the response ASCII representation.
            Dim responseData 
As [
String] = [
String].Empty
            
' Read the first batch of the TcpServer response bytes.
            Dim bytes 
As Int32 = stream.
Read(data, 0, data.Length)
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
            
'Console.WriteLine("Received: {0}", responseData)
            Label2.Text = "Received:" + responseData
            
' Close everything.
            client.Close()
        
Catch e 
As ArgumentNullException
            Console.WriteLine("ArgumentNullException: {0}", e)
        
Catch e 
As SocketException
            Console.WriteLine("SocketException: {0}", e)
        
End Try
        'Console.WriteLine(ControlChars.Cr + " Press Enter to continue..."
        'Console.Read()
    End Sub 'Connect
    'Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    '   Connect("127.0.0.1", "Clien hochet Connect"
    'End Sub
End Class