See this topic, is it very familiar? In the blog Park Search, to ensure that the article about this dongdong is too many ~ ~ ~ really is not written necessary, and I am a bit lazy to ponder words. (see this, must have a turning point, or you will not see the following, no) but, for their next article to write for reference, it is necessary to first add the socket basic knowledge.
Note: If you have contacted the socket, then there is no need to delay the time to watch. Also, if any of these errors are found, please indicate them directly.
1. By convention first to introduce the next socket
Many things in Windows are borrowed from the Unix realm, as are sockets. In Unix, the socket represents a file descriptor (in Unix everything is in a file), and this descriptor is used to describe network access. What does that mean? Is that programmers can send and receive data on the network via a socket. You can also understand it as an API. With it, you do not have to directly operate the network card, but through this interface, which saves a lot of complex operations.
In C #, MS provides us with the System.Net.Sockets namespace, which contains the socket class.
2. With a socket, you can use it to access the network
But you should not be happy too early, to access the network, there must be some basic conditions (and programming-related I do not mention): A. To determine the IP and port of this computer, the socket can only be powerful if it is bound to an IP and port. B. There must be an agreement (or who will recognize you?) what is sent to the network? To be complicated, we can make our own agreement. But this is not mentioned in this article, I introduce two kinds of people most familiar with the protocol: TCP & UDP. (Don't say you don't know, otherwise ...) Or... I won't tell you.)
If you have the basic conditions, you can start using them to access the network. Let's take a look at the steps:
A. Creating a socket
B. Binding the IP and port of the machine
C. If it is TCP, because it is connection-oriented, use the Listeno () method to monitor whether someone on the network is sending something to themselves;
D. In TCP, if you hear a connection, you can use accept to receive the connection, and then you can use send/receive to perform the operation. UDP, however, does not require accept, and uses sendto/receivefrom directly to perform the operation. (See Well, there is a difference with TCP's execution method, because UDP does not need to establish a connection, so do not know each other's IP and port before sending, so you need to specify a sent node for normal send and receive)
E. If you do not want to continue to send and receive, do not waste resources. Close you can close.
If you look at the above text, you are not clear, then come to see the picture is good:
Connection-oriented socket system call timing