|
Networking
You are welcomed to use our site for your web search.
See the fine products advertised at the bottom of each page too.
EXAMPLES ON NETWORKING
======================
Networking is a very wide field. When we think about network programming
for personal use, we think about very exciting projects.
Accessing the internet to get the latest stock prices and automatically
update your files and calculate the total value of your investment, all as one program is one exciting project.
Accessing your personal web site using the FTP protocol to download
files, update them then upload them back automatically without manual operations is also an exciting project.
Writing a program which checks your e-mail, file the useful items
and delete the rest is also nice to do. Writing a program which sends e-mail to your friends automatically when some
event occurs can also be a great project.
So, let us see how we can do some of these exciting programming projects.
========================================================================================= EXAMPLE
1: We'll write a program which we can run at any time to get the latest stock price of Microsoft from Yahoo's Financial
website. ========================================================================================= // -----------------------
FIRST STEP: Getting the html file --------------------------- public class a:pcs { public override void init()
{ tia=toa="t";
// Select "text screen" display bli=1;
// Start execution at block 1 base.init();
// Initialize PC# classes } public override void run() { if (blp==1) {
urs="http://finance.yahoo.com/q?d=s&s=msft";// Yahoo's URL for getting msft price jb=true;kb=true;nm("hg");tm();
//Get html file replacing {}<> with |'s } } } ========================================================================================= TUTORIAL:
The address for Yahoo's financial web site for quotes is only:
The rest of the URL is the query string. It starts with '?' followed
with a number of "parameter=value" strings seperated with '&' char's. We can't tell for sure what Yahoo means
by their parameters, but we can guess that "d=s" means data is for "Stock" and that "s=msft" means Symbol=Microsoft's stock
symbol.
Calling the Networking method nm() at mode "hg" gets the html file.
The html file is huge in size, so we must think of eliminating some of its unnecessary text in order to reach where
Microsoft's stock price is faster.
We know that HTML is full of tags inclosed in brackets like these
< > which we have no need for. Also there are plenty of data between braces { } which we don't need. We can eliminate
both types if ws supply the method with (jb=true;kb=true;) Instead of just removing them, the method replaces each pair
of them and the data in between with one '|' since this may help us when we search for the data.
After this elemination, the file we get may have lost 2/3 of its size,
so we can at least look through it to know where the wanted data is. Here is what we see: =========================================================================================

Example 1: Looking at the HTML file. The arrow shows where wanted data
is
// ------------------------- SECOND STEP: Searching the text --------------------------- public
class a:pcs { public override void init() { tia=toa="t";
// Select "text screen" display bli=1;
// Start execution at block 1 base.init();
// Initialize PC# classes } public override void run() { if (blp==1) {
urs="http://finance.yahoo.com/q?d=s&s=msft";// Yahoo's URL for getting msft price jb=true;kb=true;nm("hg");//tm();
//Get html file replacing {}<> with |'s txs=os;
// Assign file to search string (txs) js="Last Trade:";ks="Trade Time:";tm("s"); //
Get data between js,ks in (os) char[] CC=os.ToCharArray();
// Convert (os) to Char array to scan it os="";
// Reset (os) for (n=0;n<CC.Length;n++) {
// Scan all char's if (CC[n]!='|') os+=CC[n];
// Add all chars to (os) except |'s } os="Latest stock
price for Microsoft: $"+os;tm();
// Display data. } } } ========================================================================================= TUTORIAL:
Inspecting the received HTML file tells us that the data is located between the two phrases ""Last Trade:" and "Trade Time:".
This can be handled easily if we use the string search of method tm() at mode "s". The string the method can search
is (txs), so we need to assign (os) to (txs) before all. One of the search method abilities is to get for us the text
between two strings, and this is what we did to get the data.
The data came with some '|' chars around it so we needed to scan the
array and eliminate them as the last step. And this has done it all. =========================================================================================

Example 1: Last step, file is searched, data obtained and purified then
displayed.
EXAMPLE 2: This program will access your web site at your using FTP
protocol and do the following in sequence:
(1) Create new direcory named "dir1". (2) Upload the local
file "test.txt" from your working directory to the new directory at your website. Before you start,
you need to create the "test.txt" file at your working directory and write some text into it using Notepad. (3)
Download the same file from the server back to your working directory using the new name "test1.txt"
for the received file. (4) Delete the file "test.txt" at your website. (5) Delete the directory "dir1" at your website.
So at the end your website will be restored to original condition
and your working directory will contain the two files "test.txt" and "test1.txt" containing the sane data.
We'll display a dialog box before the execution of each operation
to give you the choice to either perform it or skip it. This will also allow you the time to access your website manually
to cofirm that the previous operation has been executed properly.
You need to modify some lines, replacing "Your user ID", "Your Password"
and "Your server" with your actual data. The lines which need modifications are marked with "***". ========================================================================================= public
class a : pcs {
// Always remember, class name = file name public override void init() { tia=toa="t";
// Use Text Screen for text display base.init();
// Should be last statement in init() } public override void run() {
ids="your user ID";pss="Your Password";// *** You
need to modify this line so it
// contains your actual user ID & Password. // --------------------------------- CREATING DIRECTORY ---------------------------------
cls="r0";os="CREATING NEW DIRECTORY:";tm();
// Display in red on Text Screen cls="S9";os="Create new directory (dr1)?";ks="yn";cm("d");
// Display choice dialog with "yes/no" buttons if(os.IndexOf("y")>-1) {
// If user clicked on "Yes" urs="ftp://your server.com/dir1";nm("fmd");// *** You need to modify this line so it
// contains the actual name of your server tm();
// Display the FTP status returned. } // ----------------------------------- UPLOADING A FILE
---------------------------------- cls="r0";os="\nUPLOADING A FILE:";tm(); cls="S9";os="Click
OK to upload file (test.txt)";ks="yn";cm("d"); if(os.IndexOf("y")>-1) {
fls="test.txt"; urs="ftp://your server.com/dir1/test.txt";// *** Modify
nm("fpf"); tm(); } // ----------------------------------
DOWNLOADING A FILE ---------------------------------- cls="r0";os="\nDOWNLOADING A FILE:";tm();
cls="S9";os="Click OK to download file (test.txt)";ks="yn";cm("d"); if(os.IndexOf("y")>-1) {
urs="ftp://your server.com/dir1/test.txt";// *** Modify
fls="test1.txt"; nm("fgf"); tm();
} // ---------------------------------- DELETING THE FILE -----------------------------------
cls="r0";os="\nDELETING THE FILE:";tm(); cls="S9";os="Click OK to delete the file (test.txt)";ks="yn";cm("d");
if(os.IndexOf("y")>-1) { urs="ftp://your server.com/dir1/test.txt";// *** Modify
nm("fdf"); tm(); } // ------------------------------- DELETING
THE DIRECTORY --------------------------------- cls="r0";os="\nDELETING DIRECTORY:";tm();
cls="S9";os="Click OK to delete new directory (dr1)";ks="yn";cm("d"); if(os.IndexOf("y")>-1) {
urs="ftp://your server.com/dir1"; // ***
Modify nm("fdd"); tm(); }
} } ========================================================================================= TUTORIAL: The ftp
address always starts with "ftp://" since this part represents the protocol. Additionally, some ftp server names also start
with "ftp" like "ftp://ftp.microsoft.com.
Authentication is a must for ftp servers. The ones which accept anonymous
caller expect you to use the word "anonymous" as your user ID and your e-mail address as your password.
There is nothing else to explain in this section, we hope you have
had no problem with this example.

Example 2: Shows how to use FTP protocol to create and delete files
and directories at your website
and how to upload and download files.
EXAMPLE 3: Now we are going to send an e-mail with an attachment file
programatically.
If your outgoing mail server requires authentication, you need to
supply your User ID and Password assigned to (ids,pss) respectively. If it does not, supply (ids=pss="";)
You need to supply the e-mail address of both sender and recipient.
If you decide to send the message to yourself, use your e-mail address for both.
We are going to be using "pix.jpg" as the attachment file. Make sure
it is available at your working directory. It will be considered your personal picture which you send as a gift to your
friend.
The lines which require your modifications are marked with "***". ========================================================================================= public
class a:pcs { public override void init() { tia=toa="t"; base.init();
} public override void run() { gm("dn");
// Stop display to avoid flickering while
// message is sent uhs="Your SMTP Server Name.com"; // *** Replace
with your SMTP server address. ids="Your User ID";pss="Your Password";// *** Replace with actual ID,
Password. js="YourID@YourHost.com";
// *** Sender E-Mail address ks="HerID@HerHost.com";
// *** Recipient E-Mail address os="My gift to you.";
// Message Subject OS[0]="Dear Friend";
// Message Body made of 2 lines. OS[1]="My picture to you with love!"; fls="pix.jpg";
// File to be attached ib=true;nm("ms");
// Send the message with attachment gm("dn");
// Restore display os="Message Sent.";tm();
// Inform user } }
=========================================================================================
EXAMPLE 4: This example will show you how to obtain a list of all
new messages which your incoming mail server has received for you, how to retrieve any message on the list and how to
delete a message.
Before we get to the example, you need to know how the "Outlook Express"
or similar mail utilities do when they receive your mail. They obtain the list of new messages from the server, retrieve
a copy of each message, store it locally then instruct the server to delete the message there. They do this operation each
specific number of minutes while the computer is on depending on your settings.
The messages which you see when you check the inbox of the "Outlook
Express" have already been deleted at the server. So don't expect to find them when you run your program to check for
new messages. Your program can only find the few messages which have been received at the server and the "Outlook Express"
has not received them yet.
So, you may like to temporarely disable the Outlook Express's automatic
message retrieval feature in order to give your program a chance to find enough new messages. To do so, run the "Outlook
Express", click on [Tools] then [Options]. At the "General" page of the "Options" window, uncheck the Checkbox labled
"Check for new messages every.." then click on [Apply], [OK].
You may also send yourself some messages to add to the list of messages
which your program is going to find.
The lines which require your modifications are marked with "***".
========================================================================================= public
class a:pcs {
public override void init() { tia=toa="t";
bli=0; base.init(); } public override void run() { if (blp==0)
{
// Obtaining the list cls="r0";os="GETTING NEW MESSAGES LIST:";tm();
uhs="Your POP Server Name.com";ids="Your User ID";pss="Your Password";// ***
// *** Replace with your actual data. nm("mc");cls="S9";tm();
// Get new msg's list and display cls="b0";os="Enter
the number of the message which you like to retrieve."; bli=1;tm("i");return;
// Get info from user then goto blk 1 } if (blp==1) {
// Retrieving a message om("ti");n=o;
// Convert msg no. to int, assign to (n) cls="r0";os="RETRIEVING A MESSAGE:";tm();
i=n;nm("mr");
// Retrieve message cls="S9";tm();
// Display message cls="b0";os="Enter the number of the message which you like to delete.";
bli=2;tm("i");return;
// Get info from user then goto blk 2 } if (blp==2) {
// Deleting a message om("ti");n=o;
// Convert msg no. to int, assign to (n) cls="r0";os="DELETING A MESSAGE:";tm();
i=n;nm("md");
// Delete message cls="S9";tm();
// Display server cofirmation msg. cls="b0";os="Enter 'r' to repeat program or 'e' to exit.";
bli=3;tm("i");return;
// Get info from user then goto blk 3 } if (blp==3) {
// Executing what to do next om("l");
// Convert user's choice to lower case if (os.Equals("e")) sm("e");
// Exit prog if "e" was selected else bli=0;um("b");return;
// Jump to block 0 if "r" was selected. } } } ========================================================================================= TUTORIAL:
The new message list which you see on display shows the message number followed with a number indicating the message size.
The message number which starts by "1" is the number to use to tell the server which message you like to retrieve or delete.
The messages which you receive from the mail server contain very large
amount of information which you don't need. So you need to search the data and obtain the data which you need.
=========================================================================================

Examples 3, 4: Show how to send, retrieve and delete an e-mail message.
EXAMPLE 5: So far, all the networking examples which you have seen,
did not show the actual communication between client and server. This example will get deeper and show the actual dialog
which happens when we call the incoming mail server and ask for new messages list.
The lines which require your modifications are marked with "***". ========================================================================================= public
class a:pcs {
public override void init() { tia=toa="t";
bli=0; base.init(); } public override void run() { if (blp==0)
{ cls="r0";fns="trb14";tm("c");
// Set font,clor, clear screen. os="
COMMUNICATING WITH INCOMING MAIL SERVER\n";tm(); fns="trb12";
uhs="Your POP Server Name.com";ids="Your User ID";pss="Your Password";// ***
// *** Replace with actual data cls="b0";os="CLIENT: Connecting ...";tm();
// Announce next action upi=110;nm("to");
// Open connection nm("tr");
// Read server response cls="S9";os="SERVER: "+os;tm();
// Display server response cls="b0";os="CLIENT: user 'Your User ID'";tm(); // Announce next
action os="user "+ids+'\n';nm("tw");
// Send "user" command nm("tr");
// Read server response cls="S9";os="SERVER: "+os;tm();
// Display server response cls="b0";os="CLIENT: pass 'Your Password'";tm();// Announce next
action os="pass "+pss+'\n';nm("tw");
// Send "Pass" Command nm("tr");
// Read server response cls="S9";os="SERVER: "+os;tm();
// Display server response cls="b0";os="CLIENT: list";tm();
// Announce next action os="list"+'\n';nm("tw");
// Send "list" conmmand nm("tr");
// Read server response cls="S9";os="SERVER: "+os;tm();
// Display server response cls="b0";os="CLIENT: quit";tm();
// Announce next action os="quit"+'\n';nm("tw");
// Send "quit" command nm("c");
// Close connection } } } =========================================================================================

EXAMPLE 5: Shows how to use lower level TCP-IP networking
to connect to incoming mail server,
exchange messages with it then close the connection.
|