[. Net MF network development board research-04] Socket programming Server

Source: Internet
Author: User

 

 

The previous articles introduced Http-related applications. In fact, from a technical perspective, we should first introduce Socket programming and then Http. After all, Http is implemented through Socket-related function programming. . NET Micro Framework Socket functions and desktop edition. the functions in the. NET Framework are fully compatible.. NET Micro Framework. NET Framework. But the opposite is not necessarily true, because the functions provided by. NET Micro Framework are the most basic, removing some overloaded functions that implement the same function. The official example contains SocketServer. The example in the article "IP Address Setting and simple web demonstration" is actually improved on the basis of this official example, it uses Socket to implement the simplest Web application. The following Web page appears when you access the IE browser.

However, this example is more complex than it (also based on the official example for function expansion), and uses the Socket Client program compiled on the PC end to connect to it, and perform direct data interaction, which makes it easier to understand the Socket communication process.

To display information on the LCD screen, we need to reference the System provided by me. tinyGUI Library (of course, you can also use the WPF technology to display information on the LCD screen, but if you really want to do so, you will find it troublesome), about System. tinyGUI library, we have a detailed introduction in "build lightweight TinyGUI for Cortex-M3", "TinyGUI drawing example", "TinyGUI bitmap display" and "let TinyGUI library support Simulator", etc, this download package contains sample code and instructions for TinyGUI, so we will not talk about it here.

 

Use Socket to create a Socket-based TCP server. The Code is as follows (from the official sample code ):

Socket server = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp );

IPEndPoint localEndPoint = new IPEndPoint (IPAddress. Any, c_port );

Server. Bind (localEndPoint );

Server. Listen (Int32.MaxValue );

While (true)

{

// Wait for a client to connect.

Socket clientSocket = server. Accept ();

// Process the client request. true means asynchronous.

New ProcessClientRequest (clientSocket, true );

}

We put the following code in the official example:

// Return a static HTML document to the client.

String s = "HTTP/1.1 200 OK \ r \ nContent-Type: text/html; charset = UTF-8 \ r \ n

M_clientSocket.Send (Encoding. UTF8.GetBytes (s ));

To:

Byte [] bytData = new byte [bytesRead];

Array. Copy (buffer, bytData, bytData. Length );

String s = new string (System. Text. UTF8Encoding. UTF8.GetChars (bytData ));

System. TinyGUI. Graphics. Print (s); // displays the received data.

M_clientSocket.Send (bytData); // directly return the received data

In addition, we need to add a While loop to this code. Otherwise, it will only receive data once and return it.

Now, compile the code, deploy it to the Development Board, and run it.

Here, we will not write the TCP client code, just use the tool we have prepared to test it, as follows: http://www.sky-walker.com.cn/mfrelease/tools/yftcpclient.rar.

After running, enter the address and port number of the Development Board, and test the communication with the Development Board. The test is as follows:

 

We can see what we send, then what is returned by the Development Board as is.

The development board displays the received data on the LCD screen, for example:

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.