请教一个基于UDP的程序,多客户端对一服务端的
1个回答
展开全部
UDP因为是一种无连接的服务,没有服务器和客户端的概念,实际上只需要把数据发出去就可以了;
给你一个VB的程序参考下;
Public Class UDPClient2015
Private Declare Sub Sleep Lib "Kernel32" (ByVal NextRunProgramTimeAsMs As Long)
Dim remoteip As String = ""
Dim localip As String = ""
Dim count As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "10.33.132.25"
Button1.Text = "发送"
Button2.Text = "连接"
Button3.Text = "一直发送"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
localip = UDPClient1.LocalIP
If Button2.Text = "连接" Then
remoteip = TextBox1.Text
With UDPClient1
.Protocol = MSWinsockLib.ProtocolConstants.sckUDPProtocol
.RemoteHost = remoteip
.RemotePort = 2014
.LocalPort = 2015
.Bind(2015, localip)
End With
Button2.Text = "关闭连接"
TextBox1.ReadOnly = True
Else
With UDPClient1
.Close()
End With
Button2.Text = "连接"
TextBox1.ReadOnly = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UDPClient1.SendData(TextBox3.Text)
TextBox2.Text = TextBox2.Text & TextBox3.Text & vbCrLf
TextBox3.Text = ""
End Sub
Private Sub UDPClient1_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles UDPClient1.DataArrival
Dim strData As String = ""
UDPClient1.GetData(strData)
TextBox2.Text = TextBox2.Text & strData & vbCrLf
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Button3.Text = "一直发送" Then
Button3.Text = "停止发送"
Timer1.Start()
Else
Button3.Text = "一直发送"
Timer1.Stop()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
UDPClient1.SendData(200)
End Sub
End Class
Storm代理
2023-06-05 广告
2023-06-05 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,IP...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询