in fact, as long as the use of socket connection, basically the use of thread, is cross-use.
The socket usage in C # encapsulation is basically not very complex, but it is not known whether the socket after hosting has any other performance or security problems.
In C # can be found in the bottom of the operation is the socket, the concept does not explain.
The program model is as follows:
WinForm Program: Start the port to listen, monitor the socket connection situation, and periodically close inactive joins;
Listener: Handles the socket's accept function, listens for new links, and establishes a new thread to handle these joins (Connection).
Connection: Handles each session of a specific join.
1:winform How to start a new thread to start listener:
[Code=csharp]//start the server
private void Btn_startserver_click (object sender, EventArgs e)
{
this.btn_startServer.Enabled = false;
Thread _createserver = new Thread (new ThreadStart (Waitforconnect));
_createserver.start ();
}
Wait All connections
private void Waitforconnect ()
{
SocketListener listener = new SocketListener (Convert.ToInt32 (this.txt_port. Text));
Listener. Startlistening ();
}[/code]Because the listen join is a loop-waiting function, it is not possible to execute directly inside the WinForm thread, or WinForm will not be able to continue any operations, so a new thread is specified to execute the function and start the listening loop.
This new thread is relatively simple and basically does not have a startup parameter, directly specifying the handler function is possible.
2:listener How to start a loop listener and start a new thread with parameters to handle the socket join session.
First look at how to set up listening: (startlistening function)
[Code=csharp] IPEndPoint localendpoint = new IPEndPoint (_ipaddress, _port);
Create a TCP/IP socket.
Socket listener = new socket (addressfamily.internetwork, SocketType.Stream, protocoltype.tcp);
Bind the socket to the local endpoint and listen for incoming connections.
Try
{
Listener. Bind (Localendpoint);
Listener. Listen (//20 trucks)
Start listening for connections.
while (true)
{
Here is being suspended while waiting for a new connection.
Socket connection = listener. Accept ();
Logger.Log ("Connect", connection. Remoteendpoint.tostring ());//log it, new connection
}
}[/code]
The basic steps are relatively simple:
The IPEndPoint object of this machine is set up, which indicates that the server is listening on the specified port;
Then bind to a listening socket;
Enter the while loop, waiting for a new join;
If there is a new join, then a new socket is created to correspond to the session of the join.
It is worth noting that this sentence joins the code: listener. Accept (). When the sentence is executed, the procedure waits in this place until the next sentence is executed by the procedure when there is a new request for a joint inspection. This is synchronous execution, and of course it can be executed asynchronously.
The new join socket is established (after accept), what should I do with these new sockets? They are still a cyclic wait, so there is still a need to create a new thread to the socket to process the session (Receive/Send the message), and this thread will receive the parameters.
Thread itself is not able to receive parameters, in order to allow it to receive parameters, you can define a new class, add parameters as a property of the method to resolve.
Because each socket is a Connection cycle, I define such a class of public classes Connection. This class has at least one such constructor public Connection (socket socket); This is done in order to pass the socket parameter to the connection object, and then let listener start the thread, thread can know which socket he is working on.
Specific processing methods: (startlistening function in listener, ocket connection = listener. Accept (); after)
[Code=csharp] Connection GPSCN = new Connection (Connection);
Each socket would be is wait for data. Keep the connection.
Thread thread = new Thread (new ThreadStart (Gpscn.waitforsenddata));
Thread. Name = connection. Remoteendpoint.tostring ();
Thread. Start (); [/code]As a result , the new socket is running in the new thread after the accept.
Session processing for 3:connection
Set up a new connection (that is, socket), remote can and this socket session, nothing more than send and receive.
Now let's look at how to write this thread to run the connection. Waitforsenddata function
[Code=csharp]while (True)
{
bytes = new byte[1024];
String data = "";
Systm'll be waiting the msg of receive Envet. Like Accept ();
Here'll be suspended and waiting for socket income MSG.
int bytesrec = this._connection. Receive (bytes);
_lastconnecttime = DateTime.Now;
if (Bytesrec = = 0)//close envent
{
Logger.Log ("Close Connection", _connection. Remoteendpoint.tostring ());
Break
}
Data + = Encoding.ASCII.GetString (bytes, 0, Bytesrec);
..... handle your data.
}[/code]You can see the following basic steps for this process:
Execute the receive function to receive the information sent by the remote socket;
Converts the information from byte to string;
Process the information, then go to the next loop and continue waiting for the socket to send new information.
There are several notable points to note:
The 1:receive function. This function is similar to the listener accept function. In this place wait for execution, if there is no new message, the function will not execute the next sentence, waiting.
2: A byte stream is received and needs to be converted into a string
3: Determine how to close a connection remotely
4: If the other party's message is very large, you have to loop to receive this data.
4: How to manage these joins (thread)
With the above program, you can basically establish a listener and process the join session. But how do you manage these thread? Otherwise a large number of thread generation is a disaster.
The management method is relatively simple, in listener I defined a static hash table (static public Hashtable connections=new Hashtable ();), Stores the connection instance and its corresponding thread instance. The connection also adds a definition of the last join time (private DateTime _lastconnecttime;). When the new link is established (after listener's accept (), the connection instance and thread instance are stored in the hash table, and the last join time is modified at connection receive. This allows us to know where the connection is and whether the session is active.
You can then manage these sessions in the WinForm program, setting the settings timeout.
C # Socket Program (TCP)