vb.net中tcp的简单介绍

vb.net TCP协议服务端ip怎么写才能使客户端能通过公网连接进来?需要出口设备进行映射内部地址;或使用打洞技术穿透NAT,但使用TCP打洞非常困难 。
参考
穿透NAT是非常困难的,因为NAT的过程中在出口路由器上的Port已经不是你定义的iPort了,具体是多少也不固定
VB.Net 关于路由器后TCP或UDP通讯问题 假设一台电脑192.168.0.1位于路由1.端口不NAT,两个子网中的两端之间不可能通信 。至少要有一方处在公网下或有路由端口映射,不必双方都映射 。
建议题主找本计算机网络的书研读一下子网划分这块儿 。
VB.NET的TCP/IP协议 怎么弄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
【vb.net中tcp的简单介绍】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的网络通信程序就可以了,我有一段不太成熟的代码:
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( "服务器已停止! "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的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、vb.net中tcp的信息别忘了在本站进行查找喔 。

    推荐阅读