Now it seems increasingly common to break down large software projects into interacting small programs, communication between parts of a program can use some type of communication protocol, which may run on different machines, in different operating systems, in different languages, but possibly on the same machine, actually, These programs can be viewed as different threads in the same program. This article mainly discusses the communication between C++/CLI programs, of course, this is to discuss interprocess communication, not network communication.
Brief introduction
Imagine an application that contains a database query function, usually having a program called a server, waiting for another to be called a client program to send a request, and when the request is received, the server performs the function and returns the result (or error message) to the client. In many cases, with multiple clients, all requests are sent to the same server at the same time, which requires a more advanced and sophisticated service-side program.
In some environments for this task, server-side programs may be just one program in many programs, others may be server-side or client programs, and in fact, if our database server needs access to files that do not exist on this computer, it could become a client of one of the other file servers. There may be a service thread and one or more client threads in a program, so we need to be careful with the terms client and server, although they express an approximate abstract meaning, but they differ greatly in their implementation. From a general standpoint, the client is the "consumer" of the service provided by the server, and the server can become the client of some other service.
Service-side sockets
Let's start with a specific, Representative server-side program (see Example 1), which waits for the client to send a pair of integers and adds them together to return the results to the client.
Example 1:
using namespace System;
using namespace System::IO;
using namespace system::net;
using namespace System::net::sockets;
int main (array<string^>^ argv)
{
if (argv->length!= 1)
{
Console::WriteLine ("Usage:server port");
Environment::exit (1);
}
int port = 0;
Try
{
Port = Int32::P arse (argv[0]);
}
catch (formatexception^ e)
{
Console::WriteLine ("Port number {0} is ill-formed", argv[0]);
Environment::exit (2);
}
/*1*/if (Port < Ipendpoint::minport | | port > Ipendpoint::maxport)
{
Console::WriteLine ("Port number must is in the range {0}-{1}",
Ipendpoint::minport, Ipendpoint::maxport);
Environment::exit (3);
}
/*2*/ipaddress^ ipaddress =
Dns::gethostentry (Dns::gethostname ())->addresslist[0];
/*3*/ipendpoint^ IPEndPoint = gcnew IPEndPoint (ipaddress, Port);
/*4*/socket^ listenersocket = gcnew Socket (addressfamily::internetwork,
Sockettype::stream, PROTOCOLTYPE::TCP);
/*5*/Listenersocket->bind (IPEndPoint);
/*6*/Listenersocket->listen (1);
/*7*/Console::WriteLine ("Server listener blocking status is {0}",
listenersocket->blocking);
/*8*/socket^ serversocket = listenersocket->accept ();
Console::WriteLine ("New connection accepted");
/*9*/Listenersocket->close ();
/*10*/networkstream^ NetStream = gcnew NetworkStream (serversocket);
/*11*/binaryreader^ br = gcnew BinaryReader (netstream);
/*12*/binarywriter^ bw = gcnew BinaryWriter (netstream);
Try
{
int value1, value2;
int result;
while (true)
{
/*13*/value1 = Br->readint32 ();
/*14*/value2 = Br->readint32 ();
Console::write ("Received values {0,3} and {1,3}",
Value1, value2);
result = value1 + value2;
/*15*/bw->write (Result);
Console::WriteLine (", sent result {0,3}", result);
}
}
/*16*/catch (endofstreamexception^ e)
{
}
/*17*/catch (ioexception^ e)
{
Console::WriteLine ("IOException {0}", e);
}
/*18*/Serversocket->shutdown (Socketshutdown::both);
/*19*/Serversocket->close ();
/*20*/Netstream->close ();
Console::WriteLine ("Shutting down server");
}