Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Namespace Multitasking_Privat_Verbindung_lan
Public Class ClasLan_Server
Public ip As String = "127.0.0.1"
Public port As Integer
Private AddrIP As IPAddress = IPAddress.Parse(ip)
Private Listener As New TcpListener(AddrIP, port)
Private Host As New TcpClient()
Private Networkstream1 As NetworkStream
Public Function Time_stream() As Boolean
If Listener.Pending = True And Host.Connected = False Then
Host = Listener.AcceptTcpClient()
Networkstream1 = Host.GetStream
Time_stream = True
Else
Time_stream = False
End If
End Function
Public Function Empfang() As String
Dim wert As String
Dim ruck As String
wert = ""
If Host.Client.Available > 0 Then
Dim Buffer(Host.Client.Available) As Byte
Networkstream1.Read(Buffer, 0, Host.Client.Available)
For Each item As Byte In Buffer
wert &= Microsoft.VisualBasic.Chr(item)
Next
End If
Send("Der befehl wird versucht bearbeitet zu werden!")
ruck = Net_Server_Privat_Befehle(wert)
Empfang = ruck
'Call close()
'Call server_start()
End Function
Public Sub Send(ByVal massage As String)
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(massage)
Networkstream1.Write(msg, 0, msg.Length)
Call close()
Call server_start()
End Sub
Public Sub server_start()
'Dim Listener As New TcpListener(Addr, 13000)
Listener.Start()
ForAuswahl.Time_Lan.Enabled = True
End Sub
Public Sub close()
Networkstream1.Close()
ForAuswahl.Time_Lan.Enabled = False
End Sub
Public Sub server_stop()
Dim argunull As Object
Dim aus_gabe As String
Try
Networkstream1.Close()
Listener.Stop()
ForAuswahl.Time_Lan.Enabled = False
Catch e As NullReferenceException
argunull = e
Listener.Stop()
ForAuswahl.Time_Lan.Enabled = False
Catch e As ArgumentNullException
argunull = e
aus_gabe = "ArgumentNullException;"
Catch e As SocketException
argunull = e
aus_gabe = "SocketException;"
End Try
End Sub
End Class
Public Class ClasLan_Client
Public Function NetClient(ByVal server As [String], ByVal PORT As Int32, ByVal message As [String]) As String
Dim gesentete_nachricht As String
Dim argunull As Object
' 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.
Try
Dim client As New TcpClient(server, PORT)
Try
' Translate the passed message into ASCII and store it as a Byte array.
'Transformiert V: massage in ascii um unt schreibt es in ein beyt rein muss sein!
Dim data As [Byte]() = System.Text.Encoding.ASCII.GetBytes(message)
' Get a client stream for reading and writing.
' Stream stream = client.GetStream();
'Nimmt einen server und spricht den so an das er schreiben und lesen kann
Dim stream As NetworkStream = client.GetStream()
' Send the message to the connected TcpServer.
'Sendet eine byte V über das netz
stream.Write(data, 0, data.Length)
'Gesendete Nachricht in V schreiben
gesentete_nachricht = "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)
NetClient = "Antwort: " & responseData
' Close everything.
stream.Close()
client.Close()
Catch e As NullReferenceException
client.Close()
argunull = e
NetClient = "NullReferenceException;"
Catch e As ArgumentNullException
argunull = e
NetClient = "ArgumentNullException;"
Catch e As SocketException
argunull = e
NetClient = "SocketException;"
End Try
Catch e As SocketException
argunull = e
NetClient = "SocketException;"
End Try
End Function
End Class
End Namespace