"Original" PPC and PC use TCP communication-simple implementation

Source: Internet
Author: User

Before that, I wrote a simple example of communication between PPC and PC through UDP. You can check it here. If there are more, I won't talk about it. Let's go to the topic directly.

This article describes how to use the socket class library in the. net cf class library to complete a simple TCP communication with the desktop program. Before that, you may need to understand the following knowledge:

1. Socket

2. connection-oriented socket

Please search for these knowledge points on your own. I will not repeat them here, but I just want to emphasize that this is very important.

Next, we will write code for the program. First, we define the client as the PPC end and use the Wm5 for ppc simulator. Of course, before we start, please follow this article to configure the simulator for subsequent network work-"original" + "reprint" to configure the simulator network environment (Access LAN) Step by Step! .

After the configuration, we create a new solution, which includes a C # console application (TCP-Server) and a C # PPC Common Program Project (PPC-Client, WM5 ,. netcf 2.0 ).

Let's take a look at the client (PPC-client) Interface Design:

Use a ListBOx to display data exchange information. The two function buttons at the bottom are as simple as that.

Then write the following code for the client:

 

PPC-Client
Using System;

Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using System. Net. Sockets;
Using System. Net;

Namespace PPC_Client
{
Public partial class PPCclient: Form
{
Public PPCclient ()
{
InitializeComponent ();
}
Socket server;
IPAddress adr;
IPEndPoint ep;
Byte [] buffer = new byte [1024];
Int end;
Private void PPCclient_Load (object sender, EventArgs e)
{
Server = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. IP); // set properties for the Socket
Adr = IPAddress. Parse ("192.168.1.29"); // prepare the IP address of the remote server for the socket.
Ep = new IPEndPoint (adr, 3000); // initialize the EndPoint for the socket
}

Private void menuItem1_Click (object sender, EventArgs e)
{
Try
{
Server. Connect (ep); // Connect to the remote endpoint
If (server. Receive (buffer)> 0) // if data exists
{
LbInfo. Items. Clear ();
LbInfo. Items. Add ("connected ");
LbInfo. Items. Add ("getting data from server ");
While (buffer [end]! = 0)
{
LbInfo. Items. Add (buffer [end]. ToString ());
End ++; // increase the offset.
}
LbInfo. Items. Add ("disconnected ");

}
}
Catch (SocketException se)
{
MessageBox. Show (se. Message );
}
Finally
{
Server. Shutdown (SocketShutdown. Both); // close the connection
Server. Close (); // Close the socket
}

}

Private void menuItem2_Click (object sender, EventArgs e)
{
This. Close ();
}

}
}

 

The code is very simple. The main logic is as follows: 1. Set each attribute of the socket during program initialization. 2. Click the connect server button to get data from the server and add it to ListBox. Then, disconnect the server.

After preparing the client, we need to write simple code for the server. The Code is as follows:

 

TCP-Server
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net;
Using System. Net. Sockets;

Namespace TCP_Server
{
Class Program
{

Static void Main (string [] args)
{
Socket client, serverSocket;
IPAddress adr;
IPEndPoint ep;

ServerSocket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. IP );
Adr = IPAddress. Parse ("192.168.1.29 ");
Ep = new IPEndPoint (adr, 3000 );
ServerSocket. Bind (ep );
ServerSocket. Listen (3 );
While (true)
{
// Console. WriteLine ("waiting for join ");
If (client = serverSocket. Accept ())! = Null)
{
Console. WriteLine ("customer connected, sending data ");
Byte [] msg = {10, 20, 30, 40}; // defines the sent data
Console. WriteLine ("Total sent:" + client. Send (msg) + "bytes"); // Send data at the same time
Console. WriteLine ("end ");
Console. ReadLine ();
Client. Close ();
Break;
}
}
}
}
}

 

The code is basically the same as the client, but only one client socket is used to deal with the client, while serverSocket is used to store the server's own information, in addition, the client socket connecting to the server is passed to the client's intermediate station. The general process is the same as that of the client.

Then, start the WM5 simulator, that is, the simulator you configured at the beginning of the article. First, "deploy" the PPC program, and then set the server (console program) in the solution) to start the project, OK, run it.

1. Start the server and wait for connection:

2. Start the PPC client and click "connect to server" to exchange data. The server displays the sending status:

 

OK. Here, a simple PPC-to-PC TCP communication is complete. Of course, it is simple because many things are not taken into account, such as multi-client connections, multi-thread usage, Asynchronous Operation Processing, and other advanced content. I will find a time to discuss related technologies with you. Write it here today.

PS: the temperature has cooled down nationwide today. chuncheng has been beaten back to winter from spring ~~~ Cool ~~~

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.