/*
* 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

why don't you post the exception and stack trace, and post the code inside code tags

why don't you post the exception and stack trace, and post the code inside code tags

thanks for replying....
init:
deps-jar:
compile-single:
run-single:
Got a client (client num : 1)

recving username:raf123
srecvng password:F75L14BX
recving ip:169.254.86.65
recvieg mac:00-0F-66-74-4F-C1
adaptport from db:49581
laserport from db:49158
inserting the values in database
Unable to connect to the database in authGeneral error
laserport got from db:49158
adport got from db:49581
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
the error is java.lang.NullPointerException

this is the output..
wat is this stacktrace...

server.lookupserver.main(serv);

Where is any of the above (aside from the String array serv) declared and/or defined.

According to your code, server is a package, so the only way this could possibly work, is if "lookupserver" were a class within server, and main a static method in that class.

the exception is caught just after run()


thanks for replying....
init:
deps-jar:
compile-single:
run-single:
Got a client (client num : 1)

recving username:raf123
srecvng password:F75L14BX
recving ip:169.254.86.65
recvieg mac:00-0F-66-74-4F-C1
adaptport from db:49581
laserport from db:49158
inserting the values in database
Unable to connect to the database in authGeneral error
laserport got from db:49158
adport got from db:49581
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
the error is java.lang.NullPointerException

this is the output..
wat is this stacktrace...

yeah there is a class in server package namely lookupserver

server.lookupserver.main(serv);

Where is any of the above (aside from the String array serv) declared and/or defined.

According to your code, server is a package, so the only way this could possibly work, is if "lookupserver" were a class within server, and main a static method in that class.

please check the run block...its there that the error occurs

yeah there is a class in server package namely lookupserver

The basis of your error, however, seems to be that you cannot connect to the DB. Are you sure that that the dsn is defined as "MS Access Database" as you have in your code.

If msk is the DSN (as your comment suggests), then use that.

no no...the database stuff is perfectly fine else it wudnt insert or retrieve tuples from the db...
u can check the output i posted a little earlier..
plz see the run() block...its there that the error is occuring

The basis of your error, however, seems to be that you cannot connect to the DB. Are you sure that that the dsn is defined as "MS Access Database" as you have in your code.

If msk is the DSN (as your comment suggests), then use that.

Then what is this
"Unable to connect to the database in authGeneral error"

i am so sorry...actually... i dint delete the record before starting the pgm...thats the reason why that error came up...this is the o/p


init:
deps-jar:
compile-single:
run-single:
Got a client (client num : 1)

recving username:raf123
srecvng password:F75L14BX
recving ip:169.254.86.65
recvieg mac:00-0F-66-74-4F-C1
adaptport from db:49581
laserport from db:49158
inserting the values in database
laserport got from db:49158
adport got from db:49581
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
the error is java.lang.NullPointerException
BUILD STOPPED (total time: 13 seconds)

Then what is this
"Unable to connect to the database in authGeneral error"

Your error then, seemingly occurs in this method
"enterintodb"
as that is called before the other (and the output around it is not contained in your output).

Also, the stack trace is all the other information that comes with that nullpointerexception. One piece of that will actually point you to the exact line where the error occurs. Which line is that?

God, I'm blind sometimes. Your error is not there (at least not this error). Your error is this

String[] serv=null;


serv[0]=String.valueOf(port_array[0]);//seending the port # of client who has the server socket

You set the String array serv to null, then attempt to access an index of that array, which will, of course, throw a nullpointerexception since serv is null.

oh..now wat do i do so as to avoid that serv = null thingy

Your error then, seemingly occurs in this method
"enterintodb"
as that is called before the other (and the output around it is not contained in your output).

Also, the stack trace is all the other information that comes with that nullpointerexception. One piece of that will actually point you to the exact line where the error occurs. Which line is that?

String[] serv=new String [10];

i changed it to this... is it OK?

God, I'm blind sometimes. Your error is not there (at least not this error). Your error is this

String[] serv=null;


serv[0]=String.valueOf(port_array[0]);//seending the port # of client who has the server socket

You set the String array serv to null, then attempt to access an index of that array, which will, of course, throw a nullpointerexception since serv is null.

oh man.... u are awesome.......
thanks...
that error has gone now...
but now the output gets stuck...
like this

init:
deps-jar:
compile-single:
run-single:
Got a client (client num : 1)

recving username:raf123
srecvng password:F75L14BX
recving ip:169.254.86.65
recvieg mac:00-0F-66-74-4F-C1
adaptport from db:49581
laserport from db:49158
inserting the values in database
laserport got from db:49158
adport got from db:49581
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
Entering into db


wat is to be done?


thanks a lot


God, I'm blind sometimes. Your error is not there (at least not this error). Your error is this

String[] serv=null;


serv[0]=String.valueOf(port_array[0]);//seending the port # of client who has the server socket

You set the String array serv to null, then attempt to access an index of that array, which will, of course, throw a nullpointerexception since serv is null.

You need to close your resultsets, statements, and connections (in every place you're using them).

oh ok...thanks...will do that.... sure trouble u more with my errors...thanks and bye for now

You need to close your resultsets, statements, and connections (in every place you're using them).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.