Visual Basic
When you create an application that uses the TCP protocol, the first thing you need to decide is what your application will be a client or server. If you create an application server, then your application will listen to the specified port. When the client application makes a connection request, the server application can accept the request and thus establish a connection. If the connection is established, the client application and the server application can freely exchange data.
//Set the port number to be accessed data exchange by assigning a value to the LocalPort property
//Call the Listen method
Private Sub Form_Load()
tcpServer.LocalPort = 1001
tcpServer.Listen
frmClient.Show
End Sub
//The State property is checked to see if the previous connection was completed. If not completed, before establishing a new connection, the old one is forcibly closed.
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
If tcpServer.State sckClosed Then tcpServer.Close
//Accept the Accept request with the requestID connection setup check parameter.
tcpServer.Accept requestID
End Sub
Private Sub txtSendData_Change()
//Text field DateData contains data to be transmitted. All characters that will be entered in this text field will be sent as a single line to the client application using the SendData method.
tcpServer.SendData txtSendData.Text
End Sub
Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
//A buffer variable is declared for the received data. The GetData method is called and the Text property of the txtOutput text field is assigned the value of a buffer variable.
Dim strData As String
tcpServer.GetData strData
txtOutput.Text = strData
End Sub
It turned out a simple server application on Visual Basic.
Source CODE >>
Visual Basic Source Code. TCP Client-Server
|