When using the TCP protocol, if you need to create a client application, you must identify the name or IP address of the server. The communication port of the application will carefully monitor the messages sent by the other party at any time, which is a guarantee for reliable connection of the system. Once the connection occurs, either party can send and receive data through SendData and separate its own data through GetData. When transmitting data, you need to set the client's LocalPort attribute first. The server only needs to set the RemoteHost attribute to the client's Ethernet address, set the same port address as the client's LocalPort attribute, and start sending messages with the SendData method. . The client separates the sent information through the DataArrival event in the GetData event. A Winsock control allows a local computer to connect to a remote computer using UDP or TCP protocols. Both protocols can create client and server applications.
When using the Winsock control, both parties to the communication need to select the same protocol. The TCP protocol is suitable for transmitting large-capacity data files that require security guarantees, while the UDP protocol is suitable for situations where you need to communicate with many subordinates separately, or where there are many and time-varying connections established, especially when the amount of data is small. . You can use the Winsock1.PRotocol=sckTCPProtocol method when setting. First, find the name of your computer and add it to the LocalHost property of Winsock.
When creating an application, you must first determine whether you are building a client application or a server service. Only when the established server application starts to work and enters the listening state, the client application begins to establish a connection and enter a normal communication state. The author created an application whose function is that when the client's mouse moves, the server application can display the position of the mouse in real time.
Here's how to build a server application:
1. Create a new standard EXE file;
2. Add a Winsock control;
3. Add the following code:
PrivateSubFormLoad()
tcpServer.LocalPort=1001
tcpServer.Localhost=servser
tcpServer.remotePort=1002
tcpServer.Localhost=klint
tcpServer.Listen
EndSub
'Connection check
PrivateSubtcpServerConnectionRequest(ByValrequestIDAsLong)
IftcpServer.State<>sckClosedThen
tcpServer.Close
tcpServer.AcceptrequestID
EndSub
'Send data
PrivateSubfrmservermonsemove(x,y)
tcpServer.SendDatax&str(x)
tcpServer.SendDatay&str(y)
EndSub
How to build a customer application is:
1. Create a new standard EXE file;
2. Add a Winsock control;
3. Add two Text boxes - txtx and txty;
4. Add the following code:
PrivateSubFormLoad()
tcpServer.LocalPort=1002
tcpServer.Localhost=klint
tcpServer.remotePort=1001
tcpServer.Localhost=servser
tcpServer.Listen
EndSub
'Connection check
PrivateSubtcpklintConnectionRequest
(ByValrequestIDAsLong)
Iftcpklint.State<>sckClosedThen
tcpklint.Close
tcpklint.AcceptrequestID
EndSub
receive data
PrivateSubtcpClientDataArrival
(ByValbytesTotalAsLong)
DimstrDataAsString
tcpklint.GetDatastrData
ifleft(strData,1)=Xthen
txtx.Text=strData
else
txty.Text=strData
endif
EndSub
The above routine implements a very simple point-to-point communication. With slight modifications on this basis, a functionally complex real-time computer network AA interactive communication system can be formed for control, graphics simulation, etc. Winsock in VB provides us with a simple data transmission method, allowing us to easily achieve point-to-point network communication.
->