Developing Client Application with Java



  1. JDK or MS VJ++

  2. Basic Fundamentals of Java


  1. A Basic Socket Program to Connect to a Server

Let us begin by first writing a simple client program which will talk to a server and recieve the reply.Since it is an application program,i will invoke the main function in my class.I will be using the Networking classes,hence i will have to import java.net.Here below is the code.

table1

import java.net.* ;
import java.io.* ;

public class socconnect
{

public static void main(String[] argc)
{

Socket s1;
PrintStream p1;
DataInputStream d1;
String recvreply;

}


}

s1 is an object of Class Socket used to connect to a server,p1 is an object of class PrintStream which helps in communicating via that socket and lastly a DataInputStream object is used to read bytes from the server to analyse the reply.Whenever we use the Socket object we have to catch errors and exception thrown.For this we use the try and catch system.We specify the name of the server and port to connect in the constructor.We use the Socket getInputStream() to recieve bytes from the server,specifying size of buffer(2500) and DataInputStream readLine() function to read each line of bytes and print it to the console using System.out.println.Here is the modified code now.

table2

import java.net.* ;
import java.io.* ;

public class socconnect
{

public static void main(String[] argc)
{

Socket s1;
PrintStream p1;
DataInputStream d1;
String recvreply;

try
{
s1=new Socket("202.54.16.99",25);
d1=new DataInputStream(new
BufferedInputStream(s1.getInputStream(),2500));
p1=new PrintStream(new
BufferedOutputStream(s1.getOutputStream(),2500),true);
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

s1.close();
System.out.println("Closed Connection with Server");

}

catch(IOException e)
{
System.out.println("Error in Connecting to Port");
}

}

}

IF you run this code ,you will see the reply from the server on the console.

That's it!!

In my opinion you have written the simplest client socket program now.The rest of the stuff to connect to a server can be got by reading any of the RFC s available freely on the net.

Back|Top

  1. An SMTP Client Program

Well if u have understood the above code the rest of the matter below will be a breeze.I am simply using the commands specified in RFC 821 to send a simple mail.I will be using the foll basic commands..

Here below is the complete code.By the way the Standard port to connect to in case of SMTP is 25.

table3

import java.net.*;
import java.io.*;

public class smtpclient
{

public static void main(String[] argc)
{

Socket s1;
PrintStream p1;
DataInputStream d1;
String recvreply;

try
{
s1=new Socket("202.54.16.99",25);
d1=new DataInputStream(new BufferedInputStream(s1.getInputStream(),2500));
p1=new PrintStream(new BufferedOutputStream(s1.getOutputStream(),2500),true);
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("HELO 202.54.16.99");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("MAIL FROM: <csd@rocketmail.com>");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("RCPT TO: <cdsd@rocketmail.com>");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("DATA");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);


p1.println("From : The God of Love<das@rocketmail.com>");
p1.println("Reply-To : asds@rocketmail.com");
p1.println("To : sdv@hotmail.com>");
p1.println("Subject : Gameplan for Disaster");
p1.println("Date : 5th Dec");
p1.println("Content-Type: text/html; charset=us-ascii");
p1.println("Content-Transfer-Encoding: 7-bit");


p1.println("<b>Hello sd,</b>");
p1.println("Howdy!!!.......So how are things with u");
p1.println("Just wanted to give u this link....check it out");
p1.println("\r\n");
p1.println("\r\n");
p1.println("<a href=http://www.yahoo.com>Search Engine</a>");
p1.println("\r\n");
p1.println("Bye for Now");
p1.println("<b>The God of Love</b>");


p1.println(".");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("QUIT");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

s1.close();
System.out.println("Closed Connection with Server");

}

catch(IOException e)
{
System.out.println("Error in Connecting to Port");
}


}

}
Back|Top

  1. A POP3 Client Program

Again in case of the POP3 Protocol i have used the commands specified in RFC 1939 to check my mails and manipulate it.Some of the basic commands used include

For POP3 Protocol the standard is to connect to port 110.Below is the complete code for the same.

table4

import java.net.*;
import java.io.*;

public class pop3client
{

public static void main(String[] argc)
{

Socket s1;
PrintStream p1;
DataInputStream d1;
String recvreply;

try
{
s1=new Socket("202.54.16.110",110);
d1=new DataInputStream(new BufferedInputStream(s1.getInputStream(),2500));
p1=new PrintStream(new BufferedOutputStream(s1.getOutputStream(),2500),true);
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("USER zzz");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("PASS zzz");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("STAT");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("LIST 1");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("RETR 1");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("QUIT");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

s1.close();
System.out.println("Closed Connection with Server");

}

catch(IOException e)
{
System.out.println("Error in Connecting to Port");
}


}

}
Back|Top

  1. An NNTP Client Program

For NNTP Protocol i have used the commands specified in RFC 977 to connect to a newsgroup,check articles and post one.Some of the basic commands used include the foll..

For NNTP Protocol the standard is to connect to port 119.Below is the complete code for the same.

table5

import java.net.*;
import java.io.*;

public class nntpclient
{

public static void main(String[] argc)
{

Socket s1;
PrintStream p1;
DataInputStream d1;
String recvreply;
int check;

try
{
s1=new Socket("msnews.microsoft.com",119);
d1=new DataInputStream(new BufferedInputStream(s1.getInputStream(),2500));
p1=new PrintStream(new BufferedOutputStream(s1.getOutputStream(),2500),true);
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

/*
p1.println("LIST");
while((recvreply=d1.readLine())!=".")
{
System.out.println("Server Response : " + recvreply);
}
*/

p1.println("GROUP microsoft.public.vc.mfc");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("POST");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("From: kamraj123@rocketmail.com (NNTP Client)");
p1.println("Newsgroups: microsoft.public.vc.mfc");
p1.println("Subject: Test Mail...Please Ignore");
p1.println();
p1.println();
p1.println("Testing NNTP Client,");
p1.println("Please ignore message");
p1.println("Thanks");

p1.println(".");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

p1.println("QUIT");
recvreply=d1.readLine();
System.out.println("Server Response : " + recvreply);

s1.close();
System.out.println("Closed Connection with Server");

}

catch(IOException e)
{
System.out.println("Error in Connecting to Port");
}


}

}
Back|Top

  1. Query a Search Engine

This one is a bit different.I found it in the networking examples section in Java available at the well known O'Reily site..

Here instead of using sockets i will be using another Class ...URL for searching.I did a bit of R&D on my own and found the parameter used for accepting the query and appended it to the URL object for connecting to that server.For example The Search Engine Yahoo uses the variable p to search ur query.Hence the complete URL passed will be http://search.yahoo.com/bin/ ...+ search? + p='query'

I used the same tactics to connect to other search engines as well.I used System.in to accept user query at the console connect to the server,get the context and write the resultant page into a file(search_engine.html).The code is as given below.

table6

import java.net.*;
import java.io.*;

public class searchengine
{

public static void main(String[] argc)
{

FileOutputStream fo1;
DataInputStream d1,d2;
URL u1,u2;
byte[] b1=new byte[25000];
String rdl1,name,value,query1;

System.out.println();
System.out.println();
System.out.println("-----------Search Engine!!!------------------");
System.out.println();
System.out.println();
System.out.println("Enter 'Query' Below and press Enter.....");
System.out.println("then wait for the caption 'Search Over' to appear.......... ");
System.out.println("then open c:\\temp\\search_yahoo.html to see search result...");
System.out.println();
System.out.println("Enter Query : ");

try
{

d2=new DataInputStream(System.in);
value=d2.readLine();

/*--------------Searching Yahoo below----------------------------------*/

name="p";
query1="search?" + name + "=" + value;
u1=new URL("http://search.yahoo.com/bin/");
u2=new URL(u1,query1);

fo1=new FileOutputStream("c:\\temp\\search_engine.html");
d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>Yahoo Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());

while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}



/*--------------Searching Yahoo above----------------------------------*/



/*--------------Searching Altavista below----------------------------------*/

name="q";
query1="query?pg=q&what=web&fmt=.&" + name + "=" + value;
u1=new URL("http://www.altavista.digital.com/cgi-bin/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>Altavista Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());

while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}



/*--------------Searching Altavista above----------------------------------*/



/*--------------Searching webcrawler below----------------------------------*/

name="searchText";
query1="WebQuery?" + name + "=" + value;
u1=new URL("http://www.webcrawler.com/cgi-bin/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>webcrawler Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());


while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}



/*--------------Searching webcrawler above----------------------------------*/



/*--------------Searching infoseek below----------------------------------*/

name="qt";
query1="Titles?" + name + "=" + value + "&col=WW&sv=IS&lk=ip-noframes&nh=10";
u1=new URL("http://www.infoseek.com/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>infoseek Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());

while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}


/*--------------Searching infoseek above----------------------------------*/




/*--------------Searching hotbot below----------------------------------*/

name="MC&MT";
query1="hResult.html?SM=" + name + "=" + value + "&DV=25&RG=.com&DC=10&DE=2&OPs=MDRTP&_v=2&DU=days&SW=web";
u1=new URL("http://www.search.hotbot.com/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>hotbot Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());


while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}



/*--------------Searching hotbot above----------------------------------*/



/*--------------Searching lycos below----------------------------------*/

name="query";
query1="pursuit?" + name + "=" + value + "&backlink=217&maxhits=25";
u1=new URL("https://www.lycos.com/cgi-bin/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>lycos Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());

while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}



/*--------------Searching lycos above----------------------------------*/



/*--------------Searching excite below----------------------------------*/

name="search";
query1="search.gw?" + name + "=" + value;
u1=new URL("http://www.excite.com/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>excite Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());

while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}


}

catch(IOException e)
{
System.err.println(e);
}


/*--------------Searching excite above----------------------------------*/


/*--------------Searching dejanews below----------------------------------*/

name="query";
query1="dnquery.xp?" + name + "=" + value + "&defaultOp=AND&svcclass=dncurrent&maxhits=25&site=yahoo";
u1=new URL("http://search.dejanews.com/");
u2=new URL(u1,query1);

d1=new DataInputStream(u2.openStream());

System.out.println("Starting Search.......Please Wait");

try
{

rdl1="<html><body><hr><br><hr><h1>dejanews Search Result.....</h1><hr></body></html>";
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());

while((rdl1=d1.readLine())!=null)
{
rdl1.getBytes(0,rdl1.length(),b1,0);
fo1.write(b1,0,rdl1.length());
System.out.println(rdl1);
}

fo1.close();

}

catch(IOException e)
{
System.err.println(e);
}


/*--------------Searching dejanews above----------------------------------*/




System.out.println();
System.out.println("Search Over");

}

catch(IOException e)
{
System.err.println(e);
}

}

}
Back|Top

  1. Complete Source Code

Click below to download the complete source code in zipped format.

Download

Back|Top

3rd Dec 1997

V.B Palathuruthil