Visual C # to establish a simple messaging system (1)
Source: Internet
Author: User
Visual Summary: This article discusses the architecture based on socket (socket) and how to build an efficient, easy-to-use messaging (message-passing) system that can be run simultaneously on PCs and Pocket PCs.
Sockets and messages
Currently, most Web services and all remote applications use the remote Procedure call (Remote-procedure-call,rpc) method. What you're doing seems to be calling a function, but there's a lot of action on the table to make sure it happens on the server. At a lower level, the system passes messages between two computers, but this is not considered.
However, when you switch to a socket operation, you are programmed in a purely message-based system. This changes the type of code you write, because the only way to read the returned data is through the message. It is used with no return value or output parameter. NET class is somewhat similar, in which case all the return information needs to be passed through the event.
Because I want the server program to tell the client when it should change the track, it is advantageous to use the message because the information can reach the client from the server without requiring the client to explicitly request it. However, it requires that you use different ways to achieve your goals.
Before I explain all the operations, I'd like to talk a little bit about security. If you open a port on your computer, someone else might use the port to do something bad. They may want to write meaningless signals to determine whether they can control your computer or make it crash. It is necessary to consider this possibility when you write such an application. My example will run on the network behind the firewall, so I feel relatively secure.
Simple sockets
I start with creating a server program that can add 1 to an integer, and here's the server-side code:
public static void Main ()
{
IPAddress localaddr = Ipaddress.parse ("127.0.0.1");
TcpListener listener = new TcpListener (LOCALADDR, 9999);
Console.WriteLine ("Waiting for initial connection");
Listener. Start ();
Socket socket = listener. AcceptSocket ();
Console.WriteLine ("Connected");
NetworkStream stream = new NetworkStream (socket);
BinaryReader reader = new BinaryReader (stream);
BinaryWriter writer = new BinaryWriter (stream);
int i = reader. ReadInt32 ();
i++;
Writer. Write (i);
}
It first establishes a TCP listener on the local port 9999, then starts the listener and waits for the connection. Once the connection is made, it receives an integer, adds 1 to it, and sends it back.
It should be noted that the local address I use here is 127.0.0.1. This can work well when tested, when client and server programs run on the same computer, but when they run on different computers, the program cannot run. In the later section I'll give you more complex code.
Passing messages
It is not fun to pass unprocessed data through sockets, and passing objects through sockets may be more interesting. To achieve this goal, we need a way to get the object and convert it to a byte stream. The most obvious solution is to use the serialization (serialization) support provided by the runtime (runtime). Unfortunately, there are a few problems with this approach.
The first problem is that serialization requires a lot of overhead, which means that it uses more bytes than is necessary to pass data. If you use SOAP format, this problem is even more serious. Whether this becomes a problem depends on the performance requirements of the application. The second problem is that serialization is not available in Compact Framework components. There is no simple way to do this, we need to do this work ourselves. In this process, we are doing less than serialization.
We start by creating an enumeration that defines what kind of messages can be passed:
public enum MessageType
{
Requestemployee = 1,
Employee,
}
For each type of message, we need an object to define the object:
public class Requestemployee:isocketobject
{
int id;
public requestemployee (int id)
{
This.id = ID;
}
}
Public Requestemployee (BinaryReader reader)
{
id = reader. ReadInt32 ();
}
The approach we use is very similar to the ISerializable interface. The Isocketobject interface defines a send () function that serialize data through the channel, followed by a constructor that parallelization the data.
When one of these objects is serialized itself, the first message it sends is the message identifier. It lets the receiver know which type of object will arrive and establish the object. The following is the client's code:
Requestemployee Requestemployee = new Requestemployee (15);
Requestemployee.send (writer);
Requestemployee This code creates an object and sends it to the server program, then it finds out what kind of object is returned and parallelization it.
Although this sample project is marked as client and server, the only real difference between the two is the way the connection is built. When this process is complete, they both send and receive messages using similar code, even if they need to handle their own collection of messages.
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