Problem Statement:
It is decided to keep information about V semester MCA students in a database at a server machine. Store the Roll No, Name, Marks obtained in each subjects. Let the client request information from server using Roll No. The server access the database and get the information and send the client machine.
Input and Output Requirements:
The client accepts Rollno through the keyboard and sends to the server. The server reads the student 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)
a. Read Rollno from the input stream.
b. Return the data from the database.
c. Send the retrieved information to the client.
d. 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 Rollno and send it to the server.
Display information send by the server.
Close the connection.
End
Program:
//Client: import java.io.*; import java.net.*; import java.util.Scanner; public class Client { ServerSocket ser; Socket so; ObjectInputStream in; ObjectOutputStream out; Scanner IDinput; String ch,s1,s2,s3,message,strrollno,name,sub1,sub2,sub3; int rollno; void ClientRun() { try { so = new Socket("localhost",2016); System.out.println("Connection got at port 2016"); try { out = new ObjectOutputStream(so.getOutputStream()); out.flush(); in = new ObjectInputStream(so.getInputStream()); sentmessage("i am a client needed to know student details"); } catch (IOException e) { System.out.println("IO error"+e); } try { message =(String) in.readObject(); System.out.println(" "+message); do { IDinput = new Scanner(System.in); System.out.println("Enter the roll number of student:"); rollno = IDinput.nextInt(); strrollno = Integer.toString(rollno); sentmessage(strrollno); name = (String) in.readObject(); s1 = (String) in.readObject(); s2 = (String) in.readObject(); s3 = (String) in.readObject(); System.out.println("StudentID Name Subject1 Subject2 Subject3"); System.out.println(" "+strrollno+" "+name+" "+s1+" "+s2+" "+s3); System.out.println("Do u want to check other student Details(y/n)...."); ch = IDinput.next(); }while(ch != "n"); sentmessage("Thank you server"); } catch (IOException e) { System.out.println("IO error"+e); } catch(ClassNotFoundException e) { System.out.println("class not found error"+e); } } catch (UnknownHostException e) { System.out.println("Unknown host error error"+e); } catch (IOException e) { System.out.println("IO error"+e); } finally { try { so.close(); in.close(); out.close(); } catch(IOException io) { System.out.println("IO error"+io); } } } void sentmessage(String msg) { try { out.writeObject(msg); out.flush(); } catch (IOException e) { System.out.println("IO error"+e); } } public static void main(String args[]) { Client c = new Client(); c.ClientRun(); } } |
//Server: import java.net.*; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.io.*; public class Server implements Runnable { private Socket client; ObjectInputStream inStream; ObjectOutputStream outStream; public Server(Socket sock) { Thread thread ; client=sock; thread = new Thread(this); thread.start(); } public static void main (String args[]) { ServerSocket listenSocket =null; System.out.println("Server Started"); try { listenSocket = new ServerSocket(2016); } catch (IOException e) { System.out.println("Unable to create server socket"); } while (true) { Socket sock; try { sock= listenSocket.accept(); new Server(sock); } catch (IOException e) { System.out.println("Unable to establish connection"+e); } } } public void run() { String data = null; Statement stm; Connection Connet; ResultSet result; String str; String rolno,data1; int introllno; try { outStream = new ObjectOutputStream(client.getOutputStream()); outStream.flush(); inStream = new ObjectInputStream(client.getInputStream()); System.out.println("Connected to client.." +client.getInetAddress().getAddress()); try { data=(String) inStream.readObject(); System.out.println(" "+data); sentmessage("You are connected to Server"); rolno = (String) inStream.readObject(); introllno = Integer.valueOf(rolno); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connet = DriverManager.getConnection ("jdbc:odbc:DS","system","manager"); stm = Connet.createStatement(); str = "select * from student where roll_no ="+introllno; result = stm.executeQuery(str); while(result.next()) { sentmessage(result.getString("name")); sentmessage(result.getString("Sub1")); sentmessage(result.getString("Sub2")); sentmessage(result.getString("Sub3")); } data1=(String) inStream.readObject(); System.out.println("asas "+data1); } catch (SQLException e) { System.out.println("sql error"+e); } } catch (ClassNotFoundException e) { System.out.println("class no found error"+e); } } catch (IOException e) { System.out.println("IO error"+e); } finally { try { outStream.close(); } catch (IOException e) { System.out.println("IO error"+e); } } } void sentmessage(String msg) { try { outStream.writeObject(msg); outStream.flush(); } catch (IOException e) { System.out.println("IO error"+e); } } } |
Output:
Server Side:
Server Started
Connected to client…[B@190d11
i am a client needed to know student details
Client Side:
Connection got at port 2016
You are connected to Server
Enter the roll number of student: 101
StudentID Name Subject1 Subject2 Subject3
101 sandeep 45 87 99
Do u want to check other student Details(y/n)….n
Leave a Reply