Visual Basic Winsock Control details

Source: Internet
Author: User
Visual Basic Winsock Control details

The Winsock control can connect to a remote machine through UDP (User Datagram Protocol) or TCP (Data Transmission Protocol) and exchange data. Both protocols can be used to create client and server applications. Like the timer control, the Winsock control does not have a visual interface when running.

Possible Use

· Create a client application that collects user information before reaching the central server.

· Create a server application, which can serve as a centralized processing point for data from multiple users.

· Create a chat program.

Protocol selection

When we use the Winsock control, we must first determine whether the TCP or UDP protocol is used. The main difference between them is the connection status:

The TCP Control is a connection-based protocol. Like a telephone, a user must establish a connection before a call;

UDP is a connectionless protocol. The transaction processing between two computers is like a piece of paper: one computer sends a message to another computer, but there is no clear connection path between them. In addition, the size of a single message sent depends on the network.

Generally, the type of the application you want to create determines the protocol you want to select. Below are a few questions that can help you select the appropriate protocol:

When sending or receiving data, does the application need to obtain authentication from the server or client? If necessary, TCP needs to establish a clear connection before sending or receiving data.

Is there a large amount of data to be sent? Once a connection is established, the TCP protocol maintains the connection and ensures data integrity. However, such connections consume more processor resources and increase the cost.

Is the data transmitted one after another, or is it all transferred at a time? For example, if the application you want to create will inform the specific computer when some tasks are completed, it is more appropriate to select the UDP protocol. UDP is also suitable for sending small amounts of data.

Protocol Configuration

Configure the protocol used by your application: in the design phase, click the protocol in the tool window and select

Scktcpprotocol or sckudpprotocol. You can also configure the protocol in the Code as follows:

Winsock1.protocol = scktcpprotocol

Confirm your computer name

To connect to a remote computer, you must know its IP address or Alias. An IP address is a string of three numbers separated by periods. Generally, computer aliases are easier to remember.
Follow these steps to find your computer name:

· Click Start in the "Taskbar"

· Click "Control Panel" in the "Settings" option ";

· Double-click the "network" icon;

· Click "network ID"

Your computer name is displayed in "computer name.

Once you find your computing name, it can be used as a property of the remote host.

TCP connection entry

When using the TCP Control to create an application, you must first determine whether your program is a server or a client. Creating a server program means that your program can "listen" on the specified port, while the client can make a request, and the server can accept the request and establish a connection. Once the connection is established, the client and the server can communicate freely.
  
Create a server program

To create a simple server program, follow these steps:

· Create a standard EXE project;

· Change the name of the default form to frmserver;

· Change form title to TCP server;

· Pull the Winsock control to the form and name it tcpserver;

Add two text boxes in the form, named txtsenddata and txtoutput respectively.

'Add the following code to the form;
Private sub form_load ()
'Set the localport property to an integer.
'Then invoke the listen method.
Tcpserver. localport = 1001
Tcpserver. Listen
Frmclient. Show 'Show the client form.
End sub
Private sub tcpserver_connectionrequest _
(Byval requestid as long)
'Check if the control's state is closed. If not,
'Close the connection before accepting the new
'Connection.
If tcpserver. State <> sckclosed then _ [Page]
Tcpserver. Close
'Accept the request with the requestid
'Parameter.
Tcpserver. Accept requestid
End sub

Private sub txtsenddata_change ()
'The Textbox Control named txtsenddata
'Ins ins the data to be sent. Whenever the user
'Types into the textbox, the string is sent
'Using the senddata method.
Tcpserver. senddata txtsenddata. Text
End sub

Private sub tcpserver_dataarrival _
(Byval bytestotal as long)
'Descare a variable for the incoming data.
'Invoke the getdata method and set the text
'Property of A textbox named txtoutput
'The data.
Dim strdata as string
Tcpserver. getdata strdata
Txtoutput. Text = strdata
End sub

The above is the process of creating a simple server application. However, to complete the entire process, you have to create a client program.

Create a tcp client program

· Add a new form in the project and name it frmclient;

· Change the form title (Caption) to tcp client;

· Add a windsock control to the form and name it tcpcllient;

· Add two text box controls to the frmclient form and name them txtsend and txtoutput respectively;
· Add a commandbutton to the form and name it cmdi I;

· Change the title (Caption) of the button control to connect;

Add the following code to the form:

Note: Make sure that the remote host property is changed to your computer alias.

Private sub form_load ()
'The name of the Winsock Control is tcpclient.
'Note: to specify a remote host, you can use
'Either the IP address (ex:/"121.111.1.1/") or
The computer's/"friendly/" name, as shown here.
Tcpclient. remotehost =/"remotecomputername /"
Tcpclient. remoteport = 1001
End sub

Private sub branch connect_click ()
'Invoke the connect method to initiate
'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 above Code creates a simple client/server application. To connect the two, you can run the project and click Connect. Input text in any txtsenddata text box. The same text information appears in the txtoutput text box of another form.

Accept multiple connection requests

The server program described above intelligently accepts a connection request. However, it is also possible to create a group of controls and use the same controls to accept multiple connection requests. In this case, you do not need to close the connection, as long as you create a new control instance (by configuring its index properties), call the Acceptance Method in the new instance. [Page]

The following code assumes that there is a Winsock Control in a form called sckserver, and its index attribute is set to 0. This control is part of the control array. In the declaration section, a module-level variable intmax is generated. In the form loading event, intmax is set to 0, and the local port attribute of the first control in the array is set to 1001.

The monitoring method is called in the control and used as the "monitoring control ". When each connection request arrives, the code will test whether its index is 0 (the value of the monitoring control). If it is 0, the intmax value in the monitoring control is increased by 1, use this value to create a new control instance. The new control instance is used to accept connection requests.

Private intmax as long

Private sub form_load ()
Intmax = 0
Sckserver (0). localport = 1001
Sckserver (0). Listen
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 connection entry

Creating a UDP application is easier than creating a TCP program, because UDP does not require a definite connection. In the preceding TCP application, one of the Winsock Controls must be explicitly set to "listener", and the other must use the connection method to initiate a connection.

On the contrary, UDP does not require explicit connections. To transmit data between two controls (connected parties), you must complete three steps:

· Determine that the remote host attribute is the computer name of the other party;

· Determine that the remote host property is the local port property of the second control;

Call the agreed method to specify the local port to be used. (This method will be discussed in detail below)

· Create a UDP connection

· Create a standard EXE project;

· Name the default form frmpeera;

· Add a Winsock Control in the form and name it udppeera;

· On the properties page, click protocol to change to udpprotocol;

· Add two text box control forms named txtsend and txtoutput respectively;

Add the following code to the form:

Private sub form_load ()
The control's name is udppeera
With udppeera
'Important: Be sure to change the remotehost
'Value to the name of your computer.
. Remotehost =/"peerb /"
. Remoteport = 1001 'port to connect.
. Bind 1002 'Bind to the local port.
End
Frmpeerb. Show 'Show the second form.
End sub

Private sub txtsend_change ()
'Send text as soon as it's 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

Create the second UDP connection

· Add a standard form to the project;

· Change the form name to frmpeerb;

· Change the form title to peer B;

· Add a windsock control in the form and name it udppeerb;

· Click protocol on the properties page and change it to udpprotocol;

· Add two text boxes to the form, named txtsend and txtoutput respectively;

Add the following code to the form:

Private sub form_load ()
The control's name is udppeerb. [Page]
With udppeerb
'Important: Be sure to change the remotehost
'Value to the name of your computer.
. Remotehost =/"peera /"
. Remoteport = 1002 'port to connect.
. Bind 1001 'Bind to the local port.
End
End sub

Private sub txtsend_change ()
'Send text as soon as it's typed.
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.