?? fudpexample.frm
字號:
VERSION 5.00
Begin VB.Form fUdpExample
BorderStyle = 1 'Fixed Single
Caption = "Winsock API Demo (UDP Example)"
ClientHeight = 4920
ClientLeft = 45
ClientTop = 330
ClientWidth = 5790
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 4920
ScaleWidth = 5790
StartUpPosition = 3 'Windows Default
Begin VB.CommandButton cmdClose
Caption = "&Close"
Height = 375
Left = 4560
TabIndex = 4
Top = 4440
Width = 1095
End
Begin VB.TextBox txtClient
Height = 1695
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 3
Top = 2520
Width = 5535
End
Begin VB.TextBox txtServer
Height = 1695
Left = 120
MultiLine = -1 'True
ScrollBars = 2 'Vertical
TabIndex = 2
Top = 360
Width = 5535
End
Begin VB.Label lblClientSocket
Caption = "Client socket handle:"
Height = 255
Left = 120
TabIndex = 1
Top = 2160
Width = 4575
End
Begin VB.Label lblServerSocket
Caption = "Server socket handle:"
Height = 255
Left = 120
TabIndex = 0
Top = 120
Width = 4575
End
End
Attribute VB_Name = "fUdpExample"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
'
' ---------------------------------------------------------------------------------
' File...........: fUdpExample.frm
' Author.........: Will Barden
' Created........: 04/06/03
' Modified.......: 04/07/03
' Version........: 1.0
' Website........: http://www.WinsockVB.com
' Contact........: admin@winsockvb.com
'
' A simple form to demonstrate sending and receiving data with UDP.
' ---------------------------------------------------------------------------------
'
' ---------------------------------------------------------------------------------
' Form events.
' ---------------------------------------------------------------------------------
'
Private Sub Form_Load()
'
Dim udtData As WSADATA
Dim hServerSocket As Long
Dim hClientSocket As Long
Dim udtLocal As sockaddr_in
Dim udtRemote As sockaddr_in
Dim udtBlank As sockaddr_in
Dim strData As String
Dim bytData() As Byte
Dim bytData2() As Byte
Dim lngCount As Long
'
' Start Winsock 2 and check the return for errors.
If (WSAStartup(WINSOCK_V2_2, udtData) = SOCKET_ERROR) Then
Call MsgBox("WSAStartup() failed")
Else
'
' Create a server socket - this will be used for receiving UDP data.
Call LogServer("1. Creating server socket")
hServerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
If (hServerSocket = INVALID_SOCKET) Then
Call LogServer("Socket creation failed")
Call WSACleanup
Exit Sub
End If
'
' Create the client socket, this will be used for sending UDP data.
Call LogClient("2. Creating client socket")
hClientSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
If (hClientSocket = INVALID_SOCKET) Then
Call LogClient("Socket creation failed")
Call closesocket(hServerSocket)
Call WSACleanup
Exit Sub
End If
'
' Setup a sockaddr_in to point to the localhost, and bind the server socket.
Call LogServer("3. Binding server socket to local addresses")
With udtLocal
.sin_family = AF_INET
.sin_addr.s_addr = vbInetAddr(INADDR_ANY)
.sin_port = htons(10101)
End With
Call bind(hServerSocket, udtLocal, LenB(udtLocal))
'
' Prepare a byte array with the data we want to send, and count the bytes.
Call LogClient("4. Sending data")
strData = "Isn't the Winsock API great?"
bytData = StrConv(strData, vbFromUnicode)
lngCount = UBound(bytData) - LBound(bytData) + 1
'
' Send the data to the local machine. To alter where this data is sent to,
' simply change the s_addr and sin_port fields of the sockaddr_in structure.
With udtRemote
.sin_family = AF_INET
.sin_addr.s_addr = vbInetAddr("127.0.0.1")
.sin_port = htons(10101)
End With
Call sendto(hClientSocket, bytData(0), lngCount, 0&, udtRemote, LenB(udtRemote))
'
' Prepare a byte array to receive on the server socket, and call recvfrom().
ReDim bytData2(0 To 1023)
lngCount = UBound(bytData2) - LBound(bytData2) + 1
Call recvfrom(hServerSocket, bytData2(0), lngCount, 0&, udtBlank, LenB(udtBlank))
Call LogServer("5. Received: " & StrConv(bytData2, vbUnicode))
'
' Clean up the socket handles, and free the Winsock DLL.
Call closesocket(hServerSocket)
Call closesocket(hClientSocket)
'
Call WSACleanup
'
End If
'
End Sub
'
' ---------------------------------------------------------------------------------
' Control events.
' ---------------------------------------------------------------------------------
'
Private Sub cmdClose_Click()
'
Call Unload(Me)
'
End Sub
'
' ---------------------------------------------------------------------------------
' Private helpers.
' ---------------------------------------------------------------------------------
'
Private Sub LogClient(ByVal strText As String)
'
With txtClient
.SelStart = Len(.Text)
.SelText = strText & vbCrLf
.SelStart = Len(.Text)
End With
'
End Sub
'
Private Sub LogServer(ByVal strText As String)
'
With txtServer
.SelStart = Len(.Text)
.SelText = strText & vbCrLf
.SelStart = Len(.Text)
End With
'
End Sub
'
' ---------------------------------------------------------------------------------
' EOF.
' ---------------------------------------------------------------------------------
'
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -