Take advantage of Winsock control (2)

zhaozj2021-02-17  45

If there is only two computers, it is very easy. It is assumed that the E.m machine is a client, and its IP is 192.192.192.1, and the receiving port is 1200 (any of the ports that are not used) are selected. First, add a WINSOCK control in the OM client program, named SCKCONNECT, and set its properties: removehost = "192.192.192.1", (ie Machine IP address), Remoteport = 1200 (ie, Methical Listening Port) ; In the IT server program, if a WINSOCK control called Sckserver (0), its localport = 1200,

Add for form_load () in the e..

Sckserver (0) .bind Sckserver (0) .localport 'Binds to the local port

Sckserver (0) .listern 'listens

If you want to transfer the data, the two machines must be connected first. The procedure for establishing the connection is as follows:

Method client must first request a connection

Sckconnect.Connect Sckconnect.Remotehost, Sckconnect.remoteport

This sentence is executed, which triggers the CONNECTREQUEST event in the server program. Decide whether to establish a connection during this process, the code is as follows:

Private sub Sckserver_ConnectionRequest (Index As Integer, Byval Requestid As Long)

IF SCKSERVER.COUNT = 1 THEN

Load Sckserver (1)

Sckserver (1) .accept requestid

END IF

End Sub

After the connection is established, the EMR or IT can apply the SendData method to transfer data. For example, if it is a string called String, simply add:

Sckconnect.senddata String

After the data is transmitted, the DataArrival event of the ie is triggered, and the data that can be received in the process of the GetData method in the process:

Private Sub Sckserver_DataArrival (INDEX AS INTEGER, BYVAL BYTESTOL AS Long)

Dim SDATA AS STRING

Sckserver (1) .Getdata SDATA, VBSTRING

End Sub

Finally, don't forget to close the Winsock control before closing the program.

Privat Sub Form_Unload (Cancel AS Integer)

IF SCKCONNECT.STATE <> SCKCLOSED THEN

Sckconnect.Close

END IF

End Sub

This is just the simplest case. If there are multiple computers, it is slightly more complicated, and the client program can do not change, and the server-side program requires a little change:

Private sub Sckserver_ConnectRequest (Index As Integer, ByVal Requestid As Long)

DIM SIP As String

DIM I as integer

SIP = SCKSERVER (0) .Remotehostip 'gets the login's IP address

I = 1

Do While i <= Sckserver.ubound 'Checks if there is a record of this address

If Sckserver (i) .Remotehostip = Sip Then 'If you have, you don't have to load a new control

Sckserver (i) .accept requestidexit Sub

END IF

I = i 1

Loop

Load Sckserver (i) 'Otherwise, loading new controls

Scksrver (i) .accept requestid

End Sub

Notice that the above information conversations actually happen between the client and the server. If you want to make a chat room, everyone can be "listened" by others, then in the DataArrival event of the server, put The data received by the received client is forwarded to all clients.

The code for its loop forwarding information is as follows:

For i = 1 to Sckserver.count

IF SCKSERVER (i) .State <> SCKCLOSED THEN

Sckserver (i) .senddata SDATA

END IF

Next i

How, let's do your own communication software!

However, I don't know if you don't have, the above programs need to have a computer as a server, but if there is no computer in our local area network, it can be normally opened, that is, if the server-side program is not running, other clients There is no way to communicate in programs. And this situation may be frequent! At least, the local area network I use is like this. Isn't it possible to enjoy the LAN communication?

Don't worry, remember, our Winsock control has another protagonist: UDP protocol.

The UDP protocol is also known as the user data packet protocol, is a connectionless protocol. What is no connection agreement? That is to say, when using this protocol, it is not necessary to be like a TCP protocol: need server-side listening, client-side requests to connect, and both parties will communicate after connecting. In addition, the UDP application can be a client, or a server program, without having to establish a client program and a server program respectively as a TCP application.

Here, a process of communicating the UDP protocol communication: UDP protocol, in order to transfer data in two machines, you must set up the LocalPort property of the two machines, and then set the REMOTEHOST property of the macar to the IP address of the IP address. The REMOTEPORT property is set to the localport attribute value of the E.. At this time, the EMRC can transfer the data, and the equipment is also used using the GetData method in the DataArrival event to get information sent to the itself. If you want to transfer data to the Machine machine, you can simulate the above process settings.

With the UDP protocol to transmit information than the TCP protocol, it does not need to listen, there is no need to connect (connect), just like we usually send a letter, as long as the address and accept the name of the name and send out I.e. We can use this information transfer program in a local area, let's briefly describe the features and basic ideas you want to implement in the following procedures:

First, we must want the program's icon to display in System TRAY and not displayed in the taskbar! This is not too easy to program the implementation, but fortunately there is an off-the-shelf program in the VB CD in Common / Tools / VB / Unsupport / SystemTray. We only need to compile it into a SYSTRAY.OCX control, then add your own program. This control can be. Its use is very simple. It has defined the mouse click, double-click, etc. You only need to write the corresponding mouse event, and you will not say more here.

The key to the program is that the UDP protocol should know the other's IP and Port when communicating. How do you implement it? The easiest way is to create a profile, which places the name, IP and port of each computer on the LAN, IP and PORT, read all the information when the program is initialized, just know who is in the program, read its corresponding IP and Port can. We know that each computer IP and port, but how can we know if other computers are online, otherwise, someone else can't receive it? We can put this program in the startup menu, let it boot automatically, and minimize, put in the System TRAY in the lower right corner of the window. When the program just starts running, it will automatically send a message to all IPs you know from the configuration file: "I am coming!", If there is a computer online, it will automatically return a message: "Welcome!", Then both the two machines have successfully, they will join the other party's name to their own communication people; if there is a computer shutdown, the program will automatically say goodbye to everyone before exiting: "Goodbye!", Receive this information The computer will automatically remove the name of the computer that transmits information from its own communicative person list. This way, if someone is not online, you will not be able to send information to it; if you don't boot except for you, you will have no one in your manifest. And other people will automatically go to your "registration", others will automatically go to your "farewell" as long as they are offline, you can know if others are using computer, you can even use this procedure to count Every day, every day, good!

Ok, the basic model of the program of a local area network is already, it is not complicated! Let's take your hand, come and enjoy the fun of chatting with your own procedure!

However, if you want to write an excellent program, it is not only good ideas and features, more importantly, the compatibility and fault tolerance of the program. In this example, there is no detailed explanation for the error handling. About this, you can find a satisfactory answer in the Error event of the Winsock control and its help.

In addition, it is also possible to consider the expansion of the function from the following aspects: such as multimedia information such as transmitting graphics, sound, etc., local area network mutual transmission file (active transmission), history call record, system log, personal board time statistics, etc., and all All this depends only on your imagination and your intelligence! Below, the author uses a transfer file as an example to talk about its functionality.

You can open the local file (graphics, sound, etc.) to open it in a binary file, read it, and read all of its contents into an array of Byte types, the local machine code is as follows:

DIM myfile () as byte

DIM POSITION As Long

Open "filename" for binary as # 1

Position = 0

Do While Not Eof (1)

Position = POSITON 1

Redim preserve myfile (1 to position)

Get # 1, Myfile (position)

loop

Close # 1

Transfer this byte array to the remote machine

Sckserver.senddata myfile

After the remote machine receives this array, open a new target file in the binary file, write the array content to this newly opened file, if it is a BMP picture, put it in the Picture image box, if it is a WAV file, Play it. In this way, two people in the local area can communicate through language, pictures, and text. The remote machine code is as follows:

Private sub sckconnect_dataarrival (byval Bytestotal As Long)

Dim ReceiveFile (1 to bytestotal) as Byte

Sckconnect.getdata ReceiveFile, VBarray VBBYTE

'Tell the Winsock control to receive data of byte array types

Open "C: / Temp / File Name" for binary as # 1

For i = 1 to bytestotal

Put # 1, RemoteArray (i)

Next i

Clost # 1

End Sub

转载请注明原文地址:https://www.9cbs.com/read-29508.html

New Post(0)