Socket programming for vb.net

Source: Internet
Author: User

(1) Client:

' Send

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim bytes (1024) as byte array
Dim Sender1 as New System.Net.Sockets.Socket (Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, NET.SOCKETS.PROTOCOLTYPE.TCP)
' Initialize socket
Dim msg as Byte () = System.Text.Encoding.Unicode.GetBytes (TextBox1.Text)
' Encode the data sent
'***************************
' Specify IP and port
Dim Iphostinfo as System.Net.IPHostEntry = System.Net.Dns.GetHostEntry ("192.168.22.131")
Dim IPAddress as System.Net.IPAddress = iphostinfo.addresslist (0)
Dim Ipe as New System.Net.IPEndPoint (IPAddress, 11000)
'**********************
Sender1. Connect (IPE) ' Establish a connection
Dim bytessent as Integer = Sender1. Send (MSG) ' sends data
' Close socket
Sender1. Shutdown (Net.Sockets.SocketShutdown.Both)
Sender1. Close ()
End Sub

(2) server-side:

Dim Listener as New System.Net.Sockets.Socket (Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream , NET.SOCKETS.PROTOCOLTYPE.TCP)
' Initial socket

' Receive
Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim bytes () as Byte
Dim handler as System.Net.Sockets.Socket = Listener. Accept () ' Establish a connection request

        Dim data as String = Nothing
         bytes = New Byte (1024) {}
        Dim bytesrec as Integer = handler. Receive (bytes) ' Receiving data
        If bytesrec > 0 Then
             data = System.Text.Encoding.Unicode.GetString (bytes, 0, Bytesrec)
            TextBox1.Text = Data
         Else
            Exit Sub
        End If
        handler. Shutdown (Net.Sockets.SocketShutdown.Both)
        handler. Close ()
    end Sub

Private Sub Form1_Load (ByVal sender as Object, ByVal e as System.EventArgs) Handles MyBase.Load
' Specify IP and port
Dim Iphostinfo as System.Net.IPHostEntry = System.Net.Dns.GetHostEntry (System.Net.Dns.GetHostName ())
Dim IPAddress as System.Net.IPAddress = iphostinfo.addresslist (0)
Dim Localendpoint as New System.Net.IPEndPoint (IPAddress, 11000)
Listener. Bind (Localendpoint)
Listener. Listen (10)
End Sub

'-------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------

Summarized as follows:

Imports System.Net
Imports System.Net.Sockets
Imports System.Net.Dns

1. This machine ip:127.0.0.1 broadcast ip:255.255.255.255.

Applying udpclient to implement socket

Broadcast send:

Dim udpclient as UdpClient = New udpclient ()
Dim EP as IPEndPoint = New IPEndPoint (ipaddress.parse ("255.255.255.255"), 8888)

Dim Defaultip as String = "127.0.0.1"
Dim IP as IPAddress

        for each IP in Dns.gethostentry (Dns.gethostname ()). AddressList
                If IP. AddressFamily = addressfamily.internetwork Then
                     defaultip = IP. ToString ()
                     Exit for
                 End If
            Next

Dim ComputerInfo as String = ": USER:" & Txtuser & ": & Dns.gethostname () &": "& Defaultip &": " & Txtgroup
Dim Buff as Byte () = Encoding.Default.GetBytes (ComputerInfo)
ComputerInfo = Nothing

Udpclient.send (buff, buff. Length, EP)
System.Threading.Thread.Sleep (2000)
Buff = Nothing

Broadcast reception:

Dim server as UdpClient = New udpclient (8888)
Dim EP as IPEndPoint = New ipendpoint (ipaddress.any, 0)

Dim Buff as Byte () = server. Receive (EP)
Dim user as String = Encoding.Default.GetString (buff, 0, buff.) Length)
Dim cmd as String = user. Substring (0, 6)
Dim user1 as String = user. Substring (6)

Dim s as String () = user1. Split (":")

2.Socket Send:

Dim iphostname as String = Me.ListViewUser.Items (i). SubItems (2). Text
' Initialization accept Socket: Addressing scheme, character stream mode and TCP communication (IP address can not be used in System.net.socket.gethostentry (dns.gethostname) Form, so CE to the CE and PC send information are not possible)
Socketsent = New System.Net.Sockets.Socket (Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, NET.SOCKETS.PROTOCOLTYPE.TCP)
Ipsent = New IPEndPoint (System.Net.IPAddress.Parse (iphostname), 8887)

' Connect to the server
Socketsent.connect (ipsent)

' Converts the message to be sent to a byte stream, and then sends
Socketsent.send (Encoding.Default.GetBytes (Sendmsg.trim ()))

Socketsent.close ()

Send procedure: Declare socket Object--Get the device IP and port to connect (IPEndPoint object)--Connect connect--send sent

3.socket Receive

' Initialize accept Socket: Addressing scheme, character stream mode and TCP communication
Socketreceive = New System.Net.Sockets.Socket (Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, NET.SOCKETS.PROTOCOLTYPE.TCP)

' Get the native IP address and set the port that accepts the information
Dim Defaultip as String = "127.0.0.1"
Dim IP as IPAddress

            for all IP in Dns.gethostentry ( Dns.gethostname ()). AddressList
                If IP. AddressFamily = addressfamily.internetwork Then
                     defaultip = IP. ToString ()
                     Exit for
                 End If
            Next

Ipreceive = New IPEndPoint (ipaddress.parse (Defaultip), 8887)
' Bind native IP addresses and accept ports to accept sockets
Socketreceive.bind (ipreceive)
' Listen to the port and set the maximum length of the listening queue
Socketreceive.listen (1024)

Receive procedure: Declare socket Object--Get native device IP and port (IPEndPoint object)--Bind bind--Monitor Listen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.