用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
【vb.net网络通信 vb局域网通信】 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.net网络通信了vb.net网络通信 , vb.net网络通信我有一段不太成熟的代码:
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="停止客户端 "
推荐阅读
- 虎牙直播在哪里直播间,虎牙直播在哪里直播间进去
- 查看mongodb吞吐量命令,mongodb命令行查询
- python中取数函数 python中取整数的方法
- java分页代码查询页面,java分页查询原理思路
- 影驰怎么判断显卡真伪,影驰怎么判断显卡真伪鉴别
- java的动态壁纸代码 java的动态壁纸代码怎么用
- 微信是flutter吗,微信是开源的吗
- 用excel怎么加误差线,excel如何添加误差线
- 竞速游戏steam,竞速游戏手游