i have this pgm which receives a string and tries to find it in database and return a value. for the first execution its working fine, but if the string is not found in database and the pgm is to be restarted it doesnt work
the output is
init:
deps-jar:
compile-single:
run-single:
apt server- 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:49000
laserport from db:49100
connecting to database
connected to database
record with 169.254.86.65 is already existing
Newly entered record
ip :169.254.86.65
mac :00-0F-66-74-4F-C1
servport :8000
status :1
servoff :FILE
adaptport :49000
username :raf123
laserport got from db:49100
adport got from db:49000
serverip got from db:169.254.244.149
servoff got from db:FILE
servoff_port got from db:8000
starting lookup at: 49100
Starting lookupserver socket at port number:49100
Started lookupserver socket at port number:49100
got a client--lookupserver--Client number 1 Connected
ip recieved:169.254.86.65
mac recieved:00-0F-66-74-4F-C1
ip from db:169.254.86.65
mac from db:00-0F-66-74-4F-C1
the client requesting for f service
service :f not found
Starting lookupserver socket at port number:49100
it doesnt create a socket at the second time...wat cud be the error? i have made the client wait for 10000 ms so that server takes its time to close and create a new socket...
plz help me
import java.io.*;
import java.io.InputStream;
import java.net.*;
import java.lang.Object.*;
import java.sql.*;
import java.net.ServerSocket.*;
import java.lang.String.*;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
public class lookupserver implements Runnable
{
private ServerSocket srv;
private Socket socket;
private static int lookagain=0;
private int ID;
static String mac="";
static String ip="";
static String servoff="";
static ResultSet res;
static Connection conn;
static Statement stat;
public static void start(int laportrec) throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//DONOT CHANGE THIS
//conn = DriverManager.getConnection("jdbc:odbc:MS Access Database");//msk is the DSN Name
lookagain=laportrec;
//servoff=args[1];
int t=1;
try
{
while (t==1)
{
Runnable runnable = new lookupserver(laportrec);
Thread thread = new Thread(runnable);
thread.start();
t=0;
}
}
catch (Exception e) {
System.out.println("error is"+e);}
}
lookupserver(int port)
{
try {
System.out.println("Starting lookupserver socket at port number:"+port);
srv= new ServerSocket(port) ;
socket= srv.accept();
ID++;
System.out.println("Started lookupserver socket at port number:"+port);
} catch (UnknownHostException ex) {
System.out.println("unable to find host in lookupserver"+ex.getMessage());
//Logger.getLogger(lookupserver.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
System.out.println("unable to create serversocket in lookupserver"+ex.getMessage());
//Logger.getLogger(lookupserver.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void run()
{
try
{
System.out.println("got a client--lookupserver--Client number "+ID+" Connected");
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin);
DataOutputStream out = new DataOutputStream(sout);
String str= in.readUTF();//getting mac and ip from client
mac="";
ip="";
StringTokenizer tokens=new StringTokenizer(str,"#");
while(tokens.hasMoreTokens())// obtain ip and mac from str
{
mac=tokens.nextToken();
ip=tokens.nextToken();
}
System.out.println("ip recieved:"+ip);
System.out.println("mac recieved:"+mac);
// System.out.println("\nfrom client\n mac : " + mac +"\n ip : " + ip);
int t=1;
ResultSet rest=null;
Connection connt=null;
Statement statt = null ;
//DONOT CHANGE THIS
connt = DriverManager.getConnection("jdbc:odbc:MS Access Database");//msk is the DSN Name
try{
statt = connt.createStatement();
rest = statt.executeQuery("select * from clients_online where ip='"+ip+"' and mac='"+mac+"'");
rest.next();
}
catch(SQLException m)
{
System.out.println("Intruder alert");
statt.execute("delete * from cients_online where ip='"+ip+"'and mac='"+mac+"'");
statt.execute("insert into blocked values('"+ip+"','"+mac+"')");
//intruder alert..work here
}
String client_ip=rest.getString(1);
String client_mac=rest.getString(2);
System.out.println("ip from db:"+client_ip);
System.out.println("mac from db:"+client_mac);
rest.close();
statt.close();
statt = connt.createStatement();
while(t==1)
{
out.writeUTF("valid");
out.flush();
String servreq=in.readUTF();
System.out.println("\n the client requesting for "+servreq+" service");
rest = statt.executeQuery("select ip,servport,status from clients_online where servoff='"+servreq+"' and status=1");
if(rest.next())
{
String pip;
int servport=0;
int sts=0;
pip=rest.getString(1);// ADD TRY CATCH BLOCK
servport=rest.getInt(2);
sts=rest.getInt(3);
System.out.println("\nservice found at "+pip+" at"+servport);
System.out.println("the status is :"+sts);
if(sts==1)//if status=1
{
out.writeUTF("Success");
out.writeUTF(pip);
out.flush();
out.writeInt(servport);
out.flush();
}
}
else
{
out.writeUTF("Fail");
System.out.println("service :"+servreq+" not found");
socket.close();
srv.close();
Thread.sleep(4000);
server.lookupserver.start(lookagain);
}
t=0;
}//while
}//try
catch (Exception ex) {
Logger.getLogger(lookupserver.class.getName()).log(Level.SEVERE, null, ex);
}
}//run
}//class