VB.Net抢答器设计(一)Client端

这个小程序是2010年我们社团在学校举行“Find Bug”编程竞赛决赛赛场上选手们抢答用的,实现的很简单没有多少复杂的功能,仅仅利用了UDP数据传输。至于服务端还要在下一篇中进行发布。
进来一直为即将毕业工作问题而伤脑筋,感觉自己知道的或者掌握的技术还是太有局限性了,作为专科,只能用硬技能去敲门,不像名牌大学的本科学生,哪怕没有太多的实践,公司愿意出时间来培养,也没办法,专科的悲哀···谁让自己高中不好好学习,整天老搞这个呢???看来学习好还是很有必要的。闲话少说吧,代码贴出来。暑假将至,祝各位假期快乐。对了,再罗嗦一句,项目使用Visual Studio2010开发的,语言VB.Net,其实和C#很相似啦···很容易转换一下的·····

Imports System.Net.NetworkInformation
Imports System.Net
Imports System.Text
Imports System.Net.Sockets
Imports System.Threading

Public Class Form1
Dim flag As Boolean = False
Private Const listenPort As Integer = 9095

Public Sub StartListener()
Dim done As Boolean = False
Dim i As String
Dim listener As New UdpClient(listenPort)
Dim groupEP As New IPEndPoint(IPAddress.Any, listenPort)
‘MsgBox(“ssdfjhasdjfhsdf”)
Try
While Not done
‘TextBox2.Text += “Waiting for broadcast:”

Dim bytes As Byte() = listener.Receive(groupEP)
‘TextBox2.Text += “Received broadcast from {0} :” + groupEP.ToString()
i = Encoding.ASCII.GetString(bytes, 0, bytes.Length)
flag = False
End While
Catch e As Exception
Console.WriteLine(e.ToString())
Finally
listener.Close()
‘Button3.Enabled = True
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim strReplay As String
Dim thePing As New Ping()
Dim ipaddress As String = TextBox1.Text
Dim thePingReplay As PingReply = thePing.Send(ipaddress)

strReplay = “服务器IP:” + thePingReplay.Address.ToString() + vbCrLf
strReplay += “缓冲区长度:” + thePingReplay.Buffer.Length().ToString() + vbCrLf
strReplay += “RoundTrip(Time):” + thePingReplay.RoundtripTime.ToString() + vbCrLf
strReplay += “Time to live(TTL):” + thePingReplay.Options.Ttl.ToString() + vbCrLf
strReplay += “Don’t Fragment:” + thePingReplay.Options.DontFragment.ToString() + vbCrLf
strReplay += “恢复状态值:” + thePingReplay.Status.ToString() + vbCrLf

TextBox2.Text = strReplay
Catch
MsgBox(“对不起,您输入的服务器IP不正确!”)
Finally

End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox2.Text = “”
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim intPort As Integer
Dim strIP As String
Dim myUdpDataSend As UdpDataSend
Dim strErrMessage As String
strIP = TextBox1.Text
intPort = Integer.Parse(TextBox4.Text)

myUdpDataSend = New UdpDataSend(strIP, intPort)
myUdpDataSend.SetSendMessage = TextBox3.Text
myUdpDataSend.SendData()
strErrMessage = myUdpDataSend.GetMessage
If Not strErrMessage.Length > 0 And Not TextBox1.Text = “” Then
MessageBox.Show(“本次抢答结束,请等待下次抢答!”, “抢答了···”)
flag = True
Else
MessageBox.Show(strErrMessage + “出错了~~~”, “抢答出错”)
End If
End Sub

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mythread As Thread
mythread = New Thread(AddressOf StartListener)
mythread.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If flag Then
Button3.Enabled = False
Else
Button3.Enabled = True
End If

End Sub

Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
‘ Determine if text has changed in the textbox by comparing to original text.
If True Then
‘ Display a MsgBox asking the user to save changes or abort.
If MessageBox.Show(“确定要退出抢答器吗?”, “退出确认”, MessageBoxButtons.YesNo) = DialogResult.Yes Then
‘ Cancel the Closing event from closing the form.
‘Thread.CurrentThread()
End
Else
e.Cancel = True
End If ‘ Call method to save file…
End If
End Sub ‘Form1_Closing

End Class

‘Class UdpRadio
‘    Dim mySocket As Socket
‘    Dim iep As IPEndPoint
‘    Dim ep As EndPoint
‘    Dim buffer(1024) As Byte
‘    Public Sub UdpRadioRec()
‘        mySocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp) ‘//初始化一个Scoket协议
‘        iep = New IPEndPoint(IPAddress.Any, 9095) ‘//初始化一个侦听局域网内部所有IP和指定端口
‘        ep = iep

‘        mySocket.Bind(iep) ‘//绑定这个实例
‘        MsgBox(“我收到了~~~~”)
‘        While True
‘            ‘buffer = new byte[1024] ‘//设置缓冲数据流
‘            mySocket.ReceiveFrom(buffer, ep) ‘//接收数据,并确把数据设置到缓冲流里面
‘            Form1.TextBox2.Text += Encoding.Unicode.GetString(buffer).TrimEnd(“u0000″) + ” ” + DateTime.Now.ToString()
‘            MsgBox(“我收到了~~~~”)
‘        End While
‘    End Sub
‘End Class

Class UdpDataSend
Private myUdpClient As UdpClient
Private strIP As String
Private SendMessage As String
Private intPort As Integer
Private ErrMessage As String = “”

Public Sub New(ByVal strIP As String, ByVal intPort As Integer)
Me.strIP = strIP
Me.intPort = intPort
End Sub

Public Sub SendData()
Try
Dim dataSend() As Byte
myUdpClient = New UdpClient(strIP, intPort)
dataSend = Encoding.Unicode.GetBytes(SendMessage)
myUdpClient.Send(dataSend, dataSend.Length)
myUdpClient.Close()
Catch ex As Exception
ErrMessage = ex.Message
End Try
End Sub

Public ReadOnly Property GetMessage() As String
Get
Return ErrMessage
End Get
End Property

Public WriteOnly Property SetSendMessage() As String
Set(ByVal value As String)
SendMessage = value
End Set
End Property
End Class

VB.Net抢答器设计(一)Client端》有一个想法

评论已关闭。