For those who do not understand the event and the delegation mechanism, you can refer to the delegate-related example program provided in this section, this is the program of the console, relatively concise (the bookstore publishes a book of events registered to a reader, the principle is consistent with the previous section, but not through the delegation to send data, So the practicality is not very wide, and there are two examples to further deepen the events and delegates.
?
In fact, I have a single control double-click event in the same way, such as the entire program loading and closing events, we just in the form of the event to choose a few mouse double-click into, seemingly direct to the method (private void Sockettest_load, and directly for us to write the parameters to come over, in fact, just find the Designer.cs file or can find the delegate and event code (this represents this form, can be simply understood as the form of the load/Shutdown event occurs when the execution of sockettest_load/ Sockettest_formclosing method)
?
Look at us again. Custom encapsulated classes use events and delegates, and also become very concise, read-write strong (if the class is static, the delegate is also made static)
?
After understanding the event and the delegation mechanism, and implementing the client code, we also implement the server-side code, the server is a little more complex than the client, the start of the first time he will block on the Accept method (until there is a client connection to proceed down, So the Beginlisten method is also put into the thread execution, if there is a client connection, then a new thread will continue to get read the data sent by the client)
?
In addition to the client, the server needs to define an additional socket object (Client_socket, if the previous accept method is executed successfully, then Client_socket will be the client's IP address and other information, The back to the client's transceiver is also through this accept after the instantiation of the client_socket in progress, imagine, if it is to do 1 servers to multiple clients, each client connection on the need to allocate a different Client_socket object in the past, Otherwise a client_socket will only save one client's message, this way, the server sends the message will only run to the latest connection on the client there)
?
When the server active click Disconnect, still need to send an empty message (in the Java version of Sockettest), the empty message is not an empty string, you send an empty string may still make sense, carriage return and so on, you can directly send an empty byte array past, So the client we wrote earlier received the first byte when it was 0 and knew that the server was disconnected.
?
Finally, the interface of the entire re-layout, the front we use is a text box, preferably with a ListBox, each time you receive data after automatically refresh display the last one (also modeled after his practice, each time he sent the text box emptied, which I think is not necessary)
?
Server side than the client a little more function, refresh the native IP, set the native IP should be the server side of the function (and ping a network address should not belong to the client and not the server, so put on the top more appropriate)
?
Finally add a logo, you can like the Java version of the Sockettest released (add a custom ico, can be run in the program when the upper left corner and the icon is displayed)
?
Summary: In addition to the synchronous way to achieve socket communication, there are asynchronous ways, but also in addition to TCP mode UDP (broadcast way, such as a server corresponding to multiple clients, the server sends a message does not need to be assigned to which customer, is all customers receive), In addition, based on the socket communication also produced a lot of industrial communication protocols such as MODBUSTCP,ETHERNETIP (socekt TCP/IP communication is simply a byte array of sending and receiving bytes, and then on this basis there are a lot of provisions on the word stream, such as the beginning, the end, Data segment definition, error check and so on, thus forming a more concise, more error-prone stream of words, compared to the specification called data frame, here are not to do detailed analysis, interested readers can find their own way to achieve, the simplest synchronization socekt has enough.
1 Implement the complete socket communication process, understand the client, the server communication process
2 understand the method of defining the server and client of the socket, do not make the static benefit (the reader can test how to implement the open multiple ports and connect multiple clients themselves, if the static can not be achieved)
3 understand the advantages of delegates and events in the delivery of messages, in the future code in addition to the various methods encapsulated into classes (especially the basic tutorial on a lot of string methods, mathematical operations), but also to standardize the class of data processing, the best way is through the delegation
?
?
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 # Application video Tutorial 1.4 Implementing full Ethernet communication