Thursday, January 13, 2011

Did Your Cling or Table Fan rotates slowly than before? Here is the solution. ඔබගේ වීදුලි පංකාව කැරකෙන්නේ පෙරට වඩා සෙමින්ද?

This is the circuit diagram of a simple fan. The fan symbol indicates the Armature Coil Windings. Rheostat indicates the speed controlling buttons.
There are two major reasons for slowness of the fan.
  1. Capacitor is burned.
  2. There is a short circuit in the winding or Some part of the winding burned.
To find out the reason, You have to do a small trick, Find the consensor of your fan, Cu the connecting wires to that condenser. then switch on the the fan and rotate fan blades with your hands. If fan starts to run in the early speed the fault occurred due to bad condenser. So you have to replace a one.


If the speed is same as before(Slow) most probably there can be a leek in the windings. So you have to replace total armature part of the fan. If it occurs in a cycling fan you have to forget of the fan. If you try to repair the winding of the fan it takes money than a new man. So replacing armature is better. You can buy it near 800 rupees. Hear is a picture of the motor.




Monday, January 3, 2011

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.