0% found this document useful (0 votes)
244 views

Networkprog

The document describes several Java programs that utilize concepts like CRC checksums, sockets, RMI, JDBC, and datagrams. The crcclient and crcserver programs demonstrate calculating and verifying CRC checksums over sockets. The RMI programs AddClient, AddServer, and AddServerImpl show how to build and call a remote addition method. The stdimpl, stdinterface, stdserver, and stuclient programs retrieve student records from a database using RMI. Finally, the oobserver program receives datagrams on a port and prints the received string.

Uploaded by

parul_pandey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
244 views

Networkprog

The document describes several Java programs that utilize concepts like CRC checksums, sockets, RMI, JDBC, and datagrams. The crcclient and crcserver programs demonstrate calculating and verifying CRC checksums over sockets. The RMI programs AddClient, AddServer, and AddServerImpl show how to build and call a remote addition method. The stdimpl, stdinterface, stdserver, and stuclient programs retrieve student records from a database using RMI. Finally, the oobserver program receives datagrams on a port and prints the received string.

Uploaded by

parul_pandey
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 47

PROGRAM:

crcclient.java:
import java.io.*;
import java.util.zip.*;
import java.net.*;
class crcclient
{
public static void main(String args[])
{
try
{
InetAddress ia=InetAddress.getLocalHost();
CRC32 cc=new CRC32();
System.out.println("PLEASE ENTER THE MESAGE");
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
String s=br.readLine();
PrintStream ps;
String m;
Socket ss=new Socket(ia,8000);
cc.update(s.getBytes());
long val=cc.getValue();
ps=new PrintStream(ss.getOutputStream());
m=s+"//"+val;
System.out.println("THE MESSAGE AND THE CRC VALUE
IS");
System.out.println(m);
ps.println(m);
DataInputStream dis=new
DataInputStream(ss.getInputStream());
String str=dis.readLine();
System.out.println("THE ACKNOWLEDGEMENT RECEIVED
FROM SERVER IS \n");
System.out.println(str);
}
catch(Exception e)
{ System.out.println(e);
}
}
}

crcserver.java:
import java.util.zip.*;
import java.util.*;
import java.io.*;
import java.net.*;
class crcserver
{
public static void main(String args[])
{
try
{
CRC32 c=new CRC32();
ServerSocket ss=new ServerSocket(8000);
Socket s;
String str=" ";
String sis[]=new String[500];
while(true)
{
s=ss.accept();
DataInputStream dis=new
DataInputStream(s.getInputStream());
str=dis.readLine();
System.out.println("the message with the Crc
value received is \n");
System.out.println(str);
StringTokenizer st=new StringTokenizer(str,"//");
int n=st.countTokens();
System.out.println("number of tokens"+n);
System.out.println("the tokens received are");
for(int i=0;i<n;i++)
{
sis[i]=st.nextToken();
System.out.println(sis[i]);
}
long val1=Long.parseLong(sis[n-1]);
String s1=Long.toString(val1);
System.out.println("The calculate CRC value is");
System.out.println(val1);
c.update(sis[0].getBytes());
long val=c.getValue();
String s2=Long.toString(val);

if(s1.equals(s2))
{
String s3="GoodMessage";
PrintStream ps=new
PrintStream(s.getOutputStream());
ps.println(s3);
}
System.out.println("the message from client is
acepted");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}

OUTPUT:
SERVERSIDE:
CLIENTSIDE:

PROGRAM:
AddClient.java:
import java.rmi.*;
public class AddClient
{
public static void main(String args[])
{
try
{
String addServerURL="rmi://"+args[0]+"/AddServer";
AddServerIntf addServerIntf=(AddServerIntf)
Naming.lookup(addServerURL);
System.out.println("the first number is"+args[1]);
double d1=Double.valueOf(args[1]).doubleValue( );
System.out.println("the Second number is"+args[2]);
double d2=Double.valueOf(args[2]).doubleValue();
System.out.println("the sum is "+AddServerIntf.add(d1,d2));
}
catch(Exception e)
{
System.out.println("Exception:"+e);
}
}
}

AddServer.java:
import java.net.*;
import java.rmi.*;
public class AddServer
{
public static void main(String args[])
{
try
{
AddServerImpl addServerImpl=new AddServerImpl();
Naming.rebind("AddServer",addServerImpl);
}
catch(Exception e)
{
System.out.println("Exception :"+e);
}
}
}

AddServerImpl.java:
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements
AddServerIntf
{
public AddServerImpl()throws RemoteException
{
}
public double add(double d1,double d2)throws RemoteException
{
return d1+d2;
}
}

AddServerIntf.java:

import java.rmi.*;
public interface AddServerIntf extends Remote
{
double add(double d1,double d2)throws RemoteException;
}

OUTPUT:
SERVER SIDE:
CLIENT SIDE:

PROGRAM:
stdimpl.java
import java.sql.*;
import java.rmi.*;
import java.rmi.server.*;
public class stdimpl extends UnicastRemoteObject implements stdinterface
{
Connection con=null;
Statement stmt;
ResultSet rs;
public stdimpl()throws RemoteException
{
super();
}
public void connect()
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:dsnone");
stmt=con.createStatement();
}
catch(Exception e)
{ System.out.println(e); }
}
public student getinfo(String id)
{
student stu=new student();
try
{
connect();
rs=stmt.executeQuery("select * from student where id='"+id+"'");
rs.next();
stu.setid(rs.getString(1));
stu.setname(rs.getString(2));
stu.setbranch(rs.getString(3));
stu.setgrade(rs.getString(4));
}
catch(Exception e)
{ System.out.println(e); }
return stu;
}
}

stdinterface.java

import java.rmi.*;
import java.net.*;
public interface stdinterface extends Remote
{
public student getinfo(String id)throws RemoteException;
}

stdserver.java
import java.rmi.*;
import java.net.*;
public class stdserver
{
public static void main(String args[])
{
try
{
stdimpl impl=new stdimpl();
Naming.rebind("stdserver",impl);
}
catch(Exception e)
{
System.out.println(e);
}
}
}

stuclient.java
import java.rmi.*;
import java.io.*;
public class stuclient
{
public static void main(String args[])
{
try
{
stdinterface
intf=(stdinterface)Naming.lookup("rmi://"+args[0]+"/st
dserver");
student s=null;
System.out.println("Enter Id:");
DataInputStream dis=new DataInputStream(System.in);
String id=dis.readLine();
s=intf.getinfo(id);
System.out.println("Student ID:"+s.getid());
System.out.println("Student NAME:"+s.getname());
System.out.println("Student Branch:"+s.getbranch());
System.out.println("Student Grade:"+s.getgrade());
}
catch(Exception e)
{ System.out.println(e); }
}
}
student.java
import java.io.*;
import java.rmi.*;
import java.net.*;
public class student implements Serializable
{
String id,name,branch,grade;
public student()
{
id=null;
name=null;
branch=null;
grade=null;
}
public void setid(String idno)
{ id=idno; }
public void setname(String n)
{ name=n; }
public void setbranch(String br)
{ branch=br; }
public void setgrade(String g)
{ grade=g; }
public String getid()
{ return id; }
public String getname()
{ return name; }
public String getbranch()
{ return branch; }
public String getgrade()
{ return grade; }
}

OUTPUT:
SERVER SIDE:
CLIENT SIDE:

PROGRAM:
oobserver.java
import java.io.*;
import java.net.*;
class oobserver
{
public static void main(String args[])
{ String hostname="localhost";
String thisline;
String s;
byte[] buffer =new byte[65535];
try
{ int port=8020;
DatagramSocket ds=new DatagramSocket(port);
DataInputStream dis=new DataInputStream(System.in);
while(true)
{
DatagramPacket dp1=new
DatagramPacket(buffer,buffer.length);
ds.receive(dp1);
s=new String(dp1.getData(),0,4,dp1.getLength()-4);
int p=(dp1.getPort());
System.out.println(s);
System.out.println(p);
thisline=dis.readLine();
if(thisline.equals("."))
break;
byte[] data=new byte[thisline.length()];
thisline.getBytes(0,thisline.length(),data,0);
DatagramPacket dp= new
DatagramPacket(data,data.length,InetAddress.getByNam
e (hostname),port);
}
ds.close();
}
catch(SocketException e)
{ System.out.println(e); }
catch(UnknownHostException e)
{ System.out.println(e); }
catch(IOException e)
{ System.out.println(e); }
}}

oobclient.java:
import java.io.*;
import java.net.*;
class oobclient
{
public static void main(String args[])
{
String hostname;
String s;
try
{
if((args.length)>0)
hostname=args[0];
else
hostname="localhost";
int port=8020;
byte[] buffer=new byte[65535];
byte oob=(byte)0xff;
String thisline;
DatagramSocket ds1=new DatagramSocket();
DataInputStream dis=new DataInputStream(System.in);
while(true)
{
thisline=dis.readLine();
if(thisline.equals("."))
break;
byte[]data=new byte[thisline.length()+4];
for(int i=0;i<4;i++)
{
data[i]=oob;
}
thisline.getBytes(0,thisline.length(),data,4);
DatagramPacket dp1=new
DatagramPacket(data,data.length,InetAddress.getByNam
e (hostname),port);
ds1.send(dp1);
DatagramPacket dp=new
DatagramPacket(buffer,buffer.length);
ds1.receive(dp);
s=new String(dp.getData(),0,0,dp.getLength());
System.out.println(s);
}
ds1.close();
}
catch(SocketException e)
{
System.out.println(e);
}
catch(UnknownHostException e)
{
System.out.println(e);
}
catch(IOException e)
{
System.out.println(e);
}
}
}

OUTPUT:
OOB SERVER:
OOB CLIENT:

PROGRAM:
jdbc.java:
import java.io.*;
import java.sql.*;
class jdbc
{
public static void main(String args[])
{
ResultSet rs=null;
String stud_no=" ";
String choice=" ";
DataInputStream dis=new DataInputStream(System.in);
try
{ display_line();
System.out.println("\n");
System.out.println("Enter ur choice:");
System.out.println("Display single record(1):");
System.out.println("Display all Records(2):");
choice=dis.readLine();
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:stud");
Statement stmt=con.createStatement();
if(choice.equals("1"))
{
System.out.println("\n");
System.out.println("Entered choice is one");
System.out.println("enter the register no:");
stud_no=dis.readLine();
int j=Integer.parseInt(stud_no);
rs=stmt.executeQuery("select * from student where
registerno="+j+"");
}
if(choice.equals("2"))
{
System.out.println("\n");
System.out.println("\t Entered choice is two");
rs=stmt.executeQuery("select * from student");
}
while(rs.next())
{
System.out.println("\n");
System.out.println(rs.getString(1));
System.out.println(rs.getString(2));
}
display_line();
}
catch(Exception e)
{
System.out.println(e);
}
}
public static void display_line()
{
char ch='*';
int i=0;
for(i=0;i<75;i++)
{
System.out.print(ch);
}
}
}

OUTPUT:
STUDENT.mdb

student
registerno Name
1001 prabhu
1002 Dhanasekar
1003 Parthiban
PROGRAM:
SMTPClient.java:

import java.io.*;
import java.net.*;
class SMTPClient
{
public static void main(String args[])throws Exception
{
try
{
Socket s=new Socket("localhost",8080);
DataInputStream dis=new
DataInputStream(s.getInputStream());
DataInputStream in=new DataInputStream(System.in);
PrintStream ps=new PrintStream(s.getOutputStream());
ps.println("Ready");
String resp=dis.readLine();
System.out.println("Enter the from address:");
String strf=in.readLine();
ps.println(strf);
System.out.println("Enter the to Address:");
String strt=in.readLine();
ps.println(strt);
System.out.println("Enter the msg");
while(true)
{
String msg=in.readLine();
ps.println(msg);
if(msg.equals("quit"))
{
System.out.println("message is delivered to
server and clients quits");
break;
}
}
}
catch(Exception e)
{
System.out.println("Exception caught:"+e);
}}}

SMTPServer.java:
import java.io.*;
import java.net.*;
class SMTPServer
{
public static void main(String args[])
{
try
{ ServerSocket ss=new ServerSocket(8080);
Socket s=ss.accept();
ServiceClient(s);
}
catch(Exception e)
{ System.out.println("Exception caught in main:"+e); }
}
public static void ServiceClient(Socket s)throws Exception
{
try
{ DataInputStream dis=null;
PrintStream ps=null;
dis=new DataInputStream(s.getInputStream());
ps=new PrintStream(s.getOutputStream());
FileWriter f=new FileWriter("abc.eml");
String tel=dis.readLine();
if(tel.equals("Ready"))
System.out.println("Ready Signal received from
client:Client Accepted");
ps.println("Enter the from address:");
String from=dis.readLine();
f.write("from:"+from+"\n");
ps.println("Enter the To address:");
String to=dis.readLine();
f.write("to:"+to+"\n");
ps.println("Enter the Message");
String msg=dis.readLine();
System.out.println(msg);
f.write("\n");
f.write("Message:"+msg+"\n");
f.close();
}
catch(Exception e)
{ System.out.println("Exception caught:"+e); }}}

OUTPUT:
SERVER SIDE:
CLIENT SIDE:

abc.eml:
PROGRAM:
objectbank.java:
import java.io.*;
import java.sql.*;
public class objectbank implements Serializable
{
String Accno,Name,Gender,Balance,Date,Address;
public void bank()
{
Accno=null;
Name=null;
Gender=null;
Balance=null;
Date=null;
Address=null;
}
public void setAccno(String accno)
{ Accno=accno; }
public String getAccno()
{ return Accno; }
public void setName(String n)
{ Name=n; }
public String getName()
{ return Name; }
public void setGender(String g)
{ Gender=g; }
public String getGender()
{ return Gender; }
public void setAddress(String ad)
{ Address=ad; }
public String getAddress()
{ return Address; }
public void setDate(String d)
{ Date=d; }
public String getDate()
{ return Date; }
public void setBalance(String b)
{ Balance=b; }
public String getBalance()
{ return Balance; }}

objectclient.java:
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class objectclient
{
public static void main(String args[])
{
Socket soc=null;
PrintWriter pw=null;
ObjectInputStream in=null;
DataOutputStream dos=null;
BufferedReader br=null;
objectbank b=null;
try
{
soc=new Socket("localhost",8080);
pw=new PrintWriter(new BufferedWriter(new
OutputStreamWriter(soc.getOutputStream())),true);
dos=new DataOutputStream(soc.getOutputStream());
br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the choice : (all/recordno)");
String input=br.readLine();
dos.writeBytes(input + "\n");
in=new ObjectInputStream(soc.getInputStream());
if(input.startsWith("al"))
{
Vector v=null;
v=(Vector)in.readObject();
for(int i=0;i<v.size();i++)
{
b=(objectbank)v.elementAt(i);
System.out.println("\t ***CUSTOMER DETAILS***");
System.out.println(b.getAccno());
System.out.println(b.getName());
System.out.println(b.getGender());
System.out.println(b.getAddress());
System.out.println(b.getDate());
System.out.println(b.getBalance());
}
}

else
{
b=(objectbank)in.readObject();
System.out.println("\t ***CUSTOMER DETAILS***");
System.out.println(b.getName());
System.out.println(b.getGender());
System.out.println(b.getAddress());
System.out.println(b.getDate());
System.out.println(b.getBalance());
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

objectserver.java:

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
class objectserver
{
public static void main(String args[]) throws Exception
{
DataInputStream dis=null;
ObjectOutputStream out=null;
ServerSocket ss=new ServerSocket(8080);
Socket s=null;
Connection con;
Statement stmt;
ResultSet rs;
objectbank b=null;
String url;
Vector bank=new Vector();
while(true)
{
try
{
s=ss.accept();
dis=new DataInputStream(s.getInputStream());
out=new ObjectOutputStream(s.getOutputStream());
String in=dis.readLine();
System.out.println("Input :" +in);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url="jdbc:odbc:banking";
con=DriverManager.getConnection(url);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIV
E,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery("select * from bank");
if(in.equals("all"))
{
rs.first();
do
{
b=new objectbank();
b.setAccno(rs.getString(1));
b.setName(rs.getString(2));
b.setGender(rs.getString(3));
b.setAddress(rs.getString(4));
b.setDate(rs.getString(5));
b.setBalance(rs.getString(6));
bank.addElement(b);
}
while(rs.next());
out.writeObject(bank);
}
else
{
b=new objectbank();
rs.first();
do
{
String temp=rs.getString(1);
if(in.equals(temp))
{
b.setName(rs.getString(2));
b.setGender(rs.getString(3));
b.setAddress(rs.getString(4));
b.setDate(rs.getString(5));
b.setBalance(rs.getString(6));
}
}
while(rs.next());
out.writeObject(b);
}
}
catch(Exception e)
{ e.getMessage(); }
}
}
}

OUTPUT:
SERVER SIDE:
CLIENT SIDE:

DATA BASE:

PROGRAM:
chatclient.java:
import java.net.*;
import java.io.*;
class chatclient
{
public static void main(String ad[])throws Exception
{
Socket s;
DataInputStream dis;
PrintStream p;
InetAddress i=InetAddress.getLocalHost();
String msg;
s=new Socket(i,1711);
dis=new DataInputStream(s.getInputStream());
p=new PrintStream(s.getOutputStream());
DataInputStream uin=new DataInputStream(System.in);
String str;
str=uin.readLine();
while(!(str.equalsIgnoreCase("end")))
{
p.println(str);
msg=dis.readLine();
System.out.println("Message from Server:"+msg);
str=uin.readLine();
}
}
}

chatserver.java:

import java.net.*;
import java.io.*;
class chatserver
{
public static void main(String ad[])throws Exception
{
ServerSocket ss;
DataInputStream uin;
DataInputStream din;
PrintStream p;
Socket s;
try
{
ss=new ServerSocket(1711);
s=ss.accept();
din=new DataInputStream(s.getInputStream());
uin=new DataInputStream(System.in);
p=new PrintStream(s.getOutputStream());
String str;
String str1;
while(true)
{
str=din.readLine();
while(!(str.equalsIgnoreCase("end")))
{
System.out.println("Message from
Client:"+str);
str1=uin.readLine();
p.println(str1);
str=din.readLine();
}
}
}
catch(IOException e)
{}
}
}

OUTPUT:
SERVER SIDE:
CLIENT SIDE:

PROGRAM:
TelnetClient.java
import java.io.*;
import java.net.*;
import java.util.*;
class TelnetClient
{ public static void main(String args[]) throws Exception
{ Socket s=new Socket("localhost",8080);
DataInputStream din=new
DataInputStream(s.getInputStream());
PrintStream ps=new PrintStream(s.getOutputStream());
DataInputStream in = new DataInputStream(System.in);
ps.println("Telnet");
String str=din.readLine();
System.out.println(str);
String str1=din.readLine();
System.out.println(str1);
String uname=in.readLine();
ps.println(uname);
String str2=din.readLine();
System.out.println(str2);
String pass=in.readLine();
ps.println(pass);
String acc=din.readLine();
System.out.println(acc);
String cmd=in.readLine();
ps.println(cmd);
if(cmd.equals("dir"))
{ while(true)
{
String rec=din.readLine();
if(rec.equals("end"))
break;
System.out.println(rec);
}
}
else
{
String quit=din.readLine();
System.out.println(quit);
}
}
}

TelnetServer.java:
import java.io.*;
import java.net.*;
import java.util.*;
class TelnetServer
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(8080);
Socket s=ss.accept();
ServiceClient(s);
}
public static void ServiceClient(Socket s)throws Exception
{
DataInputStream dis=null;
PrintStream ps=null;
try
{ dis=new DataInputStream(s.getInputStream());
ps=new PrintStream(s.getOutputStream());
String tel=dis.readLine();
System.out.println(tel);
ps.println("Hello Telnet Started");
ps.println("Login :");
String uname=dis.readLine();
System.out.println(uname);
ps.println("password :");
String pass=dis.readLine();
System.out.println(pass);
if((pass.equals("admin")) && (uname.equals("admin")))
ps.println("Ok Server Accepted");
while(true)
{ String cmd=dis.readLine();
if(cmd.equals("quit"))
{
ps.println("bye");
break;
}
else if(cmd.equals("dir"))
{
File f=new File("\\");
String files[]=f.list();
for(int i=0;i<files.length;i++)
{
ps.println(files[i]);
}
ps.println("end");
break;
}
}
}
finally
{
ps.close();
dis.close();
s.close();
}}}

OUTPUT:
CLIENT SIDE:
SERVER SIDE:

PROGRAM:
CookieClient.java:
import java.io.*;
import java.net.*;
import java.util.*;
class CookieClient
{
public static void main(String args[])
{
Socket soc=null;
DataInputStream dis=null;
DataOutputStream dos=null;
String request=null;
String resp=null;
try
{
System.out.println("Connecting to Server" + "\n");
soc=new Socket("LocalHost",8255);
dis=new DataInputStream(soc.getInputStream());
dos=new DataOutputStream(soc.getOutputStream());
request=("GET:home.html");
System.out.println("Request sent : "+request+"\n");
dos.writeBytes(request + "\n");
resp=dis.readLine();
System.out.println("Response : "+resp+ "\n");
Thread.sleep(1000);
request="cookie: "+resp.substring(10);
System.out.println("Request send : "+request+ "\n");
dos.writeBytes(request+ "\n");
resp=dis.readLine();
System.out.println("Closing Connections");
soc.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

CookieServer.java:
import java.io.*;
import java.net.*;
import java.util.*;
class CookieServer
{
public static void main (String args[])
{
ServerSocket ss=null;
Socket s=null;
DataInputStream dis=null;
DataOutputStream dos=null;
String name=null;
String value=null;
Date expires=null;
String domain=null;
String path=null;
String issecure=null;
String cookie=null;
String request=null;
try
{
System.out.println("Server Serialization");
ss=new ServerSocket(8255);
System.out.println("Waiting for request ");
s=ss.accept();
dis=new DataInputStream(s.getInputStream());
dos=new DataOutputStream(s.getOutputStream());
name="Test Cookie";
value="ABCDEF";
expires=new Date();
domain="localhost";
path="/";
issecure="TRUE";
cookie="Set-Cookie: NAME = "+name+";VALUE = "+value+";EXPIRES =
"+expires+";DOMAIN = "+domain+";PATH = "+path+";ISSECURE =
"+issecure;
request=dis.readLine();
System.out.println("Request Received : " +request);
if(request.startsWith("GET"))
{
System.out.println("Send : " +request.substring(4) + "\n");
System.out.println("Send : " +cookie+ "\n");
dos.writeBytes(cookie + "\n");
}

request=dis.readLine();
System.out.println("Request Received : "+request+ "\n");
if(request.startsWith("cookie"))
{
cookie="Set-Cookie: NAME = "+name+";VALUE = "+value+";EXPIRES =
"+expires+";DOMAIN = "+domain+";PATH = "+path+";ISSECURE =
"+issecure;
System.out.println("Send : " +cookie+ "\n");
dos.writeBytes(cookie + "\n");
}
System.out.println("Closing Connection");
}
catch(IOException e)
{
System.out.println(e);
}
}
}

OUTPUT:

SERVER SIDE:
CLIENT SIDE:

PROGRAM:
WebClient.java:
import java.io.*;
import java.net.*;
class WebClient
{
public static void main (String args[])
{
try
{
Socket s1 = new Socket ("localhost",8020);
System.out.println("Client1 : " +s1);
getPage(s1);
}
catch (UnknownHostException e)
{
System.out.println(e);
}
catch (IOException e)
{
System.out.println(e);
}
}
public static void getPage(Socket s)
{
try
{
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
DataInputStream dis = new DataInputStream(s.getInputStream());
dos.writeBytes("GET/HTTP/1.0\r\n\r\n");
String responseLine;
responseLine=dis.readLine();
System.out.println(responseLine);
dos.close();
dis.close();
s.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}

Webserver.java:
import java.io.*;
import java.net.*;
class WebServer
{public static void main(String args[])
{
ServerSocket ss=null;
Socket s=null;
int connects=0;
try
{
ss=new ServerSocket(8020,5);
while (connects < 5)
{
s=ss.accept();
ServiceClient(s);
connects++;
}
ss.close();
}
catch (Exception e)
{
System.out.println(e);
}}
public static void ServiceClient(Socket client) throws IOException
{
DataInputStream dis = null;
DataOutputStream dos = null;
try
{dis = new DataInputStream(client.getInputStream());
dos = new DataOutputStream(client.getOutputStream());
StringBuffer buf = new StringBuffer("<html><body><p>Welcome to Simple
Web Page </body></html>");
dos.writeBytes(buf.toString());
}
finally
{
System.out.println("Cleaning up Connection : " +client);
dos.close();
dis.close();
client.close();
}}}

OUTPUT:
SERVER SIDE:
CLIENT SIDE:

CODING:
%{
int COMMENT=0;
%}

identifier[_a-zA-Z][_a-zA-Z0-9]*

%%

#.* {
printf("\n %s \t preprocessor",yytext);
}

int |
float |
char |
double |
while |
for |
do |
if |
break |
continue |
void |
switch |
case |
long |
struct |
const |
typedef |
return |
else |
goto
{
printf("\n %s \t keyword",yytext);
}
"/*"{identifier}
{
COMMENT=1;
printf("\nComment Begins");
}
"*/"
{
COMMENT=0;
printf("\n Comment Ends");
}
{identifier}\(
{
if(!COMMENT)
printf(" function \n %s",yytext);
}
\{ {
if(!COMMENT)
printf("\nBlock Begins");
}

\} {
if(!COMMENT)
printf("\n Block Ends");
}

{identifier}(\[[0-9]*\])? {
if(!COMMENT)
printf("\n %s \t identifier",yytext);

\".*\" {
if(!COMMENT)
printf("\n %s \t string",yytext);
}

[0-9]+ {
if(!COMMENT)
printf("\n %s \t number",yytext);
}

\)(\;)? {
if(!COMMENT)
printf("\n\t");ECHO;
printf("\n");
}
\( ECHO;

={
if(!COMMENT)
printf("\n %s \t assignment operator",yytext);
}
\<= |
\>= |
\< |
\> |
\== {
if(!COMMENT)
printf("\n %s \t relational operator",yytext);
}
\\n
%%
int main(int argc,char **argv)
{
if(argc>1)
{
FILE *file;
file=fopen(argv[1],"r");
if(!file)
{
printf("\nCould not open %s \n",argv[1]);
exit(0);
}
yyin=file;
}
yylex();
printf("\n");
return 0;
}

int yywrap()
{
return 0;
}
INPUT:
#include<stdio.h>
double area_of_circle(double r);
int main(int argc, char *argv[])
{
if(argc<2)
{
printf(“Usage:%s radius\n”,argv[0]);
exit(1);
}
else
{
double radius=atof(argv[1]);
double area=area_of_circle(radius);
printf(“Area of circle with radius
%f=%f”,radius,area);
}
return 0;
}

OUTPUT:

#include<stdio.h> preprocessor

r identifier
);
argc identifier, *
argv[] identifier
)

Block Begins

argc identifier
< relational operator
2 number
)

Block Begins

"Usage:%s radius \n" string,


argv[0] identifier
);

1 number
);

Block Ends

Block Begins

radius identifier
= assignment operator
argv[1] identifier
);

area identifier
= assignment operator
radius identifier
);

"Area of circle with radius %f=%f\n" string,


radius identifier,
area identifier
);
Block Ends

0 number;

Block Ends

You might also like