2016年4月6日 星期三

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


沒有留言:

張貼留言