• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with login to the server

 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

I have written a java client code to connect to the server and then login to the server. I am able to connect and My login was successful but its not consistent. I had to run the program again and again as there is a request to be sent after login. it was not sucessfull, The problem is login is not consistent. when i run the program multiple times, sometimes it login and sometimes not.
Please help me.


thanks.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As noted in the other thread, without details, we cannot help.
 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Convert extends Thread {
int count = 0;
static Socket socket;

DataOutputStream os=null;
//ObjectOutputStream os2=null;
DataInputStream is=null;


private void connectServer() {
try {

socket = new Socket("IP", port);//IP and port number of the server
System.out.println("Connected to server in port ");

} catch (Exception e) {
System.out.println(" Exception in connectServer");
// System.err.println("unknown host");
e.printStackTrace();
}
}

void login() throws IOException {
// out = socket.getOutputStream();
try {

os = new DataOutputStream(socket.getOutputStream());
os.flush();
//byte[] info = new byte[51];
short val = 256;// Login value one.
//short type = swap(val);
//System.out.println( " Swapped Val is $$$$ " + type);
short len = 18944;// The length of the packet


//sending packet as per the rules.
os.writeShort(len);
os.writeShort(val);
os.writeBytes("username");
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeBytes("password");
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
os.writeByte(0);
// os.writeBytes(bin);
os.writeByte(0); // optional
os.writeBytes("client detils");
os.flush();

System.out.println("***login Response Packet >>>");
} catch (Exception e) {
System.out.println(" Exception in requestService");
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.out.println("***login Response closing ...>>>");

}
//readSocketReqService();

}

void flushh(){
try {
os.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
public static void main(String args[]) throws IOException {
try {
Convert t = new Convert();

DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();

t.connectServer();
System.out.println(dateFormat.format(date));
t.login();
//t.flush();
t.readSocketLogin();
t.flushh();

//System.out.println(dateFormat.format(date));


} catch (Exception e) {
System.out.println("Exception in Main method");
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
socket.close();
}


}

void readSocketLogin() throws IOException {
try {
// BufferedReader is = new BufferedReader(socket.getInputStream());
is = new DataInputStream(socket.getInputStream());
// BufferedInputStream bis = new BufferedInputStream(socket.
// getInputStream());

// InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

for (int count = 0; count <= 17; count++) {

short value = is.readShort();

/*if ( count < 3){
//System.out.println(" value before Swap >> " + reader);
x = swap(value);
//System.out.println(" value after Swap >> " + reader);

}*/

short reader = (short) (value / 256);

System.out.println(reader);



}
} catch (Exception e) {
System.out.println(" Exception in readSocketLogin");
e.printStackTrace();
}
}

}
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags -- thanks!
 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that new to the forum.

This is almost the whole code.



Thanks!
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Next time please edit your post instead of posting it again.

Your code doesn't compile. A variable is missing and a bracket is wrong. But you still haven't told us the real problem. Please TellTheDetails. What does happen and what do you expect to happen? And the code you posted is only on the client side. We don't know what the server is expecting and returning.

There is an unnecessary flush and your flushh method has two h's.
 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey thanks for the reply. I cleared the braces problem and the double 'h' in flushh is the method i've written different from the flush method.

I have to connect to the sever and then login to the server .After establishing the connection,(connectserver method)
The login packet is sent as per the specifications.(login() method)
After sending the requeste we have return codes as per the specification.

When i made the request for login it was sucessfull.
But the problem is, It is not consistent.
when i compile multiple times, Its successfull once and unsucessful again after recomplilation within next few seconds.

How do i make it consistent, even if i recompile it. The error which am gettin after recompiling is BAD_REQUEST_BUFFER(this is a return code as per the specification). I wonder if there is a problem while calling with the InputStream.









 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's "short len = 18944;// The length of the packet" all about? Is there some other definition of "packet length" that's not associated with the length of the packet?
 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats the unicode value which is suppose to be 74 in decimal. I tried sending either way but no difference. In the packet sequence the length of the packet and the type of the packet is to be sent as per the specification first.

In the result the out put is in unicode , thats why i divided the result by 256 to get in decimal to check with the return codes in the specification which is mentioned in decimal.

short reader = (short) (value / 256);

System.out.println(reader);

Thanks.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which specification are you referring to? Also see this about extending a Thread.
 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am refering to the specs i've been given.

Thanks.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do understand that since *we* don't have those specs, any help we offer is pure *spec*ulation, right?
 
Don boscoo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we have a c++ code running this application. It has to be converted to java. Now my question is, Do we need to write the server socket program too for the socket communication?

As of now I've written the client program and making a request to the server.

Thanks.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are not allowed to disclose that spec then just say so. Otherwise we'll keep asking for it. Java has a ServerSocket class so no you don't have to write that. You can try to analyze your output with a packet sniffer like Wireshark and compare it to what you want it to output.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!