1.Chat Prgm: Chatclient: import java.io.*; import java.net.

*; class UDPClient { public static DatagramSocket clientsocket; public static DatagramPacket dp; public static BufferedReader dis; public static InetAddress ia; public static byte buf[] = new byte[1024]; public static int cport = 789, sport = 790; public static void main(String[] a) throws IOException { clientsocket = new DatagramSocket(cport); dp = new DatagramPacket(buf, buf.length); dis = new BufferedReader(newsInputStreamReader(System.in)); ia = InetAddress.getLocalHost(); System.out.println("Client is Running... Type 'STOP' to Quit"); while(true) { String str = new String(dis.readLine()); buf = str.getBytes(); if(str.equals("STOP"))

{ System.out.println("Terminated..."); clientsocket.send(newDatagramPacket(buf,str.length(), ia,sport)); break;} clientsocket.send(new DatagramPacket(buf,str.length(), ia, sport)); clientsocket.receive(dp); String str2 = new String(dp.getData(), 0, dp.getLength()); System.out.println("Server: " + str2); }}} CharServer: import java.io.*; import java.net.*; class UDPServer { public static DatagramSocket serversocket; public static DatagramPacket dp; public static BufferedReader dis; public static InetAddress ia; public static byte buf[] = new byte[1024]; public static int cport = 789,sport=790; public static void main(String[] a) throws IOException { serversocket = new DatagramSocket(sport); dp = new DatagramPacket(buf,buf.length);

dis = new BufferedReader(new InputStreamReader(System.in)); ia = InetAddress.getLocalHost(); System.out.println("Server is Running..."); while(true) { serversocket.receive(dp); String str = new String(dp.getData(),0,dp.getLength()); if(str.equals("STOP")) { System.out.println("Terminated..."); break; } System.out.println("Client: " + str); String str1 = new String(dis.readLine()); buf = str1.getBytes(); serversocket.send(newDatagramPacket(buf,str1.length(), ia, cport)); }}} 2.TCP Prgm TCPClient import java.io.*; import java.net.*; class TCPClient { public static void main(String argv[]) throws Exception {

String FromServer; String ToServer; Socket clientSocket = new Socket("localhost", 5000); BufferedReader inFromUser =new BufferedReader(new InputStreamReader(System.in)); PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(),true); BufferedReader inFromServer = new BufferedReader(new InputStreamReader( clientSocket.getInputStream())); while (true) { FromServer = inFromServer.readLine(); if ( FromServer.equals("q") || FromServer.equals("Q")) { clientSocket.close(); break; } else { System.out.println("RECIEVED:" + FromServer); System.out.println("SEND(Type Q or q to Quit):"); ToServer = inFromUser.readLine(); if (ToServer.equals("Q") || ToServer.equals("q")) { outToServer.println (ToServer) ; clientSocket.close();

break; } else { outToServer.println(ToServer); } } } }}

TCPServer: import java.io.*; import java.net.*; class TCPServer { public static void main(String argv[]) throws Exception { String fromclient; String toclient; ServerSocket Server = new ServerSocket (5000); System.out.println ("TCPServer Waiting for client on port 5000"); while(true) { Socket connected = Server.accept(); System.out.println( " THE CLIENT"+" "+ connected.getInetAddress() +":"+connected.getPort()+" IS CONNECTED "); BufferedReader inFromUser = newBufferedReader(newInputStreamReader(System.in)); BufferedReader inFromClient =new BufferedReader(new InputStreamReader (connected.getInputStream()));

PrintWriter outToClient = new PrintWriter(connected.getOutputStream(),true); while ( true ) { System.out.println("SEND(Type Q or q to Quit):"); toclient = inFromUser.readLine(); if ( toclient.equals ("q") || toclient.equals("Q") ) { outToClient.println(toclient); connected.close(); break; } else { outToClient.println(toclient); } fromclient = inFromClient.readLine(); if ( fromclient.equals("q") || fromclient.equals("Q") ) { connected.close(); break; } else { System.out.println( "RECIEVED:" + fromclient );

}}} 3.UDPPrgm UDPClient: import java.net.*; import java.io.*; class UDPClient {

}}

public static void main(String args[]) throws Exception { byte[] send_data = new byte[1024]; BufferedReader infromuser = new BufferedReader(new InputStreamReader(System.in)); DatagramSocket client_socket = new DatagramSocket(); InetAddress IPAddress = InetAddress.getByName("127.0.0.1"); while (true) { System.out.println("Type Something (q or Q to quit): ");

String data = infromuser.readLine(); if (data.equals("q") || data.equals("Q")) break; else { send_data = data.getBytes();

DatagramPacket send_packet = new DatagramPacket(send_data,send_data.length, IPAddress, 5000); client_socket.send(send_packet); } } client_socket.close(); }} UDPServer import java.net.*; import java.io.*; class UDPServer { public static void main(String args[]) throws Exception { byte[] receive_data = new byte[1024]; byte[] send_data = new byte[1024]; int recv_port; DatagramSocket server_socket = new DatagramSocket(5000); System.out.println ("UDPServer Waiting for client on port 5000") while(true) { DatagramPacket receive_packet = new DatagramPacket(receive_data, receive_data.length); server_socket.receive(receive_packet);

String data = new String(receive_packet.getData(),0,0 ,receive_packet.getLength()); InetAddress IPAddress = receive_packet.getAddress(); recv_port = receive_packet.getPort(); if (data.equals("q") || data.equals("Q")) break; else System.out.println("( " + IPAddress + " , " + recv_port + " ) said :" + data ); }}} 4..HTMLprgm: HTMLClient: import java.io.*; import java.net.*; class clihtml { public static void main(String ar[]) { try { Socket s=new Socket("localhost",8888); DataInputStream in =new DataInputStream(System.in); DataInputStream din=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); String str="",str1="",encrypt="";

System.out.println("Enter HTML File Name"); str=in.readLine(); for(int i=0;i<str.length();i++) { char ch=str.charAt(i); encrypt+=(char)((int)ch+2); } p.println(encrypt); while(str1!=null) { str1=din.readLine(); System.out.println(str1); } } catch(Exception e) {}}} HTMLServer: import java.io.*; import java.net.*; class serhtml { public static void main(String ar[]) { try {

ServerSocket ss=new ServerSocket(8888); Socket s=ss.accept(); DataInputStream in=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); String str,str1,decrypt=""; str=in.readLine(); for(int i=0;i<str.length();i++) { char ch=str.charAt(i); decrypt+=(char)((int)ch-2); } FileReader fr=new FileReader(decrypt); System.out.print(decrypt); BufferedReader br=new BufferedReader(fr); while((str1=br.readLine())!=null) { System.out.println(str1); p.println(str1); } Runtime.getRuntime().exec("C:\Program Files\Mozilla Firefox\firefox.exe" +" "+decrypt); } catch(Exception e) { System.out.println(e);

} }} 5.HEXA conversion: HexaClient: import java.io.*; import java.net.*; class clit { public static void main(String ar[]) { try { Socket s=new Socket("localhost",7212); DataInputStream in=new DataInputStream(System.in); PrintStream p=new PrintStream(s.getOutputStream()); String str; System.out.println("Enter IP Adress"); str=in.readLine(); p.println(str); } catch(Exception e) { System.out.println(e); } } }

HexaServer:

import java.io.*; import java.net.*; import java.util.*; class serv { public static void main(String ar[]) { try { ServerSocket ss=new ServerSocket(7212); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); String str,str1="",str2="",str3="",str4="",hex; int i,j,k,l; str=din.readLine(); System.out.println(str); StringTokenizer st=new StringTokenizer(str,"."); if(st.countTokens()==4) { while(st.hasMoreTokens()) { str1=st.nextToken(); str2=st.nextToken();

str3=st.nextToken(); str4=st.nextToken(); } i=Integer.parseInt(str1); j=Integer.parseInt(str2); k=Integer.parseInt(str3); l=Integer.parseInt(str4); str1=Integer.toHexString(i); str2=Integer.toHexString(j); str3=Integer.toHexString(k); str4=Integer.toHexString(l); hex=str1+"."+str2+"."+str3+"."+str4; System.out.println("Equivalent Hexadecimal IP Address"); System.out.println(hex); } else System.out.println("Invalid IP Addresss"); } catch(Exception e) { System.out.println(e); } } }

6.DownloadFile Downloadclient:
import java.net.*; import java.io.*; class fcli1 { public static void main(String srgs[])throws IOException { Socket s=null; BufferedReader get=null; PrintWriter put=null; try { s=new Socket("127.0.0.1",8081); get=new BufferedReader(new InputStreamReader(s.getInputStream())); put=new PrintWriter(s.getOutputStream(),true); } catch(Exception e) { System.exit(0); } InputStreamReader get2=new InputStreamReader(s.getInputStream()); String u,f; System.out.println("Enter the file name to transfer from server:");

DataInputStream dis=new DataInputStream(System.in); f=dis.readLine(); put.println(f); File f1=new File("e:\\anuja"); FileOutputStream fs=new FileOutputStream(f1); BufferedInputStream d=new BufferedInputStream(s.getInputStream()); BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(f1)); byte buffer[] = new byte[1024]; int read; while((read = d.read(buffer))!=-1) { outStream.write(buffer, 0, read); outStream.flush(); } fs.close(); System.out.println("File received"); s.close(); }} DownloadServer: import java.net.*; import java.io.*; public class fser1 { public static void main(String args[])throws IOException

{ ServerSocket ss=null; try { ss=new ServerSocket(8081); } catch(IOException e) { System.out.println("couldn't listen"); System.exit(0); } Socket cs=null; try { cs=ss.accept(); System.out.println("Connection established"+cs); } catch(Exception e) { System.out.println("Accept failed"); System.exit(1); } PrintWriter put=new PrintWriter(cs.getOutputStream(),true); BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream()));

String s=st.readLine(); System.out.println("The requested file is : "+s); File f=new File(s); if(f.exists()) { BufferedInputStream d=new BufferedInputStream(new FileInputStream(s)); BufferedOutputStream outStream = new BufferedOutputStream(cs.getOutputStream()); byte buffer[] = new byte[1024]; int read; while((read = d.read(buffer))!=-1) { outStream.write(buffer, 0, read); outStream.flush(); } d.close(); System.out.println("File transfered"); cs.close(); ss.close(); } } }

7:Database Program: Client:
import java.net.*;

import java.io.*; import java.sql.*; import java.util.*; class clientda { public static void main(String a[]) { String res=""; try { Socket cs=new Socket("localhost",8888); System.out.println("Connected with Server..."); DataInputStream dis = new DataInputStream(cs.getInputStream()); DataInputStream udis = new DataInputStream(System.in); PrintStream ps =new PrintStream(cs.getOutputStream()); while(true){ System.out.println("Database Server"); System.out.println("1:Add Data "); System.out.println("2:View Data "); System.out.println("3:Exit"); System.out.print("\n\nEnter the Choice :"); int choice=Integer.parseInt(udis.readLine()); switch(choice) { case 1:

ps.println("1"); System.out.print("Enter Name : "); String nam=udis.readLine(); System.out.print("Enter City : "); String city =udis.readLine(); System.out.print("Enter Age : "); String age =udis.readLine(); String inp=nam+"@"+city+"@"+age; ps.println(inp); break;

case 2: ps.println("2"); res=dis.readLine(); StringTokenizer Stt =new StringTokenizer(res,"#"); while(Stt.hasMoreTokens()) { String temp=Stt.nextToken(); System.out.println(temp); } break; case 3: ps.println("3"); System.exit(0); default:

System.out.print("Enter choice between 1 and 3"); break; } } } catch(Exception e) { e.getMessage(); } } } Server: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class serverda { public static void main(String a[]) { Connection con; Statement stat; try { System.out.println("Waiting for Client..."); ServerSocket ss= new ServerSocket(8888); Socket s=ss.accept(); System.out.println("Client Connected"); DataInputStream dis = new DataInputStream(s.getInputStream()); PrintStream ps =new PrintStream(s.getOutputStream()); while(true){

int ch=Integer.parseInt(dis.readLine()); switch(ch) { case 1: int pk=0;int c=0; String inp[] = new String[3]; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:user","",""); stat=con.createStatement(); ResultSet rs = stat.executeQuery("select * from user"); while(rs.next()) pk=Integer.parseInt(rs.getString(1)); pk+=1; rs.close(); con.close(); stat.close(); String data=dis.readLine(); StringTokenizer stt = new StringTokenizer(data,"@"); while(stt.hasMoreTokens()) { inp[c]=stt.nextToken(); c++; }

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:user","",""); stat=con.createStatement(); int age=Integer.parseInt(inp[2]); sint cc=stat.executeUpdate("INSERT INTO user (id,name,address,age) VALUES ("+pk+",'"+inp[0]+"','"+inp[1]+"',"+age+")"); con.close(); stat.close(); break;

case 2: String resq=""; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:user","",""); stat=con.createStatement(); rs = stat.executeQuery("select * from user"); while(rs.next()) { String temp=rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4); resq+=temp+"#"; } ps.println(resq); con.close(); stat.close(); break; case 3: System.exit(0); } } } catch(Exception e) { e.printStackTrace(); } } }

8.( concatenate of keys) CLIENT 1:
import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class client1 {

public static void main(String a[]) throws Exception { Socket S1=new Socket(InetAddress.getLocalHost(),7070); System.out.println("Connected with Server1"); DataInputStream DIS = new DataInputStream(System.in); DataInputStream DS1 = new DataInputStream(S1.getInputStream()); PrintStream PS = new PrintStream(S1.getOutputStream()); System.out.print("Enter key1 : "); String k1=DIS.readLine(); PS.println(k1.trim()); System.out.println(DS1.readLine());

} } CLIENT 2: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class client2 { public static void main(String a[]) throws Exception { Socket S2=new Socket(InetAddress.getLocalHost(),7000); System.out.println("Connected with Server1"); DataInputStream DIS = new DataInputStream(System.in); DataInputStream DS2 = new DataInputStream(S2.getInputStream()); PrintStream PS = new PrintStream(S2.getOutputStream()); System.out.print("Enter key1 : "); String k2=DIS.readLine(); PS.println(k2.trim()); System.out.println(DS2.readLine());

} } SERVER 1: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class server1 { public static void main(String a[]) throws Exception { Socket S1=new Socket(InetAddress.getLocalHost(),7777); System.out.println("Connected with Server2"); ServerSocket CSS1 = new ServerSocket(7070); Socket CS1 =CSS1.accept(); ServerSocket CSS2 = new ServerSocket(7000); Socket CS2 =CSS2.accept(); DataInputStream DIS1 = new DataInputStream(S1.getInputStream()); DataInputStream CIS1 = new DataInputStream(CS1.getInputStream()); DataInputStream CIS2 = new DataInputStream(CS2.getInputStream()); PrintStream PS1 = new PrintStream(S1.getOutputStream()); PrintStream PS2 = new PrintStream(CS1.getOutputStream()); PrintStream PS3 = new PrintStream(CS2.getOutputStream()); String k1=CIS1.readLine(); String k2=CIS2.readLine(); PS1.println(k1+k2); String res=DIS1.readLine(); System.out.println(res); PS2.println(res);

PS3.println(res); } } SERVER 2: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class server2 { public static void main(String a[]) throws Exception { String ans=""; ServerSocket SS2 = new ServerSocket(7777); System.out.println("Waiting for Server1..."); Socket S2 =SS2.accept(); System.out.println("Server1 Connected"); DataInputStream DIS2 = new DataInputStream(S2.getInputStream()); PrintStream PS2 = new PrintStream(S2.getOutputStream()); String inp = DIS2.readLine(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:record","",""); Statement stat = con.createStatement(); String query = "Select name from record where regno = '"+inp+"'"; ResultSet rs = stat.executeQuery(query); while(rs.next()) ans =rs.getString(1); if(ans!="") PS2.println("Record for key "+inp+" is "+ans); else PS2.println("Record Not Found"); }

}

9: ( cell no from user and validates) CLIENT:
import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class client { public static void main(String a[]) throws Exception { Socket S1=new Socket(InetAddress.getLocalHost(),7000); System.out.println("Connected with Server1"); DataInputStream DIS = new DataInputStream(System.in); DataInputStream DS1 = new DataInputStream(S1.getInputStream()); PrintStream PS = new PrintStream(S1.getOutputStream()); System.out.print("Enter Cell no : "); String k1=DIS.readLine(); PS.println(k1.trim()); String res = DS1.readLine(); StringTokenizer Stt =new StringTokenizer(res,"#"); while(Stt.hasMoreTokens()) { String temp=Stt.nextToken(); System.out.println(temp); }

} } SERVER 1: import java.net.*; import java.io.*;

import java.sql.*; import java.util.*; import java.lang.*; class server1 { public static void main(String a[]) throws Exception { String ans="",total = ""; boolean chk=true; System.out.println("Waiting for Client1..."); ServerSocket SS1 = new ServerSocket(7000); Socket S1 =SS1.accept(); System.out.println("client Connected"); DataInputStream DIS1 = new DataInputStream(S1.getInputStream()); PrintStream PS1 = new PrintStream(S1.getOutputStream()); String cellno = DIS1.readLine(); String firstpart = cellno.substring(0,5); String secondpart = cellno.substring(5,10); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:cellfull","",""); Statement stat = con.createStatement(); String query = "Select company from cellcomp where compid = '"+firstpart+"'"; ResultSet rs = stat.executeQuery(query); while(rs.next()) ans =rs.getString(1); if(ans!="") total=" Connection is " +ans+"#"; else chk= false; rs.close(); query = "Select uname from cellhalf where cellcode = '"+secondpart+"'"; rs = stat.executeQuery(query); while(rs.next())

ans =rs.getString(1); if(ans!=""){ total=total+" User name is " +ans+"#"; System.out.print(total);} else chk = false; rs.close(); stat.close(); con.close(); if (!chk) { Socket S2=new Socket(InetAddress.getLocalHost(),7777); System.out.println("Connected with Server2"); DataInputStream DS2 = new DataInputStream(S2.getInputStream()); PrintStream PS2 = new PrintStream(S2.getOutputStream()); PS2.println(cellno); PS1.println(DS2.readLine()); } PS1.println(total); } } SERVER 2: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class server2 { public static void main(String a[]) throws Exception { String ans=""; System.out.println("Waiting for Server1..."); ServerSocket SS2 = new ServerSocket(7777);

Socket S2 =SS2.accept(); System.out.println("Server1 Connected"); DataInputStream DIS2 = new DataInputStream(S2.getInputStream()); PrintStream PS2 = new PrintStream(S2.getOutputStream()); String inp = DIS2.readLine(); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:cellfull","",""); Statement stat = con.createStatement(); String query = "Select user from celltable where cellno = '"+inp+"'"; ResultSet rs = stat.executeQuery(query); while(rs.next()) ans =rs.getString(1); if(ans!=""){ PS2.println("Username for "+inp+" is "+ans); } else PS2.println("Record Not Found"); rs.close(); stat.close(); con.close(); } }

10. (bank credit card no) CLIENT:
import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class Client { public static void main(String a[]) throws Exception { String arr[] =new String[3];int i=0; Socket S1=new Socket(InetAddress.getLocalHost(),7000);

System.out.println("Connected with Server1"); DataInputStream DIS = new DataInputStream(System.in); DataInputStream DS1 = new DataInputStream(S1.getInputStream()); PrintStream PS = new PrintStream(S1.getOutputStream()); System.out.print("Enter credit card no : "); String k1=DIS.readLine(); PS.println(k1.trim()); String res = DS1.readLine(); if(res.equals("Record Not Found")) System.out.println("Record Not Found"); else{ StringTokenizer Stt =new StringTokenizer(res,"#"); while(Stt.hasMoreTokens()) { String temp=Stt.nextToken(); arr[i]=temp; i++; } System.out.println("Customer name is "+arr[0]); System.out.println("Balance is " +arr[1]); } } } SERVER 1: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; import java.lang.*; class Server1 { public static void main(String a[]) throws Exception { char ch;String encrypt="";

System.out.println("Waiting for Client..."); ServerSocket SS1 = new ServerSocket(7000); Socket S1 =SS1.accept(); System.out.println("client Connected"); DataInputStream DIS1 = new DataInputStream(S1.getInputStream()); PrintStream PS1 = new PrintStream(S1.getOutputStream()); String creditno = DIS1.readLine(); for(int i=0;i<creditno.length();i++) { ch=creditno.charAt(i); encrypt+=(char)((int)ch+12); } Socket S2=new Socket(InetAddress.getLocalHost(),7777); System.out.println("Connected with Server2"); DataInputStream DS2 = new DataInputStream(S2.getInputStream()); PrintStream PS2 = new PrintStream(S2.getOutputStream()); PS2.println(encrypt); String res =DS2.readLine(); if(res.equals("Record Not Found")) { PS1.println("Record Not Found"); } else{ String decrypt=""; StringTokenizer Stt =new StringTokenizer(res,"+*="); while(Stt.hasMoreTokens()) { String temp=Stt.nextToken(); for(int i=0;i<temp.length();i++) { ch=temp.charAt(i); decrypt+=(char)((int)ch-4); }

decrypt+="#"; } System.out.print(decrypt); PS1.println(decrypt); } } } SERVER 2: import java.net.*; import java.io.*; import java.sql.*; import java.util.*; class Server2 { public static void main(String a[]) throws Exception { String ans1="",ans2="";char ch;String encrypt="",decrypt=""; System.out.println("Waiting for Server1..."); ServerSocket SS2 = new ServerSocket(7777); Socket S2 =SS2.accept(); System.out.println("Server1 Connected"); DataInputStream DIS2 = new DataInputStream(S2.getInputStream()); PrintStream PS2 = new PrintStream(S2.getOutputStream()); String inp = DIS2.readLine(); for(int i=0;i<inp.length();i++) { ch=inp.charAt(i); decrypt+=(char)((int)ch-12); } Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("Jdbc:Odbc:bankcredit","","");

Statement stat = con.createStatement(); String query = "Select Custname,balance from credittable where creditno = '"+decrypt+"'"; ResultSet rs = stat.executeQuery(query); while(rs.next()){ ans1 =rs.getString(1); ans2=rs.getString(2); } if(ans1!=""){ for(int i=0;i<ans1.length();i++) { ch=ans1.charAt(i); encrypt+=(char)((int)ch+4); } encrypt+="+*="; if(ans2!=""){ for(int i=0;i<ans2.length();i++) { ch=ans2.charAt(i); encrypt+=(char)((int)ch+4); } } PS2.println(encrypt); } else PS2.println("Record Not Found"); rs.close(); stat.close(); con.close(); } }

11.(reads a bank acct no and validate) CLIENT:

import java.io.*; import java.net.*; class cli { public static void main(String ar[]) { try { Socket s=new Socket("localhost",7777); DataInputStream in=new DataInputStream(System.in); PrintStream p=new PrintStream(s.getOutputStream()); DataInputStream din=new DataInputStream(s.getInputStream()); String str,str1; System.out.println("Enter 6 digit Account Number"); str=in.readLine(); p.println(str); System.out.println("====Server Reply====="); str1=din.readLine(); System.out.println(str1); } catch(Exception e) { System.out.println(e); } } } SERVER 1: import java.io.*; import java.net.*; import java.sql.*; class ser1 { public static void main(String ar[])

{ try { ServerSocket ss=new ServerSocket(7777); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); String str,str1="",str2; str=din.readLine(); System.out.println(str); int flag=0; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:db1"); Statement smt=con.createStatement(); ResultSet rs=smt.executeQuery("select * from lab71 where AccuntNo="+str+""); while(rs.next()) { str1=rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3); flag=1; } if(flag==1) { System.out.println("Record Found"); p.println(str1); } else { Socket s1=new Socket("localhost",7771); PrintStream p1=new PrintStream(s1.getOutputStream()); DataInputStream din1=new DataInputStream(s1.getInputStream()); System.out.println("Record is Not Available in server1.So Account Number sending to server2");

p1.println(str); str2=din1.readLine(); System.out.println(str2); p.println(str2); } } catch(Exception e) { System.out.println(e); } } } SERVER 2: import java.io.*; import java.net.*; import java.sql.*; class ser2 { public static void main(String ar[]) { try { ServerSocket ss=new ServerSocket(7771); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); String str,str1="",str2=""; str=din.readLine(); System.out.println(str); int flag=0; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:db2"); Statement smt=con.createStatement();

ResultSet rs=smt.executeQuery("select * from lab72 where AccuntNo="+str+""); while(rs.next()) { str1=rs.getString(1)+" "+rs.getString(2)+" "+rs.getString(3); flag=1; } if(flag==1) { System.out.println("Record Found In server2"); p.println(str1); } else { System.out.println("Record Also not Found in Server2"); p.println("Record Also not Found in Server2"); } } catch(Exception e) { System.out.println(e); } } }

12.Two’s complement: Client:
import java.io.*; import java.net.*;

class client { public static void main(String arr[]) throws Exception { Socket sc=new Socket("localhost",1090); PrintStream ps=new PrintStream(sc.getOutputStream()); String str=" ", str1=" "; int i=0; System.out.print("\n******Client *****\n"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("\nEnter any Number as value of A : "); str=br.readLine(); ps.println(str); System.out.println("\nA is send to Server1"); Socket sc1=new Socket("localhost",1055); PrintStream ps1=new PrintStream(sc1.getOutputStream()); System.out.print("\nEnter any Number as value of B : "); str1=br.readLine(); ps1.println(str1); System.out.println("\nB is send to Server2"); System.out.println("\nValue of A + B 2's Complement is :"); BufferedReader br1=new BufferedReader(new InputStreamReader(sc1.getInputStream())); str=br1.readLine();

System.out.print(str); } } Server1: import java.io.*; import java.net.*; class server1 { public int twoComp( int i) { String by = Integer.toBinaryString(i); int l=by.length(); l=l-1; String s=""; while(l>=0) { if (by.charAt(l)=='0') s=s+"1"; else if (by.charAt(l) == '1') s=s+"0"; l--; } s = new StringBuffer(s).reverse().toString(); System.out.println("\n" + i +" Binary is " + by + " 1's complement is "+s);

l=s.length(); l=l-1; String c="1"; String rs=""; while(l>=0) { if(s.charAt(l)=='0' && c.equals("0")) { rs=rs+"0"; c="0"; } else if(s.charAt(l)=='0'&& c.equals("1") ) { rs=rs+"1"; c="0"; } else if(s.charAt(l)=='1' && c.equals("0") ) { rs=rs+"1"; c="0"; } else if(s.charAt(l)=='1' && c.equals("1") ) { rs=rs+"0"; c="1";

} l--; } rs = new StringBuffer(rs).reverse().toString(); String no=""; l = rs.length(); int j = 0; for (int k = 0; k < l; k++) { no=""+rs.charAt(k); j = j + Integer.parseInt(no) * pow(2, (rs.length() - k-1)); } System.out.println("\nAnd 2's comp is "+ rs + " And its Decimal is " + j ); return j; } public int pow(int a,int b) { int res=1; while(b>0) { res=res*2; b--; } return res;

} public static void main(String arr[]) throws Exception { ServerSocket ss=new ServerSocket(1090); Socket sc=ss.accept(); PrintStream ps=new PrintStream(sc.getOutputStream()); String str=" "; server1 obj=new server1(); System.out.print("\n******Server--1*****\n"); BufferedReader br=new BufferedReader(new InputStreamReader(sc.getInputStream())); str=br.readLine(); int i=Integer.parseInt(str); System.out.println("A data received from Client is : " + i); int j=obj.twoComp( i); ServerSocket ss1=new ServerSocket(1056); Socket sc1=ss1.accept(); PrintStream ps1=new PrintStream(sc1.getOutputStream()); ps1.println(j); System.out.println("\nA 2's complement is sent to server 2") ; } } Server2: import java.io.*; import java.net.*;

class server2 { public static void main(String arr[]) throws Exception { ServerSocket ss=new ServerSocket(1055); Socket sc=ss.accept(); PrintStream ps=new PrintStream(sc.getOutputStream()); String str=" ",str2=" ",str3=""; sint i=0,k=0,l=0; long j=0; System.out.println("*******Server--2********\n"); BufferedReader br=new BufferedReader(new InputStreamReader(sc.getInputStream())); str=br.readLine(); i=Integer.parseInt(str); System.out.println("\nB received from Client is =" + i); Socket sc1=new Socket("localhost",1056); BufferedReader br1=new BufferedReader(new InputStreamReader(sc1.getInputStream())); str2=br1.readLine(); k=Integer.parseInt(str2); System.out.println("\nA 2's complement received from server is=" + k); l=i+k; System.out.println("\nAs 2's + B result send to Client "+l); ps.println(l);

} } 13.Simple Arithmetic Expression: Client: import java.io.*; import java.net.*; class cli { public static void main(String ar[]) { try { Socket s=new Socket("localhost",1111); DataInputStream in=new DataInputStream(System.in); PrintStream p=new PrintStream(s.getOutputStream()); String str; System.out.println("Enter Expression"); str=in.readLine(); p.println(str); } catch(Exception e) {} } } Server1: import java.io.*; import java.net.*;

imposrt java.util.*; class ser1 { public static void main(String ar[]) { try { ServerSocket ss=new ServerSocket(1111); Socket s=ss.accept(); StringTokenizer t1,t2,t3,t4; String str; Socket s1=new Socket("localhost",1112); Socket s2=new Socket("localhost",1113); PrintStream p1=new PrintStream(s1.getOutputStream()); PrintStream p2=new PrintStream(s2.getOutputStream()); DataInputStream din=new DataInputStream(s.getInputStream()); str=din.readLine(); t1=new StringTokenizer(str,"+"); t2=new StringTokenizer(str,"-"); t3=new StringTokenizer(str,"*"); t4=new StringTokenizer(str,"/"); if((t1.countTokens()>1)||(t2.countTokens()>1)) { System.out.println("Pluse or Minus"); p1.println(str);

} if((t3.countTokens()>1)||(t4.countTokens()>1)) { System.out.println("Multiply or Division"); p2.println(str); }} csatch(Exception e) {}}} Server2: import java.io.*; import java.net.*; import java.util.*; class ser2 { public static void main(String ar[]) {try { ServerSocket ss=new ServerSocket(1112); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); String str,str1,str2; StringTokenizer t1,t2; int i,j,k; str=din.readLine(); t1=new StringTokenizer(str,"+");

t2=new StringTokenizer(str,"-"); if(t1.countTokens()>1) { System.out.println("Pluse"); str1=t1.nextToken(); i=Integer.parseInt(str1); System.out.println("Op1 "+i); str2=t1.nextToken(); j=Integer.parseInt(str2); System.out.println("Op2 "+j); k=i+j; System.out.println("Result is "+k); } if(t2.countTokens()>1) { System.out.println("Minus"); str1=t2.nextToken(); i=Integer.parseInt(str1); System.out.println("Op1"+i); str2=t2.nextToken(); j=Integer.parseInt(str2); System.out.println("Op2"+j); k=i-j; System.out.println("Result is "+k); }}

catch(Exception e) {} }} Server3: import java.io.*; import java.util.*; import java.net.*; class ser3 {public static void main(String ar[]) {try{ ServerSocket ss=new ServerSocket(1113); Socket s=ss.accept(); DataInputStream din=new DataInputStream(s.getInputStream()); String str,str1,str2; StringTokenizer t1,t2; int i,j,k; str=din.readLine(); t1=new StringTokenizer(str,"*"); t2=new StringTokenizer(str,"/"); if(t1.countTokens()>1) { System.out.println("Multiply"); str1=t1.nextToken(); i=Integer.parseInt(str1); System.out.println("Op1: "+i); str2=t1.nextToken();

j=Integer.parseInt(str2); System.out.println("Op2 : "+j); k=i*j; System.out.println("Result is :"+k); }if(t2.countTokens()>1) { System.out.println("Divide"); str1=t2.nextToken(); i=Integer.parseInt(str1); System.out.println("Op1 :"+i); str2=t2.nextToken(); j=Integer.parseInt(str2); System.out.println("Op2 :"+j); k=i/j; System.out.println("Result is "+k); }} catch(Exception e) {} } }