lollboy
asked on
Server program response in 15 second!
Dear All !
hi
I'd made a client/server program that clients get some information from server (server gathers this information from user's profile stored in MySQL) the problem is raise because of 15 sec takes to get a simple response from server program to a client , I tested it on a LAN with 3 computer and very very low traffic(almost zero) , then could you help me to improve it in a way responsing sooner, i forgot to tell i user JAVA ,MySQL on windows!
here is the server class
package net.xagrose.officeSrv;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.Date;
import net.xagrose.officeLocalCla ss.*;
import net.xagrose.socket.*;
import net.xagrose.Authentication .*;
import net.xagrose.DB.*;
public class NetworkServer {
protected int port, maxConnections;
ObjectOutputStream output;
ObjectInputStream input;
Carrier msgCr;
private String SQLstr;
public NetworkServer(int port, int maxConnections) {
this.port = port;
this.maxConnections = maxConnections;
}
public void listen() {
int i=0;
try {
ServerSocket listener = new ServerSocket(port,100);
Socket server;
while (maxConnections == 0) {
server = listener.accept();
server.setSoTimeout(5000);
handleConnection(server);
}
} catch (IOException ioe) {
System.err.println("Networ kServer|li sten:" + ioe);
}
}
protected void handleConnection(Socket server) throws IOException{
SocketUtil s = new SocketUtil(server);
input=s.getInObjectStream( );
output=s.getOutObjectStrea m();
System.out.println("Generi c Network Server:\n" + "got connection from " + server.getInetAddress().ge tHostName( ) + "\n" );
switch(getMsg(input)){
case 1:{
loginObj userLogin=(loginObj)msgCr. getCarring Obj();
Authentication userAuth=new Authentication(userLogin.g etUser_nam e(),userLo gin.getUse r_psw());
userAuth.checkAuthenticati on();
if (userAuth.isAuthentified() ){
sendMsg("Ok!");
} else{
sendMsg("No!");
}
break;
}
case 2:{
//session started
InsertSession((String) msgCr.getCarringObj());
break;
}case 3:{
EndSession((String)msgCr.g etCarringO bj());
break;
}case 5 :{
//*
// ask if user have started a working session or not ,if so disable the start session and gets today date from server
//*
officeParameter OffParm= new officeParameter(make_persi an_date(), ISsessionS tarted());
sendMsg(OffParm);
break;
} case 6:{
PassChangerCarrier PCC=(PassChangerCarrier)ms gCr.getCar ringObj();
loginObj LO=(loginObj)PCC.getUser() ;
userProfileManager upm=new userProfileManager(LO.getU ser_name() ,LO.getUse r_psw());
if (upm.isUser()){
if(upm.changePassword(PCC. getNewPass ())){
sendMsg("Ok!");
}else{
sendMsg("No!");
}
}else{
sendMsg("NotUser!");
}
break;
}
}
server.close();
}
private void InsertSession(String id){
BaseMysql BM=new BaseMysql("office","1234", "office");
if ( BM.IntialConnection()){
Date now=new Date();
String SQLstr;
SQLstr="INSERT INTO user_worktime(date,start_t ime,user_i d) VALUES('"+ now.getYear()%100 +"-"+now.getDay()+"-"+now. getMonth() +"','"+ now.getHours() +":"+now.getMinutes()+":"+ now.getSec onds()+"', '"+id+"')" ;
BM.ExecModify(SQLstr);
BM.CloseConnection();
}
}
private void EndSession(String id){
BaseMysql BM=new BaseMysql("office","1234", "office");
if(BM.IntialConnection()){
Date now=new Date();
String SQLstr;
SQLstr="SELECT MAX(id) FROM user_worktime WHERE user_id='"+id+"'";
ResultSet rs=BM.ExecSelect(SQLstr);
if (rs!=null){
try {
int session_id=rs.getInt(1);
BM.CloseConnection();
BM=new BaseMysql("office","1234", "office");
BM.IntialConnection();
SQLstr= "UPDATE user_worktime set end_time='"+ now.getHours() +":"+now.getMinutes()+":"+ now.getSec onds()+"' WHERE id="+session_id;
//System.out.println(SQLst r);
if (BM.ExecModify(SQLstr)){
output.writeObject("Ok!");
output.flush();
}else{
output.writeObject("No!");
output.flush();
}
BM.CloseConnection();
} catch(IOException ex){
System.err.println(ex.toSt ring());
}catch(SQLException ex){
System.err.println(ex.toSt ring());
}
}
BM.CloseConnection();
}
}
private boolean ISsessionStarted() {
int id=0;
boolean isStarted=false;
BaseMysql BM=new BaseMysql("office","1234", "office");
if (BM.IntialConnection()){
String username=(String) msgCr.getCarringObj();
SQLstr= "SELECT MAX(id) FROM user_worktime where user_id='"+ username + "'";
ResultSet rs=BM.ExecSelect(SQLstr);
if( rs!=null){
try{
id=rs.getInt(1);
if (id==0){return false;}
BM.CloseConnection();
BM=new BaseMysql("office","1234", "office");
}catch(SQLException ex){
isStarted=false;
}
if (BM.IntialConnection()){
SQLstr="SELECT end_time FROM user_worktime where id="+ id;
rs=BM.ExecSelect(SQLstr);
if (rs!=null){
try{
if(rs.getTime(1).toString( ).equals(" 00:00:00") ||(rs.getT ime(1)==nu ll)){
isStarted=true;
}
}catch(SQLException ex){
isStarted=false;
}
}else{
isStarted=true;
}
}
}else{
isStarted=false;
}
}
return isStarted;
}
private String make_persian_date(){
return "Persian Date";
}
protected void sendMsg(Object msgTxt){
try{
output.writeObject(msgTxt) ;
output.flush();
}catch(IOException ex){
System.err.println("Networ kServer:"+ ex.toStrin g());
}
}
protected int getMsg(ObjectInputStream in ){
try{
msgCr=(Carrier)in.readObje ct();
if (msgCr.getCommand().equals ("Login!") ){
return 1;
}else if (msgCr.getCommand().equals ("StartSes sion!")){
return 2;
//here it start a session for given user
}else if (msgCr.getCommand().equals ("EndSessi on!")){
return 3;
//here it finish the session for given user
}else if (msgCr.getCommand().equals ("GetTotal Time!")){
return 4;
//here it send total working time in a period!
}else if (msgCr.getCommand().equals ("InitailO ffice!")){
return 5;
//here it send total working time in a period!
}else if(msgCr.getCommand().equa ls("Change Pass!")){
//here it changes user's password
return 6;
}
}catch(ClassNotFoundExcept ion ex){
System.err.println("Networ kServer:"+ ex.toStrin g());
}catch(IOException ex){
System.err.println("Networ kServer:"+ ex.toStrin g());
}
return 0;
}
public void start(){
listen();
}
}
hi
I'd made a client/server program that clients get some information from server (server gathers this information from user's profile stored in MySQL) the problem is raise because of 15 sec takes to get a simple response from server program to a client , I tested it on a LAN with 3 computer and very very low traffic(almost zero) , then could you help me to improve it in a way responsing sooner, i forgot to tell i user JAVA ,MySQL on windows!
here is the server class
package net.xagrose.officeSrv;
import java.net.*;
import java.io.*;
import java.sql.*;
import java.util.Date;
import net.xagrose.officeLocalCla
import net.xagrose.socket.*;
import net.xagrose.Authentication
import net.xagrose.DB.*;
public class NetworkServer {
protected int port, maxConnections;
ObjectOutputStream output;
ObjectInputStream input;
Carrier msgCr;
private String SQLstr;
public NetworkServer(int port, int maxConnections) {
this.port = port;
this.maxConnections = maxConnections;
}
public void listen() {
int i=0;
try {
ServerSocket listener = new ServerSocket(port,100);
Socket server;
while (maxConnections == 0) {
server = listener.accept();
server.setSoTimeout(5000);
handleConnection(server);
}
} catch (IOException ioe) {
System.err.println("Networ
}
}
protected void handleConnection(Socket server) throws IOException{
SocketUtil s = new SocketUtil(server);
input=s.getInObjectStream(
output=s.getOutObjectStrea
System.out.println("Generi
switch(getMsg(input)){
case 1:{
loginObj userLogin=(loginObj)msgCr.
Authentication userAuth=new Authentication(userLogin.g
userAuth.checkAuthenticati
if (userAuth.isAuthentified()
sendMsg("Ok!");
} else{
sendMsg("No!");
}
break;
}
case 2:{
//session started
InsertSession((String) msgCr.getCarringObj());
break;
}case 3:{
EndSession((String)msgCr.g
break;
}case 5 :{
//*
// ask if user have started a working session or not ,if so disable the start session and gets today date from server
//*
officeParameter OffParm= new officeParameter(make_persi
sendMsg(OffParm);
break;
} case 6:{
PassChangerCarrier PCC=(PassChangerCarrier)ms
loginObj LO=(loginObj)PCC.getUser()
userProfileManager upm=new userProfileManager(LO.getU
if (upm.isUser()){
if(upm.changePassword(PCC.
sendMsg("Ok!");
}else{
sendMsg("No!");
}
}else{
sendMsg("NotUser!");
}
break;
}
}
server.close();
}
private void InsertSession(String id){
BaseMysql BM=new BaseMysql("office","1234",
if ( BM.IntialConnection()){
Date now=new Date();
String SQLstr;
SQLstr="INSERT INTO user_worktime(date,start_t
BM.ExecModify(SQLstr);
BM.CloseConnection();
}
}
private void EndSession(String id){
BaseMysql BM=new BaseMysql("office","1234",
if(BM.IntialConnection()){
Date now=new Date();
String SQLstr;
SQLstr="SELECT MAX(id) FROM user_worktime WHERE user_id='"+id+"'";
ResultSet rs=BM.ExecSelect(SQLstr);
if (rs!=null){
try {
int session_id=rs.getInt(1);
BM.CloseConnection();
BM=new BaseMysql("office","1234",
BM.IntialConnection();
SQLstr= "UPDATE user_worktime set end_time='"+ now.getHours() +":"+now.getMinutes()+":"+
//System.out.println(SQLst
if (BM.ExecModify(SQLstr)){
output.writeObject("Ok!");
output.flush();
}else{
output.writeObject("No!");
output.flush();
}
BM.CloseConnection();
} catch(IOException ex){
System.err.println(ex.toSt
}catch(SQLException ex){
System.err.println(ex.toSt
}
}
BM.CloseConnection();
}
}
private boolean ISsessionStarted() {
int id=0;
boolean isStarted=false;
BaseMysql BM=new BaseMysql("office","1234",
if (BM.IntialConnection()){
String username=(String) msgCr.getCarringObj();
SQLstr= "SELECT MAX(id) FROM user_worktime where user_id='"+ username + "'";
ResultSet rs=BM.ExecSelect(SQLstr);
if( rs!=null){
try{
id=rs.getInt(1);
if (id==0){return false;}
BM.CloseConnection();
BM=new BaseMysql("office","1234",
}catch(SQLException ex){
isStarted=false;
}
if (BM.IntialConnection()){
SQLstr="SELECT end_time FROM user_worktime where id="+ id;
rs=BM.ExecSelect(SQLstr);
if (rs!=null){
try{
if(rs.getTime(1).toString(
isStarted=true;
}
}catch(SQLException ex){
isStarted=false;
}
}else{
isStarted=true;
}
}
}else{
isStarted=false;
}
}
return isStarted;
}
private String make_persian_date(){
return "Persian Date";
}
protected void sendMsg(Object msgTxt){
try{
output.writeObject(msgTxt)
output.flush();
}catch(IOException ex){
System.err.println("Networ
}
}
protected int getMsg(ObjectInputStream in ){
try{
msgCr=(Carrier)in.readObje
if (msgCr.getCommand().equals
return 1;
}else if (msgCr.getCommand().equals
return 2;
//here it start a session for given user
}else if (msgCr.getCommand().equals
return 3;
//here it finish the session for given user
}else if (msgCr.getCommand().equals
return 4;
//here it send total working time in a period!
}else if (msgCr.getCommand().equals
return 5;
//here it send total working time in a period!
}else if(msgCr.getCommand().equa
//here it changes user's password
return 6;
}
}catch(ClassNotFoundExcept
System.err.println("Networ
}catch(IOException ex){
System.err.println("Networ
}
return 0;
}
public void start(){
listen();
}
}
Although I personally do not know anything about the net.xagrose.* package and how it works internally, but your code seems to be creating a new database connection every time you talk to the DB. That's going to cost some time, especially if the database is running on a different computer. But I might just misinterpret how this BaseMysql class works. Also, is there a particular reason why you are transmitting Objects instead of bytes or strings? This may also affect performance. I personally cannot help more than that, because of my lack of knowledge about the package mentioned above.
ASKER
I make a package net.xagrose and use it! then BaseMysql class is for MySql connection handler and in this package I keep some transfering object (through Network) and also I used objects for transfering because of different type of data I want to transfer through network and I think it's better to use serilaized objects instead of using XML (because of low speed of XML)
above u mentioned that making connection may cause it , but at least when I test it on a computer both Client and Server programms it works well but when I try to test it on sperated ones the speed goes down.
above u mentioned that making connection may cause it , but at least when I test it on a computer both Client and Server programms it works well but when I try to test it on sperated ones the speed goes down.
From the code posted here, I don't think it can be determined where the problem is. Since there is other code that your wrote and relying upon, the problem might be there. Maybe it has to do with your custom protocol. Maybe there is some network specific code? Are you creating a new connection from the client for each message? How does the client find the server (by IP address or network name). How is the network name of the server looked up? Maybe that's where the client spends all the time. You may also want to log certain events on the server and then see what happens and when while a client is trying to connect.
Did you get anywhere with this - did any of my comments help?
ASKER
thanks all
I found what was the problem!
nor ip or mysql reading
I changed it by following:
instead if function connection hanling i used a thread connection handler and then in client side i keep the refrence of socket i made to connect to server
I found what was the problem!
nor ip or mysql reading
I changed it by following:
instead if function connection hanling i used a thread connection handler and then in client side i keep the refrence of socket i made to connect to server
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.