C # Socket send & amp; receive & amp; return simple application,

Source: Internet
Author: User

C # simple application for sending, receiving, and returning data through Socket,

I haven't written a blog for a long time. Recently, due to project requirements, I need to use Socket for communication. I wrote a few simple examples to record it. The code is very simple, and it is nothing more than receiving and sending, and return a custom message after receiving the data, which can also be defined as sending.

The acceptor needs to listen to a port. It first checks whether the port to be used is occupied and defines a processing method. The following is the processing code:

1 public static bool PortIsUse (int port) 2 {3 bool isUse = false; 5 IPGlobalProperties ipProperties = IPGlobalProperties. getIPGlobalProperties (); 6 IPEndPoint [] ipEndPoints = ipProperties. getActiveTcpListeners (); // find the used port 8 foreach (IPEndPoint endPoint in ipEndPoints) 9 {10 if (endPoint. port = port) // determines whether 11 {12 isUse = true; 13 break; 14} 15} 16 return isUse; 17} exists}

Define the acceptor:

 

1 TcpListener tcpl = new TcpListener (new IPAddress (new byte [] {127, 0, 0, 1}), 1111 ); // define a TcpListener object to listen to local port 1111 2 tcpl. start (); // listener Start 3 while (true) 4 {6 Socket s = tcpl. acceptSocket (); // suspends a Socket object 7 string remote = s. remoteEndPoint. toString (); // get the sender's IP address and port to String standby 8 Byte [] stream = new Byte [1024]; 9 s. receive (stream); // receives the data sent from the sender and writes it to the byte array 10 // BGW_Handle.ReportProgress (1, "received from [" + remote + "); 11 string _ data = Encoding. UTF8.GetString (stream); // convert the byte data array to String12 s. send (stream); // return the received content to the receiver for 13 s. shutdown (SocketShutdown. both); 14} 15 tcpl. stop (); // Stop listening


Define the sender code:

 

IPAddress ip = IPAddress. parse ("127.0.0.1"); // IP address of the receiver 3 IPEndPoint ipEnd = new IPEndPoint (ip, 1111); // interface 4 Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // initialize a Socket object 5 try 6 {7 socket. connect (ipEnd); // Connect to the specified IP & Port 8} 9 catch (SocketException e) 10 {11 Console. writeLine ("connection failed"); 12 Console. writeLine (e. toString ());
14 return; 15} 16 socket. send (Encoding. UTF8.GetBytes ("1234567890"); // send data 17 while (true) // define a loop to receive returned data 18 {19 byte [] data = new byte [1024]; 20 socket. receive (data); // receives the returned data 21 string stringData = Encoding. UTF8.GetString (data); 22 if (! String. isNullOrWhiteSpace (stringData) 23 {24 Console. write (stringData); 25 break; 26} 27} 29 socket. shutdown (SocketShutdown. both); 30 socket. close (); // Close the Socket

From the code above, it is still very simple, thanks to the work done by Microsoft. If there is any error in the above code, you can make it in the comments.

Related Article

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.