/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package server;
/**
*
* @author 007
*/
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Random.*;
import java.net.*;
import java.lang.String.*;
import java.io.InputStream;
import java.lang.Object.*;
import java.net.ServerSocket.*;
public class aptserver implements Runnable{
private static String password;
private Socket aptsocket=null;
int c=0;
static ResultSet res=null;
static Connection conn=null;
static Statement stat=null;
private static String servoff=null;
private static int servoff_port=0;
//private static int laserport;
//private static int adaptport;
public static void main(String[] ar) throws Exception
{
try
{
int port=14007; //the only constant port
ServerSocket aptsrv= new ServerSocket(port);
int count=0;
while(true)
{
Socket aptsocket=aptsrv.accept();
Runnable runnable=new aptserver(aptsocket,++count);
Thread thread=new Thread(runnable);
thread.start();
}
}//try
catch(Exception e) {
System.out.println("The error is in the main:"+e.getMessage());
}
}//main
aptserver(Socket s,int c)
{
this.aptsocket=s;
this.c=c;
}
public void run()
{
try
{
System.out.println("Got a client (client num : "+c+")");
System.out.println();
InputStream sin = aptsocket.getInputStream();
OutputStream sout = aptsocket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String username="";
String passrec="";
String ip="";
String mac="";
InetAddress myself = InetAddress.getLocalHost ();
String serverip=myself.toString();
serverip=serverip.substring(4);
int flag=0; int port_array[]= new int[10];
while(true)
{
username = in.readUTF();
System.out.println("recving username:"+username);
passrec=in.readUTF();
System.out.println("srecvng password:"+passrec);
ip=in.readUTF();
System.out.println("recving ip:"+ip);
mac=in.readUTF();
System.out.println("recvieg mac:"+mac);
port_array=getports();
flag= auth(username,passrec,ip,mac,port_array[1]);
if(flag==1)
{
out.writeUTF("Success");
System.out.println("laserport got from db:"+port_array[0]);
System.out.println("adport got from db:"+port_array[1]);
System.out.println("serverip got from db:"+serverip);
System.out.println("servoff got from db:"+servoff);
System.out.println("servoff_port got from db:"+servoff_port);
out.flush();
out.writeInt(port_array[0]);
out.flush();
out.writeInt(port_array[1]);
out.flush();
out.writeUTF(serverip);
out.flush();
out.writeUTF(servoff);
out.flush();
out.writeInt(servoff_port);
out.flush();
String[] serv=null;
serv[0]=String.valueOf(port_array[0]);//seending the port # of client who has the server socket
System.out.println("Entering into db");
enterintodb(port_array[1],servoff,servoff_port,ip,mac);
//serv[1]=servoff;
//serv[2]=String.valueOf(servoff_port);
server.lookupserver.main(serv);
}
else
{
out.writeUTF("Mismatch");
out.flush();
}
break;
//Thread.sleep(4000);
}//while
}//try
catch(Exception x)
{
System.out.println("the error is "+x);
}
}
private static int auth(String usrn, String pwd, String ipaddr, String macaddr, int adaptport) throws ClassNotFoundException, SQLException
{
//usrn="raf123";ipaddr="169.254.175.83";macaddr="00-0F-B8-UJ-9K-D3";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//DONOT CHANGE THIS
conn = DriverManager.getConnection("jdbc:odbc:MS Access Database");//msk is the DSN Name
stat = conn.createStatement();
res = stat.executeQuery("Select service,service_port from valid_clients where ip='"+ipaddr+"' and mac='"+macaddr+"'"); // Customer is the table name
res.next(); // initially points to first data
try{
//System.out.println("valid user"); //displays first attribute
servoff=res.getString(1); //displays second attribute
servoff_port=res.getInt(2);
}
catch(SQLException e){
System.out.println("Intruder alert in here");
stat.execute("insert into blocked values('"+ipaddr+"','"+macaddr+"')");
//intruder alert..work her
}
char a,c,b,d,e,f;
String g;
a=uname(usrn);
b=ipnum(ipaddr);
c=ipalpha(ipaddr);
d=macnum(macaddr,6,7,1);
e=macnum(macaddr,9,10,2);
f=macnum(macaddr,15,16,2);
g=fromdb(usrn,ipaddr);
password = String.valueOf(a)+String.valueOf(b) +String.valueOf(c)+String.valueOf(d)+String.valueOf(e)+String.valueOf(f)+String.valueOf(g);
//System.out.println("the calc password is:"+password);
if(password!=null && pwd != null)
{
if(password.equals(pwd))
{
try{
System.out.println("inserting the values in database");
stat.execute("insert into clients_online values('" + ipaddr + "','" + macaddr + "','" + servoff_port + "','1','"+servoff + "','"+adaptport+"','"+usrn+"')");
//stat.execute("insert into blocked values('168.ad','aa-xx')");
}
catch (SQLException ex) {
System.out.println("Unable to connect to the database in auth"+ex.getMessage());
}
return 1;
}
else
return 0;
}
return 0;
}
private static String fromdb(String name, String ip)
{
ResultSet resdb;
Connection conndb;
Statement statdb;
int j=0;char a,b;String keys="";
for(int i=0;i<ip.length();i++)
{
b=ip.charAt(i);
if(b=='.')
{
continue;
}
j+=b-'0';
//System.out.println(j);
}
if(j>10)
{
j=j%10;
}
try
{
int flag=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conndb = DriverManager.getConnection("jdbc:odbc:MS Access Database");
statdb = conndb.createStatement();
resdb = statdb.executeQuery("SELECT * FROM uname ");
while(resdb.next())
{
if(resdb.getString(1).equals(name))
{
keys=resdb.getString(2);
flag=1;
break;
}
}//while
if(flag==0)
{
System.out.println("No rows found");
}
}//try
catch(Exception e)
{
System.out.println("Error in 1st(no username in the table) " +e);
}
a=keys.charAt(0);
b=keys.charAt(1);
int c=a-'A'+1;
int d=b-'A'+1;
c+=j;d+=j;
while(c>26)
{
c=c%26;
}
while(d>26)
{
d=d%26;
}
c+=64;
d+=64;
a=(char) c;
b=(char) d;
keys=String.valueOf(a)+String.valueOf(b);
return (keys);
}
private static char ipalpha(String ip)
{
int j=0,count=0;char b,d;
for(int i=ip.length()-1;i>0;i--)
{
b=ip.charAt(i);
if(count==2)
{
break;
}
if(b=='.')
{
count++;
continue;
}
j+=b-'0'; //to get the number out of the number itself
//System.out.println(j);
}
j=j%10;
j=j+48; //getting back the number
d=(char) j;
return d;
}
private static char ipnum(String ip)
{
int j=0,count=0;char b;
for(int i=0;i<ip.length();i++)
{
b=ip.charAt(i);
if(count==2)
{
break;
}
if(b=='.')
{
count++;
continue;
}
j+=b-'0';
//System.out.println(j);
}//for
while(j>10)
{
j=j%10;
}
b=(char) (j+48);
return (b);
}
private static char macnum(String mac, int first, int sec, int flag)
{
char temp;int j=0;char d;
if(Character.isDigit(temp=mac.charAt(first)))
{
j+=temp-'0';
// b=(char) (j+64);
}
else
{
j+=temp-'A'+1;
}
if(Character.isDigit(temp=mac.charAt(sec)))
{
j+=temp-'0';
}
else
{
j+=temp-'A'+1;
}
if(flag==1)
{
while(j>26)
{
j=j%26;
}
d=(char) (j+64);
return d;
}
else
{
j=j%10;
j=j+48; //getting back the number
d=(char) j;
return d;
}
}
private static char uname(String unam)
{
int j=0;char b;
for(int i=unam.length()-1;i>2;i--)
{
b=unam.charAt(i);
j+=b-'0';
}
b=(char) (j+64);
//System.out.println("Uname last three characters sum upto:"+j);
//System.out.println(b);
return (b);
}
private void enterintodb(int adport, String servoff, int servoff_port,String ip,String mac) {
Connection connlu;
Statement statlu;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connlu = DriverManager.getConnection("jdbc:odbc:MS Access Database");
statlu = connlu.createStatement();
statlu.execute("update clients_online set servoff = '"+servoff+"' where ip='"+ip+"' and mac='"+mac+"'");
statlu.execute("update clients_online set servport = '"+servoff_port+"' where ip='"+ip+"' and mac='"+mac+"'");
statlu.execute("update clients_online set adaptport = '"+adport+"' where ip='"+ip+"' and mac='"+mac+"'");
}
catch(Exception x)
{
System.out.println("unable to connect to the database in enterintodb");
System.out.println(x);
}
}
private int[] getports()
{
ResultSet resad;
Connection connad;
Statement statad;
int array[] = new int[10];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connad = DriverManager.getConnection("jdbc:odbc:MS Access Database");
statad = connad.createStatement();
resad = statad.executeQuery("SELECT * FROM port where index="+c+"");
resad.next();
array[1]=resad.getInt(3);//adapt port
array[0]=resad.getInt(2);//lookup port
System.out.println("adaptport from db:"+array[1]);
System.out.println("laserport from db:"+array[0]);
}
catch(Exception x)
{
System.out.println(x);
System.out.println("Unable to connect to database in laport");
}
return array;
}
}
the entire thing sends the info to the client, but at last just when the lookupserver() pgm
has to execute...it catches a null pointer exception error and stops...the program control doesnt goto to the lookupserver();
its multi thread program which works on a socket level...
one port...but different socket each time a new client enters
plz help me...the final proj submission date is soon approaching