[ Back to Software Project homepage ]
Sender.cpp | Receiver.cpp |
#include "common.h" #include <iostream> using namespace std; class Packet; class Port; class Address; class SendingPort; int main(int argc, const char * argv[]) { try { const char* hname = "localhost"; |
#include "common.h" #include <iostream> using namespace std; class Packet; class Port; class Address; class ReceiverPort; int main(int argc, const char * argv[]) { try { const char* hname = "localhost"; |
The above code configures the ports with addresses and then initialize them. After that, a packet is sent from one port to another.
The Sender program takes two arguments, destination hostname and
destination port number.
The Rreceiver program takes only one argument, local port number of the
receiving port.
To have a communication link, the sending port's destination port number must match the receiving port's local port number.
g++ common.cpp sender.cpp -o senderor use the Makefile
g++ common.cpp receiver.cpp
make cleanAfter compilation, you will find two new programs "sender" and "receiver" in current directory.
make
./receiver 4000
./sender localhost 4000
Each port (sending port and receiving port) could have two addresses. One is the local address, the other is the remote address. The remote address corresponds to the local address of the "port" at the other end of communication link.
To configure a sending port, both addresses need to be configured. But to configure a receiving port, only the local address is necessary. Because it is as long as sending port configure the destination correctly, the receiving port could remains "dumb" and needn't to be aware of the remote addresses.
In the above example, we have successfully set up a simplex (not duplex) link from the sending port to the receiving port. Note that the reverse link does not exist!