Monday, January 3, 2011

Pin It


Get Gadget

Client Server Application With Java

For our Universities 3rd Semester subject, Object Oriented Software Development Project, I tried Client Server Applications for the first time. So at that time I encountered lot of troubles. So i think that i have to post them to help the people who like to start from the beginning of Java client server applications.

So this is a guide to build many Clients- One Server type applications in Java.

1st Step - Creating the Server Side

First of all we have to create the server port, which is watched by the server to handle clients. Hear is the way to do it.

try {
ServerSocket serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
System.exit(-1);
}

After initiating the port we have to start watching the port. For that purpose i use a While loop for ever. that loop again and again until a client try to get in to the server.
In my Software System, Client request for help from server to accomplish three tasks,
  1. Authenticate User Name And Password
  2. Get User Details
  3. Update User Details
  4. Get data from server(Hear Adjacency List of a Graph)
Hear Main Sever Handles Only One Thing in the Clients Request. It is the task of Login Authentication. After that server starts a client handler Thread. So that thread is responsible for providing services to th client after words. Following code shows how the Main server authenticate user name and password and start a client handler thread.

while (true){
try{

ser=serverSocket.accept();
br = new BufferedReader(new InputStreamReader(ser.getInputStream()));
message=br.readLine();
if(message.equals("logMeIn")){
System.out.println(message);

oos=new ObjectOutputStream(ser.getOutputStream());
pkt=new Packet();
String UserName=br.readLine();
String Password=br.readLine();
ResultSet rs = stmt.executeQuery("SELECT * FROM users WHERE
userName='"+UserName+"'");
if(rs.next()){

if(Password.equals(rs.getString("password"))){
pkt.verified=true;
oos.writeObject(pkt);
oos.flush();
ch=new Client_Handler(ser,oos,br,UserName);//Start Client handler Thread

ch.start();
}
else{
pkt.verified=false;
oos.writeObject(pkt);
oos.flush();

oos.close();
br.close();
}
}
else{

pkt.verified=false;
oos.writeObject(pkt);
oos.flush();
oos.close();
br.close();
}

}

}
catch(Exception e){
System.out.println("Error");

}
}

Hear is the Code for Client Handler Thread; It handles all the Request after the authentication proceeder happens.

import datapacket.Packet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client_Handler extends Thread {

private Socket sk;
private String url;
private Connection con;
private Statement stmt;
private BufferedReader br;
private ObjectOutputStream oos;
private ObjectOutputStream ois;
private Packet pkt;
private String uName;
public String fName, tp, email, ad;

public Client_Handler(Socket sk, ObjectOutputStream oos, BufferedReader br, String uName) {
this.sk = sk;
this.oos = oos;
this.br = br;
this.uName = uName;
url = "jdbc:mysql://localhost:3306/Map";
}

public void run() {
String message = null;
while (true) {
try {
try {
message = br.readLine();
System.out.println(message);
} catch (IOException ex) {
Logger.getLogger(Client_Handler.class.getName()).log(Level.SEVERE, null, ex);
}
if (message.equals("giveMeMap")) {
} else if (message.equals("giveMeUser")) {
} else if (message.equals("up")) {
}
} catch (Exception ex) {
}
}
}
}

2nd Step - Writing the Client side

Now we know how the Server will respond to requests and how to ask from server to get responses. So we can easily write the client side of the Application now.

No comments:

Post a Comment