We first encapsulate the previous code into a complete class, because the network-related method is not necessarily to establish a socket server and client, so still should be two separate, such as the acquisition of native IP, modify the native ip,ping remote host these things should be placed in a separate class, And the inside method should be static (an application does not need multiple instances to do the above), so I personally suggest a separate class functionnetwork, which will make the main program very concise
?
Note the methods that may involve automatic acquisition of IP addresses and manual modification require additional DLL references (System.Management), which are temporarily unavailable
?
Encapsulating the client's socket class method is relatively simple, mostly just copy the previous code. But when we receive the data, we use the delegate method to communicate the data externally. General events and delegate-to-use is more convenient (do not care why so write, follow the copy on the line), delegate section declares the name of the delegate and the type of data to be passed, and the event section declares the names of the events. This socket client event is also three (connection success, receive data, disconnect, of course, you can also define additional events, such as the sending of the success of the event, send failed also counted events)
?
When the socket client has onrecievemessagefromserversuccess (Received message), then the problem comes, who will process the data? Our current program is required to process the main program, so the main program at the time of initialization, as long as the declaration when the theclient occurred xxx event, the variables he will pass through the function of + + (note the tab auto-completion,) Then you can take the data that the class is passing on to the control. It can be imagined, if not Form1 to use, but other classes or forms to use, as long as the initialization of the time to add a delegate event binding function can be, it is very convenient (otherwise how do you put a method of a class of the data generated by the outgoing?) Delegates and events are meant to solve this problem.)
?
Not yet, the data for the direct update interface we described earlier is implemented by turning off cross-thread access control detection, which is actually not standardized, and in fact, when the main program receives a message to update the control, it first determines whether the xxx control is invokerequired, Then if you do not want to execute the Invoke method, it is more ugly in the writing, and do not need to understand, as long as you can follow the gourd painting scoop on the line
?
Corresponding to this, the other two delegate methods are also done (even if there are multiple controls to update, you need only consider a control. invokerequired)
?
In order to see more clearly from the server side of the data sent, you can use a string traversal (such as the server sent a character a, in addition to this character also includes the carriage return line \ r \ n), we can see that the byte array we define only the first three bits is meaningful (65/13/10), Check the ASCII code table can be found to be a + Enter + newline (so if you want to solve the problem of Chinese garbled, also need to respond to write to byte and extract from Byte, maybe a byte is not enough to represent a Chinese, and most of the software is only thought to pass a byte is a character, So the hard to put two or more byte together to make sense of the Chinese split the resulting garbled)
?
When the server disconnects, it will actually send an empty message (of course, you write the server or the client when you can also send a custom message), or the previous method can be tested, regardless of the server endpoint stoplistening directly stop the server, Or disconnect just kicked off the client, the client can finally receive a byte array, and this array is nothing, this message can be used to determine whether the server is actively disconnecting the connection
?
So we can make the software more perfect, if the server click on the disconnection, the prompt server to disconnect, if the client clicked on the port, the client is prompted to disconnect
?
and whether it is active or passive disconnect, the interface buttons are reset to the connection is available, the disconnection is unavailable, the Send is unavailable (if the connection is successful), so this is also a delegate and event to encapsulate the entire class of possible behavior and other methods of triggering the advantage, if not events and delegates, these things can be done, But the cost of the code and maintenance to be spent is much greater.
?
?
For more instructional videos and downloads, please pay attention to the following information:
My Youku Space:
Http://i.youku.com/acetaohai123
?
My online forum:
http://csrobot.gz01.bdysite.com/
?
Problem Exchange:
qq:910358960
Email:[email protected]
?
?
?
C # App Video tutorial 1.3 socket communication Client Perfect