1. Service-Side procedures
1UsingSystem;
2UsingSystem.Collections.Generic;
3UsingSystem.Text;
4UsingSystem.Net.Sockets;
5UsingSystem.Net;
6
7NamespaceConsoleApplication1
8{
9ClassProgram
10{
11StaticvoidMain (String[] args)
12{
13//1. Create a nested section word
14Socket Slisten=NewSockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);
15
16//2. Fill the IP
17IPAddress IP=Ipaddress.parse ("127.0.0.1");
18IPEndPoint Ipe=NewIPEndPoint (IP,4321);
19
20 //3. Binding
21stSlisten.bind (IPE);
22
23//4. Monitoring
24Slisten.listen (2);
25
26//5. Loop to accept customer connection requests
27While(True)
28{
29Socket Clientsocket;
30Try
31{
32Clientsocket=Slisten.accept ();
33}
34Catch
35{
36Throw;
37}
38//Sending data to the client
39clientsocket.send (Encoding.Unicode.GetBytes (" Hello!!!! "));
+ }
*
42
+ }
/+ }
$
Well done,, service-side,
2 See the client now
1UsingSystem;
2UsingSystem.Collections.Generic;
3UsingSystem.Text;
4UsingSystem.Net.Sockets;
5UsingSystem.Net;
6
7NamespaceConsoleApplication2
8{
9ClassProgram
10{
11StaticvoidMain (String[] args)
12{
13//1. Create a nested section word
14Socket s=NewSockets (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp);
15
16 //2. Fill in the remote IP
17IPAddress IP= ipaddress.parse ( " 127.0.0.1);
18 ipendpoint ipe = new IPEndPoint (Ip, 4321 19
20 // 3. Connection server 21stConsole.WriteLine("Start connecting to the server ....");
22S.connect (IPE);
23
24//4. Receiving data
25Byte[] Buffer=NewByte[1024];
26S.receive (buffer, buffer. Length, Socketflags.none);
27var msg=Encoding.Unicode.GetString (buffer);
Console.WriteLine (" Receive message: {0}", msg);
29
Console.readkey ();
+ }
+ }
+ }
OK, look at the effect ~ ~
Is it simple ~ ~
C # Socket Primer 1 (RPM)