C # Socket Primer 1 (RPM)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.