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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值