VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > vb >
  • vb.net的socket编程

(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

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

总结如下:

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

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

广播接收:

           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发送:

    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()

发送过程:声明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)

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/angxiao/article/details/6969301

相关教程