vb.netip通信 vbnet opc通讯

求 VB.Net的TCP/IP通讯实例,最好包括客户端请求连接与处理来自服务器的响应相关代码服务器和客户端都有vb.netip通信,但是都是很基本vb.netip通信的代码vb.netip通信 , 可以作为参考学习 。。。
用vb.net实现网络通信用 vb.net socket通信
Dim th As Threading.Thread
2Dim tcpl As System.Net.Sockets.TcpListener
3
4Private Sub Form1_Load()Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
5th = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf MyListen))
6th.Start()
7End Sub
8
9Public Sub SendMessage()Sub SendMessage(ByVal IP As String, ByVal SendMsg As String)
10Try
11If IP"" Then
12Dim tcpc As New System.Net.Sockets.TcpClient(IP, 5656)
13Dim tcpStream As Net.Sockets.NetworkStream = tcpc.GetStream
14Dim reqStream As New IO.StreamWriter(tcpStream)
15reqStream.Write(SendMsg)
16reqStream.Flush()
17tcpStream.Close()
18tcpc.Close()
19End If
20Catch ex As Exception
21MsgBox(ex.Message.ToString)
22End Try
23End Sub
24Private Sub MyListen()Sub MyListen()
25Try
26Dim ipAddress As System.Net.IPAddress = System.Net.Dns.Resolve(System.Net.Dns.GetHostName).AddressList(0)
27tcpl = New System.Net.Sockets.TcpListener(ipAddress, 5656)
28tcpl.Start()
29While True
30Dim s As System.Net.Sockets.Socket = tcpl.AcceptSocket()
31Dim MyBuffer(1024) As Byte
32Dim i As Integer
33i = s.Receive(MyBuffer)
34If i0 Then
35Dim lstrRec As String
36Dim j As Integer
37For j = 0 To i - 1
38TextBox1.Text= Chr(MyBuffer(j))","
39Next
40End If
41End While
42Catch ex As Exception
43MsgBox(ex.Message.ToString)
44End Try
45End Sub
46
47Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
48SendMessage("192.168.0.61", TextBox2.Text)
49End Sub
VB.NET 2003中 想做一个基于TCP/IP的网络通信程序就可以vb.netip通信了vb.netip通信,我有一段不太成熟vb.netip通信的代码vb.netip通信:
ImportsSystem.Net
ImportsSystem.Net.Sockets
ImportsSystem.Threading
ImportsSystem.IO
PublicClassForm1
DimTCPListenerRun,TCPClientRunAsBoolean
DimControlTCPClient,ServerClientAsTcpClient
DimControlTCPListenerAsTcpListener
DimTCPClientT,TCPListenerTAsThread
PrivateControlStream,SControlStreamAsNetworkStream
PrivateControlReadStream,SControlReadStreamAsStreamReader
PrivateControlWriteStream,SControlWriteStreamAsStreamWriter
'异步委托
PrivateDelegateSubInvokeDelegate(ByValiMessageAsString)
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Try
IfTCPListenerRunThen
TCPListenerT.Abort()
ControlTCPListener.Stop()
TCPListenerRun=False
Button1.Text="启动服务端 "
TextBox2.AppendText( "服务器已停止vb.netip通信! "vbCrLf)
Else
ControlTCPListener=NewTcpListener(IPAddress.Parse( "127.0.0.1 "),13000)
ControlTCPListener.Start()
TCPListenerT=NewThread(AddressOfListen)
TCPListenerT.Start()
TextBox2.AppendText( "等待连接... "vbCrLf)
TextBox2.AppendText( "服务器已启动! "vbCrLf)
TCPListenerRun=True
Button1.Text="停止服务端 "
EndIf
CatchexAsException
TextBox2.AppendText(ex.MessagevbCrLf)
EndTry
EndSub
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
Try
IfTCPClientRunThen
TCPClientT.Abort()
ControlTCPClient.Close()
TCPClientRun=False
Button2.Text="启动客户端 "
Else
ControlTCPClient=NewTcpClient(NewIPEndPoint(IPAddress.Parse( "127.0.0.1 "),13001))
ControlTCPClient.Connect(NewIPEndPoint(IPAddress.Parse( "127.0.0.1 "),13000))
TCPClientRun=True
Button2.Text="停止客户端 "
TCPClientT=NewThread(AddressOfReceive)
TCPClientT.Start()
ControlStream=ControlTCPClient.GetStream()
ControlReadStream=NewStreamReader(ControlStream)
ControlWriteStream=NewStreamWriter(ControlStream)
EndIf
CatchexAsSocketException
'Ifex.ErrorCode=10048Then
ControlTCPClient.Close()
'EndIf
TCPClientRun=False
Button2.Text="启动客户端 "
TextBox2.AppendText(ex.MessagevbCrLf)
EndTry
EndSub
PrivateSubListen()
ServerClient=ControlTCPListener.AcceptTcpClient
TextBox2.BeginInvoke(NewInvokeDelegate(AddressOfInvokeMethod_Receive),"成功连接... ")
SControlStream=ServerClient.GetStream()
SControlReadStream=NewStreamReader(SControlStream)
SControlWriteStream=NewStreamWriter(SControlStream)
WhileTrue
TextBox2.BeginInvoke(NewInvokeDelegate(AddressOfInvokeMethod_Receive),SControlReadStream.ReadLine())
EndWhile
EndSub
PrivateSubReceive()
WhileTrue
TextBox2.BeginInvoke(NewInvokeDelegate(AddressOfInvokeMethod_Receive),ControlReadStream.ReadLine())
'Console.WriteLine(ControlTCPClient.Available)
EndWhile
EndSub
PrivateSubForm1_Load(ByValsenderAsObject,ByValeAsSystem.EventArgs)HandlesMe.Load
TCPListenerRun=False
TCPClientRun=False
EndSub
PrivateSubInvokeMethod_Receive(ByValiMessageAsString)
TextBox2.AppendText(iMessagevbCrLf)
EndSub
PrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
ControlWriteStream.WriteLine(TextBox1.Text)
ControlWriteStream.Flush()
EndSub
PrivateSubButton4_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton4.Click
SControlWriteStream.WriteLine(TextBox3.Text)
SControlWriteStream.Flush()
EndSub
EndClass
VB.NET 如何通过TCP/IP协议通讯实现16进制数据的接收?传输Integer 数据类型来传输比较方便.因为Integer vb.netip通信的取值可以是0x0000-0xFFFF .当vb.netip通信你把数据发送到目vb.netip通信的主机后可以利用Hex() 把接收到vb.netip通信的数据从十进制转换到十六进制..
【vb.netip通信 vbnet opc通讯】关于vb.netip通信和vbnet opc通讯的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读