The datagram mode refers to the method in which the sender sends the message to the other party and completes interaction after receiving the confirmation message. The sender only determines that the message is successfully sent, however, I do not know whether the message has reached the final node, whether it has been processed, and how the returned results are completely unknown;
The client implements the ioutputchannel, and the server implements the iinputchannel
SenderCode:
Using system. servicemodel. channels;
Using system. servicemodel;
namespace client output
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> bindingelement [] bindingelements = new bindingelement [3];
bindingelements [0] = new textmessageencodingbindingelement ();
bindingelements [1] = new onewaybindingelement ();
bindingelements [2] = new httptransportbindingelement ();
// HTTP Transmission
Custombinding binding = new custombinding (bindingelements );
Message message;
Ichannelfactory <ioutputchannel> factor = NULL;
Ioutputchannel outputchannel = NULL;
String msgbody = console. Readline ();
Message = message. createmessage (binding. messageversion, "sendmessage", msgbody );
// Create a message
Factor = binding. buildchannelfactory <ioutputchannel> (New bindingparametercollection ());
Factor. open ();
Outputchannel = factor. createchannel (New endpointaddress ("http: // localhost: 9090/inputservice "));
Outputchannel. open ();
Outputchannel. Send (Message );
Console. writeline ("message sent successfully ");
Outputchannel. Close ();
Factor. Close ();
}
}
}
Acceptor:
Using system. servicemodel. channels;
namespace Server Input
{< br> class Program
{< br> static void main (string [] ARGs)
{< br> bindingelement [] bindingelements = new bindingelement [3];
bindingelements [0] = new textmessageencodingbindingelement ();
bindingelements [1] = new onewaybindingelement ();
bindingelements [2] = new httptransportbindingelement ();
Custombinding binding = new custombinding (bindingelements );
Ichannellistener <iinputchannel> listener = binding. buildchannellistener <iinputchannel> (New uri ("http: // localhost: 9090/inputservice"), new bindingparametercollection ());
Listener. open ();
Iinputchannel inputchangel = listener. acceptchannel ();
Inputchangel. open ();
Console. writeline ("start receiving messages ");
Message message = inputchangel. Receive ();
Console. writeline ("received a new message, Action: {0}, body: {1}", message. headers. action, message. getbody <string> ());
Message. Close ();
Inputchangel. Close ();
Listener. Close ();
Console. Read ();
}
}
}
From:. net3.5 Advanced Programming