Winsock control, realize network point-to-point communication

xiaoxiao2021-03-06  63

The ladder of the network: Using the Winsock control, realize the network point-to-point communication Blog appears in 9CBS, and the blog will not blog. Will you ask why? No matter whether there is this question mark in your heart, but in my heart, this is a period! Will you ask why? Ok, no matter what you ask. I decided to answer now. But the cause of the long story, please don't be able to list one by one. I can only explain directly and indirectly. Blog appears in 9cbs, and Blog will not blog. Using VB, written online programs, I haven't tried Winsock this control is really regret (except the API master). I think there is no friend has this regret ... huh! Because, through the Winsock control, you can simplify the network communication and simplify and simplify. What is that extent? Maybe it is within 10 lines of code (Ide generated)! Because that is just network communication, and communication, just send a message, the other party receives it, displayed. Can you calculate it? Take a look: First, the window is loaded, we write: 'Set the first Winsock control to wait WINSOCK1.LOCALPORT = 5052 WINSOCK1.Listen' to connect the second Winsock control to the first Winsock2.connect "127.0 .0.1 ", 5052, then the conspectionRequest event of Winsock1 control is triggered. We write: if winsock1.state <> SCKCLOSED THEN WINSOCK1.CLOSE WINSOCK1.ACCEPT Requestid 'Accepted connection! Simple is very. Again: Winsock1.senddata text1.text 'passes the text in Text1 to the other party, you pass the data to Winsock2, then its DataArrival event also triggered. DIM STRDAT AS STRING WINSOCK2.GETDATA STRDAT 'The data text2.text = strdat' is displayed in Text2, one uses the Winsock control, and the network communication program is completed! Hahaha, is I think it is deceived? This idea, MSDN has been sold early! Gatz can really be covered, I have thought of using MSDN to break up my way! Hey. No wonder him is better than me! I finished the task? No, I am going to "use the Winsock control to realize network point-to-point communication". Communication is implemented, it is also a point to another point. However, in a universal understanding, the point-to-point is not so explained. How to solve? Is two-way communication! Oh, but Gatz is always more than me. This idea has been sold in MSDN! Not impatient? But I want to write a program, I can't be afraid. If you decide to walk this road, then you are more annoying to wait for you. Hello, I am annoying, can you see it? Haha ~! Since the good idea is sold to Gates, what else is it? Just like, Bill sells you an empty data sheet, but does not fill in data.

It is still an empty data table, not a data sheet! But is Bill who can't help you fill in the data suitable for you, so you have to fill the data on your existing data sheet, and delete the source data sheet, then it will become a Unique data sheet! I see you really impatient ... Are you hungry? Steam the mouse and steamed it! Ok, let me talk about it, point to an important program in the point of communication application ... But this time, I will not list the code of a line of code, then tell you to add it, then how .. Now I have to adhere to the policy of "teaching the people to fish", adhere to the principle of "programming"; go to the "understanding of the deep into the deep", from the philosophical perspective of "understanding". Explain how the VB program that transmits files on the network is written through the Winsock control! As with the previous example, communicate with the Winsock control, you can separate the client and server, namely: Client, Server (C / S). But everyone knows that this is only the scene that is required to be staged using the TCP protocol. And in UDP, there is no clear copy of C / S! But such concepts have to be blurred. In the next topic, I will explain again. Today our protagonist is TCP. In the above example, the steps of gradual steps, do you experience it? It doesn't matter, now come and look back.

The server monitors 5052 port. The 5052 port connected to the server is the port of the server program listening! Due to the customer connection, the Winsock control of the server program triggers the ConnectionRequest event (see MSDN). The server accepts the connection, the WINSOCK control of the client programs triggers the Connect event to indicate the connection (extrusion omit). After the connection, the server program sends data to the client program. Data arrives at the guest, the Winsock control triggers the DataArrival event prompt of the guest program, obtains data, display! The whole process is like this, obviously very simple, is wrong? Ha ha! Then we get such a process table. It is necessary to start filling out. Open the file we want to send to the server to send it. After receiving the data, the other party receives it back to the file! The process is like this! Specifically, it is necessary to think about thinking. Ok, now we will think about it from a user's angle! How does two Winsock I will not say, anyway, Gatz is more than me! Then we want to pass the file, first so let the user choose a file. Then I started to go! Good pass, slow! How does the customer know what type of files you want to pass? it is good! With this idea, we get the name of the file you have chosen when you click on the server program to transfer file calls. Want to pass? Still not, if we are chatting pass, it is not very confusing? Well, give this information a name! Just called: "Send" What? Ok, it is a very domineering name ~ Everyone has used the chat software pass files such as QQ know that QQ will first ask if the recipient receives the files of XXXX.xxx! Then let's imitate it, set the transfer command and the file name to: "Send File Name", such a format. Then pass it to the other party with Winsock's Senddata method. Then I went to the client to write code: Ok, when I received this order, we got four words in the school, the other party wants to pass the document! Nothing is written, just play a msgbox, say that the other party will pass XXXX.xxx to you, are you willing to receive? Ok, when the user determines the save path, then in such a path, create a file to be written when receiving data. We now have to return a message to the server, just call: "iliefly"! Represents confirmation reception. It turns back to the server to write code: I received the reception confirmation information, we started reading the file! Open files and declare an array. Read the open file data into this array. Haha ~ Isn't it? That passed the past ... The client starts receiving the data: Hey, bad, how can there be a data type error? It turns out that every time there is data to the client, we have executed the code written in the DataArrival event. I thought it was a command or chat content! No, you have to set a tag to use the data that is sent from the data is the data of the file.

Use a Static statement (see MSDN) to declare a static Boolean variable, when False is our command or chat content, and True is the file data! Change again, the source code written in the client, set this Boolean variable to true after promise to receive! what? The problem came out again! At this time, I think when can I set the Boolean variable back to false? Pour, still have problems! That is concentrated to the server there ... Go back to the server's code edit box: Is there any way to let the client know when it is over? send command? No, it will write the command to the file. Connection is closed? More can't, I have to chat ... Well, if you let the client know the length of the file passing, can you determine when is it finished? Haha! Action, change the command to change the documentation as: "Send File Name File Length". Oh? That's too space, so that the other party explains quite troubles, use INSTR? No, if there is a space in the file name? It's not wrong ... Fortunately, we have split (see MSDN). Ok, then you have to separate the command with a special character! By vbnullchar (see MSDN)! Change the command to: ["Send" & VbnullChar & File Name & VBNullChar & File Length]. Pass it to the other party. (Note: Filelen Function) The client wants to modify the code: declare a dynamic character array, attached to it, haha! The subscript 0 is a command, the subscript 1 is the file name, the subscript 2 is the length of the file! Cool ~ Save the length with the module variable, do you finish it! (Note: VAL function) Continue to write our endless code on the client: Boolean variable is true, with data arrive. Then get its data! It is important to pay attention to the received data type. The opposite is not a string, we want to specify the type "VBARRAY VBBYTE" (see Msnd About GetData), meaning the byte array. Of course, we have to define a byte array to receive DIM BYTEARRAY () as Byte. Ok, then the length that has just been saved is useful, the total length - the number of bytes received at a time = the number of residual bytes. When the number of remaining bytes is 0, isn't it finished? Haha! No urgent, first organize ideas: data arrive, get, save to an array of bytes. Use the Ubound function (see MSDN) to get the maximum subscript of the byte array, but due to the data in the array, the data is saved, but the file is not so calculated, so it is necessary to do 1! Total length = Total length - ubound (byteArray) 1 oh, line! Try it quickly, find more than 50 MB.

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

New Post(0)