At the end of the year, people were too impetuous to study technology. However, yesterday I finally got the ticket to go home.
Back to the original volume. After WCF came out, we may be playing with fewer and fewer original TCP. We know that WCF has made another packaging of TCP. The first response to us may be
Homogeneous systems use TCP and heterogeneous systems use HTTP. The question is, can heterogeneous systems use TCP? At least WCF cannot be used, because other languages do not have "services" for. net.
Reference ", there is no ChannelFactory for you to play, if you must pursue performance, the original TCP will help you.
We know that the most primitive is to play Socket, because the Socket is more complex, but the most flexible, C # provides two simplified packaging classes: TcpListener and TcpClient.
I. TcpListener
This exists as a server program. Let's take a look at how to use this server.
1: Enable the listener address
1 TcpListener listener = new TcpListener(new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 }), 2222);2 3 listener.Start();
2: Okay. It has been enabled. The server program starts to listen to the client requests on this port. How can I get this request? The AcceptTcpClient attribute of listener is simple.
1 var myclient = listener.AcceptTcpClient();
3: We know that TCP transmits byte streams. You can use myclient. GetStream () to obtain a NetworkStream, which can be used to send and receive information.
<1> receiving operation:
1 BinaryReader sr = new BinaryReader (client. GetStream (); 2 3 // client data 4 var data = sr. ReadString ();
<2> sending operation: Return Value
1 // return data to the client after processing. 2 BinaryWriter sw = new BinaryWriter (client. getStream (); 3 4 sw. write (string. format ("Current Time: {0}, the server has completed processing! ", DateTime. Now ));
The note here is that the AcceptTcpClient is blocked by the thread, and a TCP connection is established only when the client request is received. during the processing of the server, subsequent client requests will
It will process and wait until the previous request is processed. If so, we recommend that you open a thread to serve every request, just like this.
1 // accept client connection request 2 var myclient = listener. acceptTcpClient (); 3 4 // use a working thread to execute the user's request 5 tasks. factory. startNew (obj) => 6 {7 var client = obj as TcpClient; 8 9 client. close (); 10 11}, myclient );
Well, this is probably the way on the server side. There are some related attribute configurations, such as the opentime and sendtime of wcf.
Ii. TcpClient
The client is also very simple, as long as we Connect the port and then Send some data through NetworkStream, it will be OK.
1 TcpClient client = new TcpClient (); 2 3 client. connect (new System. net. IPAddress (new byte [] {127, 0, 0, 1}), 2222); 4 5 BinaryWriter bw = new BinaryWriter (client. getStream (); 6 7 bw. write (string. format ("hello, I will request you! {0}, current Thread: {1} ", j, Thread. CurrentThread. ManagedThreadId ));
Iii. Simulation
Finally, we simulate that the client starts 100 threads, each thread requests 100 times, and the server uses a working thread to process each thread. Have you found the netTcpBinding
I feel that the last thing everyone understands is that it is not a good thing to have too many threads.
Server:
View Code
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. net. sockets; 6 using System. IO; 7 using System. threading. tasks; 8 using System. threading; 9 using System. net; 10 11 namespace ConsoleApplication112 {13 class Program14 {15 static void Main (string [] args) 16 {17 TcpListener listener = new TcpListener (new System. net. IPAddress (new byte [] {127, 0, 0, 1}), 2222); 18 19 listener. start (); 20 21 // use a dedicated thread to accept the request 22 Task. factory. startNew () => 23 {24 // continuously accept client request 25 while (true) 26 {27 // accept client connection request 28 var myclient = listener. acceptTcpClient (); 29 30 // use a working thread to execute the user's request 31 tasks. factory. startNew (obj) => 32 {33 var client = obj as TcpClient; 34 35 BinaryReader sr = new BinaryReader (client. getStream (); 36 37 // client data 38 var data = sr. readString (); 39 40 // client ip41 va R ip = (IPEndPoint) client. client. remoteEndPoint; 42 43 Console. writeLine ("Current Time: {0}, received from IP: {1 }:{ 2}, sent data: {3}", DateTime. now, 44 ip. address, ip. port, data); 45 46 Thread. sleep (1000*5); 47 48 // after processing, return data to the client 49 BinaryWriter sw = new BinaryWriter (client. getStream (); 50 51 sw. write (string. format ("Current Time: {0}, the server has completed processing! ", DateTime. now); 52 53 client. close (); 54 55}, myclient); 56} 57}); 58 59 Console. writeLine ("the server has started... "); 60 61 Console. read (); 62} 63} 64}
Client:
View Code
1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. net. sockets; 6 using System. IO; 7 using System. threading; 8 using System. threading. tasks; 9 10 namespace ConsoleApplication211 {12 class Program13 {14 static void Main (string [] args) 15 {16 for (int I = 0; I <100; I ++) 17 {18 Task. factory. startNew () => 19 {20 for (int j = 0; j <100; j ++) 21 {22 TcpClient client = new TcpClient (); 23 24 client. connect (new System. net. IPAddress (new byte [] {127, 0, 0, 1}), 2222); 25 26 BinaryWriter bw = new BinaryWriter (client. getStream (); 27 28 bw. write (string. format ("hello, I will request you! {0}, current Thread: {1} ", j, Thread. currentThread. managedThreadId); 29 30 BinaryReader sr = new BinaryReader (client. getStream (); 31 32 var s = sr. readString (); 33 34 Console. writeLine ("received data: {0}", s); 35} 36}); 37} 38 39 Console. read (); 40} 41} 42}