Da ich in den letzten Threads ziemlich oft gefragt wurde, wie man einen TCP Server / Client schreibt, werd ich jetzt 2 Beispiele angeben.
TCP Listener. Läuft hier auf einer Konsole.
Da sich dieses bsp in einer Form aufhängt, hier der Link für eine Form: tutorials.de/forum/visual-basi…12633-winsock-vb-tcp.html
gpwiki.org/index.php/VBNET:Sockets
Besser ist es aber wenn man einfach den code unten mit einem Thread ausführt, dass führt zu keinem absturz...
Und hier der dazugehörige TCP Client. Hier: Für Konsole läuft aber genauso für eine Form.
Ich hoff das mein Thread euch weiter hilft!
*Topic verschoben, passt gut hierher*
TCP Listener. Läuft hier auf einer Konsole.
Da sich dieses bsp in einer Form aufhängt, hier der Link für eine Form: tutorials.de/forum/visual-basi…12633-winsock-vb-tcp.html
gpwiki.org/index.php/VBNET:Sockets
Besser ist es aber wenn man einfach den code unten mit einem Thread ausführt, dass führt zu keinem absturz...
VB.NET-Quellcode
- Imports System
- Imports System.IO
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.Text
- Imports Microsoft.VisualBasic
- Class MyTcpListener
- Sub Main()
- Console.WriteLine("Bitte geben sie die Locale IP ein: ")
- connect(Console.ReadLine())
- End Sub
- Sub connect(ByVal ip As String)
- Dim server As TcpListener
- server = Nothing
- Try
- ' Set the TcpListener on port 13000.
- Dim port As Int32 = 1234
- Dim localAddr As IPAddress = IPAddress.Parse(ip)
- server = New TcpListener(localAddr, port)
- ' 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
- Console.Write("Waiting for a connection... ")
- ' Perform a blocking call to accept requests.
- ' You could also user server.AcceptSocket() here.
- Dim client As TcpClient = server.AcceptTcpClient()
- Console.WriteLine("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)
- Console.WriteLine("Received: {0}", data)
- ' Hier hast du den empfangenen string data
- If data = ....
- 'Anschließend gibst du die antwort:
- data = "Button1.text = hehehe"
- Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
- stream.Write(msg, 0, msg.Length)
- Console.WriteLine("Sent: {0}", data)
- i = stream.Read(bytes, 0, bytes.Length)
- End While
- ' Shutdown and end connection
- client.Close()
- End While
- Catch e As SocketException
- Console.WriteLine("SocketException: {0}", e)
- Finally
- server.Stop()
- End Try
- Console.WriteLine(ControlChars.Cr + "Hit enter to continue....")
- Console.Read()
- End Sub
- End Class 'MyTcpListener
Und hier der dazugehörige TCP Client. Hier: Für Konsole läuft aber genauso für eine Form.
VB.NET-Quellcode
- Imports System
- Imports System.IO
- Imports System.Net
- Imports System.Net.Sockets
- Class MyTcpListener
- Sub Main()
- Console.WriteLine("Bitte geben sie die IP des Servers ein: ")
- Dim temp As String = Console.ReadLine
- Console.WriteLine("Bitte die Nachricht eingeben: ")
- Connect(temp, Console.ReadLine)
- 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 = 1234
- 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)
- ' 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)
- ' Close everything.
- stream.Close()
- 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
- End Class 'MyTcpListener
Ich hoff das mein Thread euch weiter hilft!
*Topic verschoben, passt gut hierher*
Dieser Beitrag wurde bereits 5 mal editiert, zuletzt von „Malischh“ ()