如题,请高手不吝赐教!
解决方案 »
- vb控件
- 如何捕捉 dbADO.Open(链接sql 服务器) 时的错误??
- 如果源程序正在运行,如何将正在运行的源程序置成当前窗口
- treeview1.Nodes(1).Visible = True 实时错误'383' 属性只读?
- 请问:如何使执行后的程序 在按Ctrl+Alt+Del弹出的关闭程序列表中看不到?
- ImageList控件与TreeView控件联系,运行几次后,ImageList控件的图片老变黑...
- 谁能告诉我CoolMenu V3.0.24a 的注册码?急用。在线等待!!谢了
- 關於InstallShield
- 小问题
- 论证VB缺陷,来者有分
- AutoCAD的Line点不重复排序
- 诚拜师傅一名~
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ?1996-2006 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Const OPEN_EXISTING = 3
Private Const GENERIC_WRITE = &H40000000
Private Const FILE_SHARE_READ = &H1
Private Const FILE_ATTRIBUTE_NORMAL = &H80Private Const vbDotEveryone As String = "*"'User-defined type for passing
'the data to BroadcastMessage
Private Type MailslotMessageData
sMsgFrom As String
sSendTo As String
sMessage As String
End TypePrivate Declare Function CreateFile Lib "kernel32" _
Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long) As LongPrivate Declare Function WriteFile Lib "kernel32" _
(ByVal hFile As Long, _
ByVal lpBuffer As Any, _
ByVal nNumberOfBytesToWrite As Long, _
lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hHandle As Long) As LongPrivate Sub Form_Load() Text1.Text = "爱爱爱"
Text2.Text = "*"
With Text3
' .MaxLength = 128
.Text = "祝大家新春愉快,恭喜发财!"
End With
Command1.Caption = "Send Message"
End Sub
Private Sub Command1_Click()
Dim msg As MailslotMessageData
'fill the structure with the
'message data
Label1.Caption = ""
With msg
.sMsgFrom = Text1.Text
.sSendTo = Text2.Text
.sMessage = Text3.Text
End With
'return the success to a label
Label1.Caption = BroadcastMessage(msg)
End Sub
Private Function BroadcastMessage(msg As MailslotMessageData) As String Dim hFile As Long
Dim byteswritten As Long
Dim buff As String
Dim sSlotName As String
'ensure message type has data
'and either abort transmission
'or set default values
If Len(msg.sMessage) = 0 Then
BroadcastMessage = "No message specified; nothing to do."
Exit Function
End If
If Len(msg.sSendTo) = 0 Then
msg.sSendTo = vbDotEveryone
End If
If Len(msg.sMsgFrom) = 0 Then
msg.sMsgFrom = "Mailslot System Message"
End If 'must be something to do, so...
'the mailslot name is a combination of
'the receiving machine name prefixed
'with double slashes, followed by
'\mailslot\messngr
sSlotName = "\\" & msg.sSendTo & "\mailslot\messngr"
'the message transmitted is a
'combination of the from, to and
'message strings separated by null
'characters
buff = msg.sMsgFrom & vbNullChar & _
msg.sSendTo & vbNullChar & _
msg.sMessage & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar _
& vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar & vbNullChar
' buff = msg.sMsgFrom & vbNullChar & _
' msg.sSendTo & vbNullChar & _
' msg.sMessage & vbNullChar & vbNullChar
'obtain a handle to the mailslot
hFile = CreateFile(sSlotName, _
GENERIC_WRITE, _
FILE_SHARE_READ, _
0&, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL, _
0&)
'if it's a go ...
If hFile <> 0 Then
'write the message to the slot and close the file
If WriteFile(hFile, _
buff, _
Len(buff), _
byteswritten, _
0) <> 0 Then
If (Len(buff) = byteswritten) Then
BroadcastMessage = "The message was successfully sent."
Else
BroadcastMessage = "Message sent but bytes written <> message size."
End If 'If (Len(buff)
Else
BroadcastMessage = "Error writing the message."
End If 'If WriteFile
Call CloseHandle(hFile)
End If 'If hFileEnd Function
使用Winsock的一个例子:
Option ExplicitPrivate Sub Form_Load()
Winsock1.Protocol = sckUDPProtocol
Winsock1.RemoteHost = "255.255.255.255"
Winsock1.RemotePort = 8000
Winsock1.SendData "Hello!"
End
End Sub
如果UDP没有的话就用TCP协议吧,稳当些。
如果说没有消息的话就是你的安全问题。
你关掉服务器和客户端所有的防火墙看看。
程序本身是对的'这个应该是UDP客户端广播的程序
Private Sub Form_Load()
Winsock1.Protocol = sckUDPProtocol
Winsock1.RemoteHost = "255.255.255.255"
Winsock1.RemotePort = 8000
Winsock1.SendData "Hello!"
'End '这里退得可能太快了
End Sub
Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub
'这个应该是UDP服务端的程序
Private Sub Form_Load()
Winsock3.LocalPort = 8000
Winsock3.Bind
End Sub
Private Sub Winsock3_DataArrival(ByVal bytesTotal As Long)
Dim GStr As String
Winsock3.GetData GStr, vbSingle, bytesTotal
Msgbox GStr
End Sub
你可以这样测试,在局域网内用3台电脑,用两台电脑开两个服务器端程序
然后再另一台运行客户端,如果没有防火墙的阻止,那两台一定能收到信息