vb.net网路通信 vbnet tcp通讯

用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与VB.NET串口及以太网通迅关于串口通讯vb.net网路通信的问题vb.net网路通信:
很明显vb.net网路通信,你还不知道(不会、不习惯)使用事件驱动的方式接收数据 。
【vb.net网路通信 vbnet tcp通讯】建议你仔细看看串口组件(无论VB6还是VB.net)的OnComm事件,你的问题很容易解决 。
关于以太网通讯:
在TCP通讯中端口确实可以重用,你百度一下“TCP端口重用”能查到很多示例 。
但绝大多数情况下不推荐端口重用,而应该采取服务器端建立连接池的方法 。
或者,干脆不用TCP,用UDP解决也可以 。
VB.Net 关于路由器后TCP或UDP通讯问题 假设一台电脑192.168.0.1位于路由1.端口不NATvb.net网路通信,两个子网中vb.net网路通信的两端之间不可能通信 。至少要有一方处在公网下或有路由端口映射vb.net网路通信,不必双方都映射 。
建议题主找本计算机网络的书研读一下子网划分这块儿 。
关于vb.net网路通信和vbnet tcp通讯的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读