Current location - Loan Platform Complete Network - Big data management - vb gurus, can you tell me the exact usage of winsock?
vb gurus, can you tell me the exact usage of winsock?
From MSDN:

Using the Winsock Control

Using the WinSock control, you can establish a connection to a remote computer and exchange data over the User Datagram Protocol (UDP) or Transmission Control Protocol (TCP). Both protocols can be used to create client-server applications. Similar to the Timer control, the WinSock control is not visible at runtime.

Possible uses

Creating a client application that collects information about a user and sends the collected information to a central server.

Creating a server application that acts as a sink for data from multiple users.

Create a "chat" application.

Selecting a Communication Protocol

When using the WinSock control, you first need to consider what protocol to use. The protocols that can be used are TCP and UDP. The important difference between the two protocols is their connection state:

The TCP protocol control is a connection-based protocol, which can be compared to the telephone system. The user must establish a connection before data transfer can begin.

The UDP protocol is a connectionless protocol, and transfers between two computers are similar to sending mail: messages are sent from one computer to another, but there is no explicit connection between the two. Also, the maximum amount of data that can be transferred in a single transmission depends on the specific network.

The exact choice of protocol is usually determined by the application that needs to be created. The following questions will help in choosing the appropriate protocol:

Does the application need to get an acknowledgement from the client or server when sending or receiving data? If so, use the TCP protocol to establish a clear connection before sending or receiving data.

Is the amount of data particularly large (e.g., image and sound files)? After the connection is established, the TCP protocol maintains the connection and ensures the integrity of the data. However, such connections require more computing resources and are therefore more "expensive".

Is data sent intermittently, or within a session? For example, if an application needs to notify a computer when a task is completed, the UDP protocol is more appropriate; UDP is suitable for sending small amounts of data.

Protocol settings

At design time, you can set up the protocols used by your application as follows: Click Protocol in the Properties window, and then select either sckTCPProtocol or sckUDPProtocol. You can also use program code to set the Protocol property as follows:

Winsock1.Protocol = sckTCPProtocol

Determining the name of the computer

When connecting to a remote computer, you need to know its IP address or the name of its "nice" computer. An IP address is a string of numbers in groups of three, separated by dots (e.g., xxx.xxx.xxx.xxx). Often, the easiest thing to remember is the computer's "catchy name".

To determine the name of your computer, follow these steps:

On your computer's Taskbar, click Startup.

In Settings, click Control Panel.

Double-click the Network icon.

Click the Logos tab.

The name of the computer can be found in the Computer Name box.

The computer name found above can be used as the value for the RemoteHost property.

TCP Connection Preliminary

If the application is going to use the TCP protocol, then you must first decide whether the application is a server or a client. To create a server side, the application needs to "listen" on the specified port. When a client requests a connection, the server side is able to accept the request and establish the connection. After the connection is established, the client and server can freely communicate with each other.

The following steps create a very simple server:

To create a TCP server, follow these steps:

Create a new Standard EXE project.

Change the name of the default form to frmServer.

Change the title of the form to "TCP Server".

Place a Winsock control in the form and change its name to tcpServer.

Add two TextBox controls to the form. Name the first one txtSendData and the second one txtOutput.

Add the following code to the form.

Private Sub Form_Load()

'Set the LocalPort property to an integer.

'Then call the Listen method.

tcpServer.LocalPort = 1001

tcpServer.Listen

frmClient.Show 'Display the client form.

End Sub

Private Sub tcpServer_ConnectionRequest _

(ByVal requestID As Long)

'Checks that the control's State property is off.

'If it is not,

'Close this connection before accepting a new one.'

If tcpServer.State <> sckClosed Then _

tcpServer.Close

'Accept the

'connection with the requestID parameter.

tcpServer.Accept requestID

End Sub

Private Sub txtSendData_Change()

'The TextBox control named txtSendData

'contains the data to send. data.

tcpServer.SendData txtSendData.Text

End Sub

Private Sub tcpServer_DataArrival _

(ByVal bytesTotal As Long)

'Declare a variable for the incoming data.

'Call the GetData method and give the data to the Text property of the TextBox named txtOutput

'.

Dim strData As String

tcpServer.GetData strData

txtOutput.Text = strData

End Sub

The above steps create a simple server application. In order for it to work, a client application must also be created for it.

To create a TCP client, follow these steps:

Add a new form to the project and name it frmClient.

Change the title of the form to "TCP Client".

Add a Winsock control to the form and name it tcpClient.

Add two TextBox controls to frmClient. Name the first one txtSend and the second one txtOutput.

Put a CommandButton control on the form and name it cmdConnect.

Change the title of the CommandButton control to Connect.

Add the following code to the form .

Highlights The RemoteHost property value must be changed to the name of your computer.

Private Sub Form_Load()

'The name of the Winsock control is tcpClient.

'Note: To specify the remote host, you can either use

' the IP address (e.g., "121.111.1.1"), or you can use

' the computer's "nice name" as shown below.

tcpClient.RemoteHost = "RemoteComputerName"

tcpClient.RemotePort = 1001

End Sub

Private Sub cmdConnect_Click()

'Call the Connect method to initialize the connection.

tcpClient.Connect

End Sub

Private Sub txtSendData_Change()

tcpClient.SendData txtSend.Text

End Sub

Private Sub tcpClient_DataArrival _

(ByVal bytesTotal As Long)

Dim strData As String

tcpClient.GetData strData

txtOutput.Text = strData

End Sub

The code above creates a simple client/server model application. We can run both: run the project and click Connect. Type text in the txtSendData text box of one of the two forms, and you can see that the same text will appear in the txtOutput text box of the other form.

Accepting Multiple Connection Requests

The basic server designed above can only accept one connection request. It is also possible to accept multiple connection requests at the same time using a single control by creating an array of controls. With this approach, there is no need to close the connection, but simply create a new instance of the control (by setting its index property) and then call the Accept method on the new instance.

The following code assumes that the form named sckServer has a Winsock control on it, with its Index property set to 0; the control is therefore part of an array of controls. In the declaration section, a module-level variable, intMax, is declared. intMax is set to 0 in the form's Load event, and the LocalPort property of the first control in the array is set to 1001. The Listen method of the control is then called, making it a "listening" control. When a connection request arrives, the code checks to see if Index is 0 (the value of the "listen" control). If it is 0, the listening control increments the value of intMax and uses that number to create a new instance of the control. The new control instance is then used to accept connection requests.

Private intMax As Long

Private Sub Form_Load()

intMax = 0

sckServer(0).LocalPort = 1001

sckServer(0).Listen< /p>

End Sub

Private Sub sckServer_ConnectionRequest _

(Index As Integer, ByVal requestID As Long)

If Index = 0 Then

intMax = intMax + 1

Load sckServer(intMax)

sckServer(intMax).LocalPort = 0

sckServer(intMax).Accept requestID

Load txtData(intMax)

End If

End Sub

UDP Preliminaries

Creating a UDP application is even easier than creating a TCP application because the UDP protocol does not require an explicit connection. In the TCP application above, one Winsock control must explicitly "listen" and the other must initialize the connection using the Connect method.

The UDP protocol does not require an explicit connection. To send data between two controls, you need to complete the following three steps (on both sides of the connection):

Set the RemoteHost property to the name of the other computer.

Set the RemotePort property to the LocalPort property of the second control.

Call the Bind method to specify the LocalPort to use. (This method is discussed in more detail below.)

Because the two computers can be viewed as "equal," this application is also known as point-to-point. To illustrate this, here's how to create a "chat" application that allows two people to talk to each other in real time.

To create a UDP partner, follow these steps:

Create a new Standard EXE project.

Change the name of the default form to frmPeerA.

Change the title of the form to "Peer A".

Place a Winsock control in the form and name it udpPeerA.

On the Properties page, click Protocol and change the protocol to UDPProtocol.

On the Properties page, click Protocol and change the protocol to UDPProtocol. p>

Add two TextBox controls to the form. Name the first one txtSend and the second one txtOutput.

Add the following code to the form.

Private Sub Form_Load()

'The name of the control is udpPeerA

With udpPeerA

'Emphasis: You must change the value of RemoteHost

'to the name of the computer.'

.RemoteHost= "PeerB"

.RemotePort = 1001 'The port number to connect to.

.Bind 1002 'The port to bind to locally.

End With

frmPeerB.Show 'Show the second form.

End Sub

Private Sub txtSend_Change()

'Sends the text as soon as it is typed.

udpPeerA.SendData txtSend.Text

End Sub

Private Sub udpPeerA_DataArrival _

(ByVal bytesTotal As Long)

Dim strData As String

udpPeerA.GetData strData

txtOutput.Text = strData

End Sub

To create a second UDP partner, follow these steps:

Add the following to the project a standard form to the project.

Change the name of the form to frmPeerB.

Change the title of the form to "Peer B".

Place a Winsock control in the form and name it udpPeerB.

On the Properties page, click Protocol and change the protocol to " UDPProtocol".

Add two TextBox controls to the form. Name the first one txtSend and the second one txtOutput.

Add the following code to the form.

Private Sub Form_Load()

'The name of the control is udpPeerB.

With udpPeerB

'Emphasis: You must change the value of RemoteHost to the name of the

'computer.'

.RemoteHost= "PeerA"

.RemotePort = 1002 'The port to connect to.

.Bind 1001 'Bind to the local port.

End With

End Sub

Private Sub txtSend_Change()

'Sends text immediately after typing.

udpPeerB.SendData txtSend.Text

End Sub

Private Sub udpPeerB_DataArrival _

(ByVal bytesTotal As Long)

Dim strData As String

udpPeerB.GetData strData

txtOutput.Text = strData

End Sub

To try out the above example, run the project by pressing F5 and then type some text into each of the two forms' txtSend TextBoxes of both forms and type some text into each. The typed text will appear in the txtOutput TextBox of the other form.

About the Bind Method

In the code above, the Bind method is called when the UDP application is created, which is required.The purpose of the Bind method is to "reserve" a local port for the control. For example, if you bind the control to port 1001, no other application will be able to "listen" on that port. This method prevents other applications from using the same port.

The second parameter of the Bind method is optional. If there is more than one network adapter on the computer, you can use the LocalIP parameter to specify which adapter to use. If this parameter is ignored, the control uses the first adapter listed in the Network control panel dialog box in the Control Panel settings on your computer.

When using the UDP protocol, you can change the RemoteHost and RemotePort properties as often as you like, while always remaining bound to the same LocalPort, unlike the TCP protocol, which requires you to close the connection before changing the RemoteHost and RemotePort properties.