Using the Winsock Control
A WinSock control allows you to connect to a remote machine and exchange data using either the User Datagram Protocol (UDP) or the Transmission Control Protocol (TCP). Both protocols can be used to create client and server applications. Like the Timer control, the WinSock control doesn't have a visible interface at run time. www.tartoos.com Possible Uses
Selecting a ProtocolWhen using the WinSock control, the first consideration is whether to use the TCP or the UDP protocol. The major difference between the two lies in their connection state:
The nature of the application you are creating will generally determine which protocol you select. Here are a few questions that may help you select the appropriate protocol:
Setting the ProtocolTo set the protocol that your application will use: at design-time, on the Properties window, click Protocol and select either sckTCPProtocol, or sckUDPProtocol. You can also set the Protocol property in code, as shown below:
Determining the Name of Your ComputerTo connect to a remote computer, you must know either its IP address or its "friendly name." The IP address is a series of three digit numbers separated by periods (xxx.xxx.xxx.xxx). In general, it's much easier to remember the friendly name of a computer. To find your computer's name
Once you have found your computer's name, it can be used as a value for the RemoteHost property. TCP Connection BasicsWhen creating an application that uses the TCP protocol, you must first decide if your application will be a server or a client. Creating a server means that your application will "listen," on a designated port. When the client makes a connection request, the server can then accept the request and thereby complete the connection. Once the connection is complete, the client and server can freely communicate with each other. www.tartoos.com The following steps create a rudimentary server: To create a TCP server
The procedures above create a simple server application. However, to complete the scenario, you must also create a client application. To create a TCP client
Important Be sure to change the value of the RemoteHost property to the friendly name of your computer.
The code above creates a simple client-server application. To try the two together, run the project, and click Connect. Then type text into the txtSendData TextBox on either form, and the same text will appear in the txtOutput TextBox on the other form. Accepting More than One Connection RequestThe basic server outlined above accepts only one connection request. However, it is possible to accept several connection requests using the same control by creating a control array. In that case, you do not need to close the connection, but simply create a new instance of the control (by setting its Index property), and invoking the Accept method on the new instance. www.tartoos.com The code below assumes there is a Winsock control on a form named sckServer, and that its Index property has been set to 0; thus the control is part of a control array. In the Declarations section, a module-level variable intMax is declared. In the form's Load event, intMax is set to 0, and the LocalPort property for the first control in the array is set to 1001. Then the Listen method is invoked on the control, making it the "listening control. As each connection request arrives, the code tests to see if the Index is 0 (the value of the "listening" control). If so, the listening control increments intMax, and uses that number to create a new control instance. The new control instance is then used to accept the connection request.
UDP BasicsCreating a UDP application is even simpler than creating a TCP application because the UDP protocol doesn't require an explicit connection. In the TCP application above, one Winsock control must explicitly be set to "listen," while the other must initiate a connection with the Connect method. In contrast, the UDP protocol doesn't require an explicit connection. To send data between two controls, three steps must be completed (on both sides of the connection):
Because both computers can be considered "equal" in the relationship, it could be called a peer-to-peer application. To demonstrate this, the code below creates a "chat" application that allows two people to "talk" in real time to each other: To create a UDP Peer www.tartoos.com
To create a second UDP Peer
To try the example, press F5 to run the project, and type into the txtSend TextBox on either form. The text you type will appear in the txtOutput TextBox on the other form. About the Bind MethodAs shown in the code above, you must invoke the Bind method when creating a UDP application. The Bind method "reserves" a local port for use by the control. For example, when you bind the control to port number 1001, no other application can use that port to "listen" on. This may come in useful if you wish to prevent another application from using that port. www.tartoos.com The Bind method also features an optional second argument. If there is more than one network adapter present on the machine, the LocalIP argument allows you to specify which adapter to use. If you omit the argument, the control uses the first network adapter listed in the Network control panel dialog box of the computer's Control Panel Settings. www.tartoos.com When using the UDP protocol, you can freely switch the RemoteHost and RemotePort properties while remaining bound to the same LocalPort. However, with the TCP protocol, you must close the connection before changing the RemoteHost and RemotePort properties. www.tartoos.com |
| PC school | مشاكل انترنت | الصفحة الثالثة | الصفحة الثانية | الصفحة الأولى |
|
| Home | Syria | سورية | أرواد | Amrit | عمريت | المجلة الطبية | مدرسة الكمبيوتر | دليل المواقع العربية |
| للسيدات فقط | المعلومات | معجم الكمبيوتر | المجلة الطبية | لمحة عن طرطوس | صور من طرطوس | صور من سورية | دليل المواقع الأجنبية | |
|
© 2007 LBCI Corporation. All rights reserved Eng.Hanna Ata Lahoud |
||||||||