Netzwerkkommunikation mit VB.NET

  • Hi,

    hab jetzt n 3 Wochen langes Seminar mit Objektorientierter Programmierung in VB.net hinter mir.
    Da mir das riesig spaß macht möcht ich noch selbst noch ein wenig weiter machen.

    Ich würde gerne mal zur Kommunikation übers Netzwerk n paar infos haben,
    TCP/ UDP und was ich mal gehört habe "Winsock".
    Leider finde ich nur Source codes zu VB6 wo man nicht heraus erschließen
    kann woher jetzt Winsock kommt.
    (ob das importiert werden muss, einstellungssache etc.)

    Wäre cool wenn ihr da ein paar auf lager hättet!

    Ich möchte erst mal die Grundlagen lernen,
    einfache Strings über Netzwerk schicken solche sachen

    Gruß

  • Ih Winsock .. :D

    Client Beispiel:

    Code
    Imports System.Net.SocketsImports System.TextClass TCPCli    Shared Sub Main()        Dim tcpClient As New System.Net.Sockets.TcpClient()        tcpClient.Connect("127.0.0.1", 8000)        Dim networkStream As NetworkStream = tcpClient.GetStream()        If networkStream.CanWrite And networkStream.CanRead Then            ' Do a simple write.            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")            networkStream.Write(sendBytes, 0, sendBytes.Length)            ' Read the NetworkStream into a byte buffer.            Dim bytes(tcpClient.ReceiveBufferSize) As Byte            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))            ' Output the data received from the host to the console.            Dim returndata As String = Encoding.ASCII.GetString(bytes)            Console.WriteLine(("Host returned: " + returndata))        Else            If Not networkStream.CanRead Then                Console.WriteLine("cannot not write data to this stream")                tcpClient.Close()            Else                If Not networkStream.CanWrite Then                    Console.WriteLine("cannot read data from this stream")                    tcpClient.Close()                End If            End If        End If        ' pause so user can view the console output        Console.ReadLine()    End SubEnd Class


    Server Beispiel:

  • Oh ne das hatt ich auch schon ^^


    Ich wusst nicht das das Winsock ist, ich habs leider nur mit einem String geschafft. Einmal
    Naja dann vertief ich mich nochmal rein xD

    Weil du sagst "Ih Winsock" was gibt es denn für alternativen?

  • "Ih Winsock" Bezieht sich auf VB6... ich finde in VB.NET ist das wesentlich einfacher / schöner mit TCP zu Arbeiten.

    Ok, wie gesagt bin ja kein Profi.

    Komm auch langsam mit deinem Beispielcode zurecht,
    also umgesetzt in ein einfaches Netzwerk "Stein Schere Papier" spiel

  • Hi ich habe eine frage wie kan ich in vb 2008 express machen ein text oda comboBox zu "ip" oda "port" inputbox ich hab alles mrgliche schon ausprobiert :nein::hm::grübeln: ps. sorry for bad german :D lang.

  • Naja wenn du bedenkst dass das thema nun fast 1 Jahr alt ist,
    solltest du dich nicht über die lange Wartezeit wundern.

    Leider versteh ich dein Problem nicht ^^

    Aber falls du für die IP Eingabe ein Textfeld oder eine Combobox benutzen
    willst dann weiß ich vll Rat. Du darfst die IP einfach nicht als String in deinem
    Sourcecode hart kodieren, sonder stattdessen jedes mal auf das Textfeld zugreifen.

    Alt:

    Code
    tcpClient.Connect("127.0.0.1", 8000)

    Neu 1 (Textbox):

    Code
    tcpClient.Connect(textbox1.text, 8000)

    oder

    Neu 2 (Combobox):

    Code
    tcpClient.Connect(combobox1.SelectedItem.text, 8000)
  • ok ich hab das geschaft abba jetz hab di nexte proiblem :(

    Code
    Private Sub ClConnect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClConnect.Click        If ClCheck.Enabled = True Then            Dim IpLis As IPAddress = IPAddress.Parse(ClIp.Text)            Dim PortLis As Int32            PortLis = ClPort.Text            Dim TcpListener As New System.Net.Sockets.TcpListener(IpLis, PortLis)            TcpListener.Start()            TcpListener.AcceptSocket()        End If        If ClCheck.Enabled = False Then            Dim TcpClient As New System.Net.Sockets.TcpClient            Dim Ip As IPAddress = IPAddress.Parse(ClIp.Text)            Dim Port As Int32            Port = ClPort.Text            ClIp.Text = AccessibleRole.IpAddress            TcpClient.Connect(Ip, Port)        End If


    so in dise code client solte if ein check box aktiviert ist er solte listnen auf connections und if dise checkbox ist nicht aktiviert er solte connect normal to (Ip,Port) und eigentlich to krige ich problem in dise linie

    Code
    TcpListener.Start()

Jetzt mitmachen!

Sie haben noch kein Benutzerkonto auf unserer Seite? Registrieren Sie sich kostenlos und nehmen Sie an unserer Community teil!