daran liegt's nicht...
ich hab den Port für den PC des Servers freigegeben (Router: Speedport W701-V)
--------------------
Das sind Konsolen:
TCP SERVER CODE:
TCP Client Code:
ich hab den Port für den PC des Servers freigegeben (Router: Speedport W701-V)
--------------------
Das sind Konsolen:
TCP SERVER CODE:
VB.NET-Quellcode
- Imports System
- Imports System.IO
- Imports System.Net
- Imports System.Net.Sockets
- Imports System.Text
- Imports Microsoft.VisualBasic
- Module Module1
- 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("Warte auf eine Verbindung... ")
- ' Perform a blocking call to accept requests.
- ' You could also user server.AcceptSocket() here.
- Dim client As TcpClient = server.AcceptTcpClient()
- Console.WriteLine("Verbunden!")
- 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)
- ' Hier hast du den empfangenen string data
- If data <> "" Then
- Console.WriteLine("")
- Console.WriteLine("Empfangen: " & data & "")
- Console.WriteLine("")
- Console.WriteLine("Bitte Antwort eingeben!")
- Console.WriteLine("")
- Else
- Console.WriteLine("Es wurden keine Daten gesendet.")
- End If
- 'Anschließend gibst du die antwort:
- data = Console.ReadLine()
- Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
- stream.Write(msg, 0, msg.Length)
- Console.WriteLine("")
- Console.WriteLine("Antwort gesendet: {0}", data)
- Console.WriteLine("")
- i = stream.Read(bytes, 0, bytes.Length)
- End While
- ' Shutdown and end connection
- client.Close()
- End While
- Catch e As SocketException
- Console.WriteLine("FEHLER - ERROR: {0}", e)
- Finally
- server.Stop()
- End Try
- Console.WriteLine(ControlChars.Cr + "Bitte ENTER drücken....")
- Console.Read()
- End Sub
- Sub Main()
- Dim IP As Net.IPAddress
- Try
- IP = Net.Dns.Resolve(My.Computer.Name).AddressList(0)
- Catch ex As Exception
- MsgBox("Die IP konnte nicht ermittelt werden!")
- End Try
- Console.WriteLine("Welcome to TCP-Server!")
- Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
- Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
- Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
- Console.WriteLine("====================")
- Console.WriteLine("Ermittelte IP:")
- Console.WriteLine("====================")
- Console.WriteLine(IP.ToString)
- Console.WriteLine("====================")
- Console.WriteLine("Falls IP richtig oder IP unbekannt, bitte die Obenstehende IP eingeben!")
- Console.WriteLine("====================")
- connect(Console.ReadLine)
- End Sub
- End Module
TCP Client Code:
VB.NET-Quellcode
- Imports System
- Imports System.IO
- Imports System.Net
- Imports System.Net.Sockets
- Module Module1
- 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("")
- Console.WriteLine("Gesendet: {0}", message)
- Console.WriteLine("")
- ' 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("")
- Console.WriteLine("Empfangen: {0}", responseData)
- Console.WriteLine("")
- ' Close everything.
- stream.Close()
- client.Close()
- Catch e As ArgumentNullException
- Console.WriteLine("FEHLER - ERROR: {0}", e)
- Catch e As SocketException
- Console.WriteLine("FEHLER - ERROR: {0}", e)
- End Try
- Console.WriteLine(ControlChars.Cr + " Bitte ENTER drücken...")
- Console.Read()
- End Sub 'Connect
- End Module