Problem Statement:
Southern Railway stores information about each ticket issued by the department in a table called PnrStatus. Assume the table is available at a server machine. Write both
Client and server programs to retrieve information by providing the PNRNo. It is
required to output the following information at client side.
i) PNRNo ii) From station iii) Destination iv) Date of journey
v) Customer name. vi) Train No.
Input and Output Requirements:
The client accepts PNRNo through the keyboard and sends to the server. The server reads the train information from the database and sends back to the client.
Algorithm: server side
Begin
Create a connection object.
Create an object of server socket class and listen for incoming client connection in a specified port.
While (true)
e. Read PNRNo from the input stream.
f. Return the data from the database.
g. Send the retrieved information to the client.
h. Close the server and client socket.
End
Algorithm: client side
Begin
Establish a connection to the server program. Use IP address and port number of the server.
Create input and output streams for the established connection.
Read PNRNo and send it to the server.
Display information send by the server.
Close the connection.
End
Program:
Server side
import java.net.*; import java.io.*; import java.sql.*; class trainserver { public static void main(String args[]) { String Query; ResultSet rs = null; String msg; int pnr = 0,trainnum=0; String from = new String(); String to = new String(); String date = new String(); String name = new String(); try { ServerSocket ser=new ServerSocket(8000); System.out.println("Server Started..."); Socket soc=null; soc=ser.accept(); System.out.println("Received Connection: "+soc.getInetAddress().getHostAddress()); DataOutputStream out=new DataOutputStream(soc.getOutputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(soc.getInputStream())); try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn=DriverManager.getConnection("jdbc:odbc:mydb","scott","tiger"); Statement stmt=conn.createStatement(); msg = in.readLine(); pnr = Integer.parseInt(msg); Query = "Select * from train where pnrno = "+pnr; rs = stmt.executeQuery(Query); if(rs.next()) { pnr = Integer.parseInt(rs.getString(1)); from = rs.getString(2); to = rs.getString(3); date = rs.getString(4); name = rs.getString(5); trainnum=Integer.parseInt(rs.getString(6)); } else { System.out.println("invalid PNRNO"); } } catch(SQLException e) { System.out.println("Error Caught: "+e); } out.writeBytes("".valueOf(pnr)); out.write(10); out.writeBytes(from); out.write(10); out.writeBytes(to); out.write(10); out.writeBytes(date); out.write(10); out.writeBytes(name); out.write(10); out.writeBytes("".valueOf(trainnum)); out.write(10); soc.close(); } catch(Exception e) { System.out.println("Error Caught: "+e); } } } |
//Client side
import java.io.*; import java.net.*; class traindbclient { public static void main(String args[]) { Socket objclient= null; BufferedReader br = null,in = null; DataOutputStream out = null; int pnr = 0; try { objclient = new Socket("Localhost",8000); in = new BufferedReader(new InputStreamReader(objclient.getInputStream())); br = new BufferedReader(new InputStreamReader(System.in)); out = new DataOutputStream(objclient.getOutputStream()); System.out.println("Enter the PNR NO: "); pnr = Integer.parseInt(br.readLine()); out.flush(); out.writeBytes("".valueOf(pnr)); out.write(10); System.out.println("PNRNo: "+in.readLine()); System.out.println("SOURCE : "+in.readLine()); System.out.println("DESTINATION: "+in.readLine()); System.out.println("JOURNEY DATE: "+in.readLine()); System.out.println("CUSTOMER NAME: "+in.readLine()); System.out.println("TRAIN NO: "+in.readLine()); objclient.close(); } catch(Exception e) { System.out.println("Error Caught: "+e); } } } |
Output:
Server side:
Server Started…
Received Connection: 127.0.0.1
Client Side:
Enter the PNR NO: 1003
PNRNo: 1003
SOURCE: Mumbai
DESTINATION: Palaghat
JOURNEY DATE: 2009-11-03 00:00:00
CUSTOMER NAME: Cyril
TRAIN NO: 6834