บทความนี้แสดงตัวอย่างที่สองของวิธีการใช้ตัวควบคุม Winsock ActiveX ที่ติดตั้งพร้อมกับเครื่อง Microsoft Office 2000 นักพัฒนา Edition มือ
ตัวควบคุม Winsock ช่วยให้คุณสามารถเชื่อมต่อไปยังรีโมทคอมพิวเตอร์และอัตราแลกเปลี่ยนข้อมูลระหว่างคอมพิวเตอร์ไคลเอนต์และเซิร์ฟเวอร์ ตัวควบคุม Winsock สนับสนุนโพรโทคอลสอง: ส่งควบคุมโพรโทคอล (TCP) และโพรโทคอลเดตาแกรมผู้ใช้ (UDP)
tcp เป็นโพรโทคอลแบบเชื่อมต่อ analogy ทั่วไปที่ใช้เพื่ออธิบาย TCP เป็นของโทรศัพท์ ใน analogy นี้ ผู้เรียกต้องสร้างการเชื่อมต่อบนทั้งสิ้นสุดของสายโทรศัพท์ก่อนที่จะสามารถแลกเปลี่ยนข้อมูล คอมพิวเตอร์โดยใช้ TCP ต้องรับการยืนยันจากคอมพิวเตอร์เครื่องรับที่ได้รับการเชื่อมต่อที่สร้างไว้ก่อนที่คอมพิวเตอร์สองเครื่องสามารถถ่ายโอนข้อมูล
udp เป็นโพรโทคอ connectionless analogy ทั่วไปที่ใช้เพื่ออธิบาย UDP เป็นของวิทยุ ใน analogy นี้ สถานีวิทยุกระจายสัญญาณนั้นโดยไม่ทราบอย่างแน่ใจว่าว่าใครก็ตามการฟัง คอมพิวเตอร์ที่ใช้ UDP ส่งข้อมูล และไม่จำเป็นต้องเชื่อมต่อกับคอมพิวเตอร์ในการรับสินค้าจุดสิ้นสุดของการส่งผ่าน
ตัวอย่างที่ 1 - การใช้ตัวควบคุม activex Winsock กับ TCP
ตัวอย่างนี้ใช้คอมพิวเตอร์เครื่องเดียวเพื่อส่ง และรับข้อมูล คุณสามารถสร้างฟอร์ม ด้วยตัวควบคุม Winsock ที่สาม ตัวควบคุมอย่างใดอย่างหนึ่ง emulates ระบบคอมพิวเตอร์ไคลเอนต์ส่งการร้องขอการเชื่อมต่อกับเซิร์ฟเวอร์ ตัวสองควบคุมอื่น ๆ ของระบบบนคอมพิวเตอร์เซิร์ฟเวอร์ที่เลียนแบบ: ตัวควบคุมหนึ่งรอการร้องขอการเชื่อมต่อ และอื่น ๆ ยอมรับการร้องขอเมื่อมันมา
- สร้างฐานข้อมูลเปล่าใหม่ที่ชื่อว่า WinsockDemo.mdb
- สร้างฟอร์มใหม่ต่อไปนี้ (ไม่ตามตารางหรือแบบสอบถามใด ๆ) ในมุมมองออกแบบ:
Form: TCPForm
---------------------------------
Caption: TCP Form
Command button:
Name: cmdListen
Caption: Listen
Command button:
Name: cmdConnect
Caption: Establish Connection
Command button:
Name: cmdSend
Caption: Send Data
Command button:
Name: cmdRespond
Caption: Respond
Command button:
Name: cmdClose
Caption: Close Connection
Text box:
Name: Text1
Label Caption: Data Received:
Winsock control:
Name: axWinsockListen
Winsock control:
Name: axWinsockClient
Winsock control:
Name: axWinsockServer
- ในการมุมมองเมนู คลิกรหัส.
- พิมพ์บรรทัดต่อไปนี้ในส่วนการประกาศของโมดูคลาของฟอร์ม:
Dim wsListen, wsClient, wsServer As Winsock
- พิมพ์ขั้นตอนต่อไปนี้
เมื่อต้องการแสดงให้เห็น optimally ถึงวิธีการควบคุม Winsock ที่ทำงานระหว่างไคลเอนต์และเซิร์ฟเวอร์ กระบวนการจะแสดงรายการในใบสั่งที่จะเกิด
Private Sub Form_Load()
' Set one server Winsock control and the client Winsock control
' when the form loads.
Set wsListen = Me!axWinsockListen.Object
Set wsClient = Me!axWinsockClient.Object
' Set the protocol for each control.
wsListen.Protocol = sckTCPProtocol
wsClient.Protocol = sckTCPProtocol
' Set the remote host on the client Winsock control. Because
' client and server are the same computer in this example, set
' RemoteHost equal to LocalIP.
wsClient.RemoteHost = wsListen.LocalIP
' Set a local and a remote port for the client.
wsClient.RemotePort = 100
wsClient.LocalPort = 99
' Set a local and a remote port for the server. Note that the
' server RemotePort is the client LocalPort and vice versa.
wsListen.LocalPort = 100
wsListen.RemotePort = 99
End Sub
Private Sub cmdListen_Click()
' Start the server listening for a connection request.
wsListen.Listen
Msgbox "Server is waiting for a connection request."
End Sub
Private Sub cmdConnect_Click()
' The client requests a connection with the server.
Msgbox "Client requested connection with server."
wsClient.Connect
End Sub
Private Sub axWinsockListen_ConnectionRequest(ByVal requestID As _
Long)
' When the server receives a connection request, set the second
' Winsock on the server to accept the request.
Set wsServer = Me!axWinsockServer.Object
wsServer.Protocol = sckTCPProtocol
' Accept the connection request.
wsServer.Accept requestID
Msgbox "Server accepted client connection request."
End Sub
Private Sub axWinsockClient_Connect()
' When the server accepts the connection request, the Connect
' event fires on the client. Display a message indicating success.
MsgBox "Connection Successful!"
End Sub
Private Sub cmdSend_Click()
' After a connection is established, use a command button to send
' data from client to server.
wsClient.SendData "Hello"
End Sub
Private Sub axWinsockServer_DataArrival(ByVal bytesTotal As Long)
Dim strClientMsg As String
' The DataArrival event fires on the server when the client sends
' information. Get the data and display it in a text box.
wsServer.GetData strClientMsg, vbString
Me!Text1.Value = strClientMsg
End Sub
Private Sub cmdRespond_Click()
' Send a message from the server to the client.
wsServer.SendData "Thanks for the message!"
End Sub
Private Sub axWinsockClient_DataArrival(ByVal bytesTotal As Long)
Dim strServerMsg As String
' The DataArrival event fires on the client when the server sends
' information. Get the data and display it in a text box.
wsClient.GetData strServerMsg
Me!Text1.Value = strServerMsg
End Sub
Private Sub cmdClose_Click()
' Close the server connections
wsServer.Close
wsListen.Close
Msgbox "Server connections closed."
End Sub
Private Sub axWinsockClient_Close()
' Close event on client fires after server closes connection.
' Close the client connection and display a message box.
wsClient.Close
MsgBox "Client connections closed. Good-Bye!"
End Sub
- บันทึก และปิด TCPForm
- เปิด TCPForm ในมุมมองฟอร์ม และทำงานต่อไปนี้:
- คลิกการฟังปุ่มเริ่มการทำงานของเซิร์ฟเวอร์ในการรอรับการติดต่อสำหรับการร้องขอการเชื่อมต่อ
หมายเหตุกล่องข้อความที่ปรากฏเพื่อบ่งชี้ว่า เซิร์ฟเวอร์กำลังรอการเชื่อมต่อ - คลิกการสร้างการเชื่อมต่อปุ่ม
หมายเหตุกล่องข้อความจากไคลเอ็นต์การตรวจสอบว่า การเชื่อมต่อเสร็จสมบูรณ์ และกล่องข้อความจากเซิร์ฟเวอร์การตรวจสอบว่า มีการยอมรับการร้องขอ - คลิกการส่งข้อมูลปุ่ม และหมายเหตุว่าข้อความ "Hello" ไคลเอนต์ปรากฏในกล่องข้อความบนแบบฟอร์ม
- คลิกการตอบสนองปุ่ม และโปรดทราบว่า ข้อความเซิร์ฟเวอร์ "ขอบคุณสำหรับข้อความถูกแสดงในกล่องข้อความ
- คลิกการปิดการเชื่อมต่อปุ่ม และบันทึกย่อของข้อความกล่องจากไคลเอนต์และเซิร์ฟเวอร์ที่ระบุว่า การเชื่อมต่อได้ปิด
ตัวอย่างที่ 2 - การใช้ตัวควบคุม activex Winsock ด้วย UDP
ตัวอย่างนี้ใช้คอมพิวเตอร์เครื่องเดียวเพื่อส่ง และรับข้อมูล คุณสร้างฟอร์มที่ มีการควบคุม Winsock ที่สอง: หนึ่งของตัวควบคุม emulates คอมพิวเตอร์ไคลเอนต์ และตัวควบคุมอื่น ๆ emulates เซิร์ฟเวอร์
- สร้างฐานข้อมูลเปล่าใหม่ที่ชื่อว่า WinsockDemo.mdb หรือใช้ฐานข้อมูลที่คุณสร้างขึ้นในตัวอย่างก่อนหน้านี้
- สร้างฟอร์มใหม่ต่อไปนี้ (ไม่ตามตารางหรือแบบสอบถามใด ๆ) ในมุมมองออกแบบ:
Form: UDPForm
--------------------------------
Caption: UDP Form
Command button:
Name: cmdSend
Caption: Send Data
Text box:
Name: Text1
Label Caption: Data Received:
Winsock control:
Name: axWinsockClient
Winsock control:
Name: axWinsockServer
- ในการมุมมองเมนู คลิกรหัส.
- พิมพ์บรรทัดต่อไปนี้ในส่วนการประกาศของโมดูคลาของฟอร์ม:
Dim wsClient, wsServer As Winsock
- พิมพ์ขั้นตอนต่อไปนี้
เมื่อต้องการแสดงให้เห็น optimally ถึงวิธีการควบคุม Winsock ที่ทำงานระหว่างไคลเอนต์และเซิร์ฟเวอร์ กระบวนการจะแสดงรายการในใบสั่งที่จะเกิด
Private Sub Form_Load()
' Set the control objects when the form loads.
Set wsClient = Me!axWinsockClient.Object
Set wsServer = Me!axWinsockServer.Object
' Set the protocol for client and server.
wsClient.Protocol = sckUDPProtocol
wsServer.Protocol = sckUDPProtocol
' Set the host and ports for client and server. Because client
' and server are the same computer in this example, set RemoteHost
' equal to LocalIP.
wsServer.RemoteHost = wsClient.LocalIP
wsServer.RemotePort = 1007
wsClient.Bind 1007
End Sub
Private Sub CmdSend_Click()
' Send a broadcast message from the server.
wsServer.SendData "Hello"
End Sub
Private Sub axWinsockClient_DataArrival(ByVal bytesTotal As Long)
Dim strServerMsg As String
' When a message arrives from the server, display it in a text
' box.
wsClient.GetData strServerMsg, vbString
Me!Text1.Value = strServerMsg
End Sub
- บันทึก และปิดแบบฟอร์ม UDPForm
- เปิด UDPForm ในมุมมองฟอร์ม และคลิกการส่งข้อมูลปุ่ม
โปรดสังเกตว่า กล่องข้อความแสดง "Hello" เนื่องจากเป็นส่งข้อมูล connectionless คุณไม่มีการสร้างการเชื่อมต่อไคลเอ็นต์เซิร์ฟเวอร์
หมายเลขบทความ (Article ID): 209905 - รีวิวครั้งสุดท้าย: 8 มกราคม 2554 - Revision: 2.0
ใช้กับ
- Microsoft Access 2000 Standard Edition
| kbhowto kbinfo kbusage kbmt KB209905 KbMtth |
แปลโดยคอมพิวเตอร์ข้อมูลสำคัญ: บทความนี้แปลโดยซอฟต์แวร์การแปลด้วยคอมพิวเตอร์ของ Microsoft แทนที่จะเป็นนักแปลที่เป็นบุคคล Microsoft มีบทความที่แปลโดยนักแปลและบทความที่แปลด้วยคอมพิวเตอร์ เพื่อให้คุณสามารถเข้าถึงบทความทั้งหมดในฐานความรู้ของเรา ในภาษาของคุณเอง อย่างไรก็ตาม บทความที่แปลด้วยคอมพิวเตอร์นั้นอาจมีข้อบกพร่อง โดยอาจมีข้อผิดพลาดในคำศัพท์ รูปแบบการใช้ภาษาและไวยากรณ์ เช่นเดียวกับกรณีที่ชาวต่างชาติพูดผิดเมื่อพูดภาษาของคุณ Microsoft ไม่มีส่วนรับผิดชอบต่อความคลาดเคลื่อน ความผิดพลาดหรือความเสียหายที่เกิดจากการแปลเนื้อหาผิดพลาด หรือการใช้บทแปลของลูกค้า และ Microsoft มีการปรับปรุงซอฟต์แวร์การแปลด้วยคอมพิวเตอร์อยู่เป็นประจำ
ต่อไปนี้เป็นฉบับภาษาอังกฤษของบทความนี้:
209905
(http://support.microsoft.com/kb/209905/en-us/
)