关键词搜索

源码搜索 ×
×

vb.net的socket编程

发布2021-02-13浏览862次

详情内容

(1) 客户端:

‘发送

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bytes(1024) As Byte '声明字节数组
        Dim sender1 As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        '初始化socket
        Dim msg As Byte() = System.Text.Encoding.Unicode.GetBytes(TextBox1.Text)
        '对发送的数据进行编码
        '***************************
        '指定ip和端口
        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) '建立连接
        Dim bytesSent As Integer = sender1.Send(msg) '发送数据
        '关闭socket
        sender1.Shutdown(Net.Sockets.SocketShutdown.Both)
        sender1.Close()
    End Sub

    (2)服务器端:

     Dim listener As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
        '初始socket
    
     ‘接收
        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() '建立连接请求
    
            Dim data As String = Nothing
            bytes = New Byte(1024) {}
            Dim bytesRec As Integer = handler.Receive(bytes) '接收数据
            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
            '指定ip和端口
            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
    
      21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

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

    总结如下:

    Imports System.Net
    Imports System.Net.Sockets
    Imports System.Net.Dns
    
    • 1
    • 2
    • 3

    1.本机ip:127.0.0.1;广播IP:255.255.255.255。

    应用UdpClient实现socket

    广播发送:

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    广播接收:

    Dim server As UdpClient = New UdpClient(8888)
    Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
    
    • 1
    • 2

    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(":")
    
    • 1

    2.Socket发送:

        Dim IpHostName As String = Me.ListViewUser.Items(i).SubItems(2).Text
       '初始化接受套接字:寻址方案,以字符流方式和Tcp通信(IP地址不能用system.net.socket.gethostentry(dns.gethostname)形式,这样CE给CE和PC发信息都发不通)
      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)
    
    '与服务器进行连接
                                    socketSent.Connect(ipSent)
    
    '将要发送的消息转化为字节流,然后发送
                                    socketSent.Send(Encoding.Default.GetBytes(SendMsg.Trim()))
    
    socketSent.Close()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    发送过程:声明socket对象——获取要连接的设备IP和端口(IPendPoint对象)——连接connect——发送Send

    3.socket接收

     '初始化接受套接字:寻址方案,以字符流方式和Tcp通信
                socketReceive = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
    
    '获取本机IP地址并设置接受信息的端口
                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
    
               ipReceive = New IPEndPoint(IPAddress.Parse(DefaultIP), 8887)
                '将本机IP地址和接受端口绑定到接受套接字
                socketReceive.Bind(ipReceive)
                '监听端口,并设置监听队列最大长度
                socketReceive.Listen(1024)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    接收过程:声明socket对象——获取本机设vb.net教程备IP和端口(IPendPoint对象)——绑定Bind——监听listen

    相关技术文章

    点击QQ咨询
    开通会员
    返回顶部
    ×
    微信扫码支付
    微信扫码支付
    确定支付下载
    请使用微信描二维码支付
    ×

    提示信息

    ×

    选择支付方式

    • 微信支付
    • 支付宝付款
    确定支付下载