用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 关于路由器后TCP或UDP通讯问题 假设一台电脑192.168.0.1位于路由1.端口不NAT,两个子网中的两端之间不可能通信 。至少要有一方处在公网下或有路由端口映射,不必双方都映射 。
建议题主找本计算机网络的书研读一下子网划分这块儿 。
vb.net TCP协议服务端ip怎么写才能使客户端能通过公网连接进来?需要出口设备进行映射内部地址;或使用打洞技术穿透NAT,但使用TCP打洞非常困难 。
参考
穿透NAT是非常困难的,因为NAT的过程中在出口路由器上的Port已经不是你定义的iPort了 , 具体是多少也不固定
求 VB.Net的TCP/IP通讯实例,最好包括客户端请求连接与处理来自服务器的响应相关代码服务器和客户端都有vb.nettcp连接,但是都是很基本vb.nettcp连接的代码vb.nettcp连接,可以作为参考学习 。。。
vb.net2005 tcp聊天程序问题‘客户端向这样写vb.nettcp连接:在窗体说放两个文本框、两个标签、两个按钮、一个列表框
Imports System.Net
Imports System.Net.Sockets
Imports System.IO
Imports System.Threading
Public Class Form1
'网络基础数据流
Private ns As NetworkStream
Private swriter As StreamWriter
Private nsread As StreamReader
Private tcpclient As TcpClient
Private tcpconnected As Boolean = False
Private clisocket As Socket
Private mythread As Thread
'发送
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.TextBox2.Text"" Then
swriter.WriteLine(Me.TextBox2.Text)
swriter.Flush()
Me.TextBox2.Text = ""
Else
MessageBox.Show("发送信息不能为空vb.nettcp连接!", "错误提示")
End If
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
推荐阅读
- 电脑群直播签到怎么签,直播签到表怎么做
- sqlserver数据库文件夹,sqlserver 数据文件
- 鸿蒙桌面创意布局加文字,鸿蒙桌面创意布局加文字怎么弄
- 开直播要具备什么工具,开直播要具备什么工具和技术
- php升级数据库 php数据库更新语句
- 智慧电视连接功放机怎么连,智能电视怎样连接功放机
- 便宜的国外服务器推荐吗,国外服务器买哪个好用
- 新联想电脑怎么换系统,联想电脑更换系统设置
- 字符串函数在c语言中 c语言字符串函数用法