Visual Basic achieve point-to-point communication

Author:Anonymous    Updated:2008-10-28 12:08:22
The use of TCP protocol, if a need to create custom applications, it is necessary to identify the server name or IP address. Application of communications port at any time will be carefully monitoring the news issued by each other, this is the system to ensure reliable connections. Once the connection took place, neither side can SendData to send and receive data, and GetData with their own separate data. Transmission of data, the need to set the client's property LocalPort, servers only need to RemoteHost attribute set to the client's Ethernet address, and set the client with the same attributes LocalPort port address, with SendData method to start sending News. GetData client in the case through the DataArrival isolated incident sent. Winsock control of a local computer can connect to a remote computer, at the same time the use of TCP or UDP protocol, the two agreements can create client and server applications.

The use of Winsock control, communications, the two sides need to select the same agreement. TCP protocol apply to the transmission of high-capacity, the need to ensure the security of data files, and the UDP protocol with the need to apply under a lot of communication, or to establish a connection with too many changes, especially in a very small amount of data when . When set to use Winsock1.Protocol = sckTCPProtocol method, you must first find the name of the computer, and it's Winsock add LocalHost properties.

The creation of an application, you must first determine the establishment of a client side application or server, the server application is only the beginning of the establishment and monitoring into the state, the client application began to establish a connection into a normal state of communications. I set up an application, and its function when the client side of the mouse, the server application to real-time display on the mouse.

The following is the establishment of server applications:

1. To create a new standard EXE file;

2. Winsock to add a control;

3. Accession to the code is as follows:

Private Sub Form Load ()

TcpServer.LocalPort = 1001

TcpServer.Localhost = \ "servser \"

TcpServer.remotePort = 1002

TcpServer.Localhost = \ "klint \"

TcpServer.Listen

End Sub

'Connections check

Private Sub tcpServer ConnectionRequest (ByVal requestID As Long)

If tcpServer.State <> sckClosed Then

TcpServer.Close

TcpServer.Accept requestID

End Sub

'To send data

Private Sub frmserver monsemove (x, y)

TcpServer.SendData \ "x \" & str (x)

TcpServer.SendData \ "y \" & str (y)

End Sub

Customers to build applications for the method:

1. To create a new standard EXE file;

2. Winsock to add a control;

3. Text Entry box - txtx and txty;

4. Accession to the code is as follows:

Private Sub Form Load ()
TcpServer.LocalPort = 1002
TcpServer.Localhost = \ "klint \"
TcpServer.remotePort = 1001
TcpServer.Localhost = \ "servser \"
TcpServer.Listen
End Sub

\ 'Connection check

Private Sub tcpklint ConnectionRequest
(ByVal requestID As Long)
If tcpklint.State <> sckClosed Then
  Tcpklint.Close
  Tcpklint.Accept requestID
End Sub

Receive data

Private Sub tcpClient DataArrival
(ByVal bytesTotal As Long)
Dim strData As String
Tcpklint.GetData strData
If left (strData, 1) = \ "X \" then
Txt x.Text = strData
Else
Txt y.Text = strData
Endif
End Sub

Routine to realize the above is a very simple point-to-point communications, on this basis, slightly modified, can form a functional complex real-time computer network AA interactive communication system for the control, graphics simulation, and so on. Winsock in VB has provided us with a simple method of data transmission so that we can easily point-to-point communications network.
Previous:Visual Basic preparation of the virus
Next:VB with the realization of multi-media API
User Reviews
Site Search
Recommended article
AD