用 vb.net socket通信
創(chuàng)新互聯(lián)專(zhuān)注于企業(yè)網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣、網(wǎng)站重做改版、伊春網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5高端網(wǎng)站建設(shè)、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷(xiāo)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為伊春等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。
Dim th As Threading.Thread
2 Dim tcpl As System.Net.Sockets.TcpListener
3
4 Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5 th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6 th.Start()
7 End Sub
8
9 Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10 Try
11 If IP "" Then
12 Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13 Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14 Dim reqStream As New IO.StreamWriter(tcpStream)
15 reqStream.Write(SendMsg)
16 reqStream.Flush()
17 tcpStream.Close()
18 tcpc.Close()
19 End If
20 Catch ex As Exception
21 MsgBox(ex.Message.ToString)
22 End Try
23 End Sub
24 Private Sub MyListen()Sub MyListen()
25 Try
26 Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27 tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28 tcpl.Start()
29 While True
30 Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31 Dim MyBuffer(1024) As Byte
32 Dim i As Integer
33 i = s.Receive(MyBuffer)
34 If i 0 Then
35 Dim lstrRec As String
36 Dim j As Integer
37 For j = 0 To i - 1
38 TextBox1.Text += Chr(MyBuffer(j)) ","
39 Next
40 End If
41 End While
42 Catch ex As Exception
43 MsgBox(ex.Message.ToString)
44 End Try
45 End Sub
46
47 Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48 SendMessage("192.168.0.61", TextBox2.Text)
49 End Sub
關(guān)于串口通訊的問(wèn)題:
很明顯,你還不知道(不會(huì)、不習(xí)慣)使用事件驅(qū)動(dòng)的方式接收數(shù)據(jù)。
建議你仔細(xì)看看串口組件(無(wú)論VB6還是VB.net)的OnComm事件,你的問(wèn)題很容易解決。
關(guān)于以太網(wǎng)通訊:
在TCP通訊中端口確實(shí)可以重用,你百度一下“TCP端口重用”能查到很多示例。
但絕大多數(shù)情況下不推薦端口重用,而應(yīng)該采取服務(wù)器端建立連接池的方法。
或者,干脆不用TCP,用UDP解決也可以。
這個(gè)很正常,因?yàn)榉?wù)器(對(duì)方)還沒(méi)對(duì)客戶端(本地)做Close動(dòng)作。還有一個(gè)說(shuō)法是本地端控件的緩存還存在著與服務(wù)器對(duì)應(yīng)Handle,只是State屬性狀態(tài)不是sckConnected而已。繼續(xù)進(jìn)行測(cè)試,你也會(huì)發(fā)現(xiàn)只要超過(guò)60秒通信閑置,也會(huì)自動(dòng)Not sckConnected。
TCP連線,可以使用Load 控件數(shù)組進(jìn)行嘗試連線,當(dāng)控件Close后就Unload,這可以取代必須關(guān)閉窗體、重新打開(kāi)連接的問(wèn)題。
看下這個(gè)異常的類(lèi)型 在捕捉異常的時(shí)候處理為關(guān)閉并重新打開(kāi)端口監(jiān)聽(tīng)(這個(gè)看你自己需求) 然后在socketException下面再catch其它exception 進(jìn)行彈出錯(cuò)誤提示之類(lèi)的操作 就可以了
客戶端簡(jiǎn)單代碼:
Imports System.Net.Sockets
Imports System.IO
Private Output As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Dim StrSend As String
Dim client As New TcpClient '
client.Connect(servIP, 2006)
Output = client.GetStream
Writer = New BinaryWriter(Output)
Reader = New BinaryReader(Output)
StrSend = "字符串"
System.Threading.Thread.Sleep(100)
Writer.Write(StrSend)
Try
Message = Reader.ReadString
If Message = "OK" Then
LBinfo.Text = "成功!" '測(cè)試
Else
LBinfo.Text = "失?。?
End If
System.Threading.Thread.Sleep(100)
Catch ex As Exception
MessageBox.Show("Client Application Closing!")
Finally
Writer.Close()
Reader.Close()
Output.Close()
client.Close()
End Try
服務(wù)器端
用線程
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Imports System.Windows.Forms
Private Connection As Socket
Private readThread As Thread
Private SocketStream As NetworkStream
Private Writer As BinaryWriter
Private Reader As BinaryReader
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent()
'在 InitializeComponent() 調(diào)用之后添加任何初始化
readThread = New Thread(AddressOf RunServer)
readThread.Start()
End Sub
Public Sub RunServer()
Dim Listener As TcpListener
Try
Listener = New TcpListener(Net.IPAddress.Parse("192.168.1.16"), 2006)
Listener.Start()
While True
Connection = Listener.AcceptSocket 'accept an incoming connection
SocketStream = New NetworkStream(Connection) 'create networkstream object associated with socket
Writer = New BinaryWriter(SocketStream) 'create object for transferring data across stream
Reader = New BinaryReader(SocketStream) 'read string data sent from client
Try
Do
strIncept = Reader.ReadString
strIncept = DataDisp(strIncept) '此處插入數(shù)據(jù)處理程序()
SendInfoToClient(strIncept) 'send to client
Loop While Connection.Connected
Catch ex As Exception
MsgBox(ex.ToString)
Finally
Writer.Close()
Reader.Close()
SocketStream.Close()
End Try
End While
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Sub SendInfoToClient(ByVal Info As String)
'Send to client
Try
If Connection.Connected Then
Writer.Write(Info)
Connection.Close()
End If
Catch ex As SocketException
MessageBox.Show(ex.ToString)
End Try
End Sub
整個(gè)過(guò)程都有了
自學(xué)還是必要的
你去看看TcpServerChannel TcpClient類(lèi)
msdn鏈接地址