We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Im trying to make a login client with a server. this is the client code
problem is that when server sends answer to req it shows as null and not DNY which the server sent.
import processing.net.*;
char LS = 'A';
String UN = "";
String PW = "";
String address = "localhost";
Client myClient;
void setup(){
size(600,620);
myClient = new Client(this,address,5205);
}
void draw(){
if (myClient != null) {
if (myClient.available() > 0) {
println("Recieved");
if (myClient.readString() == "AGR") {
println("greed");
} else if (myClient.readString().startsWith("DNY") == true){
println("Denied");
} else {
println("Empty response");
println(myClient.readString());
}
}else{
}
}
background(0);
fill(200,0,0);
rect(width/2-200,height/2+5,400,-30);
rect(width/2-200,height/2+50,400,-30);
rect(width/2-50,height/2+100,100,-30);
fill(0,0,0);
textSize(18);
text("Login",width/2-22,height/2+90);
text(UN,105,305);
text(PW,105,350);
}
void keyReleased(){
if (keyCode == BACKSPACE){
switch (LS) {
case 'A':
if (UN.length() > 0) {
UN = UN.substring(0, UN.length()-1);
}
break;
case 'B':
if (PW.length() > 0) {
PW = PW.substring(0, PW.length()-1);
}
break;
}}
if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && keyCode != BACKSPACE){
switch (LS) {
case 'A':
UN = UN + key;
break;
case 'B':
PW = PW + key;
break;
}}}
void mousePressed(){
if (mouseX >= width/2-200 && mouseY <= height/2+5 && mouseX <= width/2+200 && mouseY >= height/2+5-30) {
LS = 'A';
} else if (mouseX >= width/2-200 && mouseY <=
height/2+50 && mouseX <= width/2+200 && mouseY >= height/2+50-30) {
LS = 'B';
} else if (mouseX >= width/2-50 && mouseY
<= height/2+100 && mouseX <= width/2-50+100 && mouseY >= height/2+100-30) {
myClient.write("req"+" - "+UN+PW);
println("Sent REQ");
}}
And this is the server code:
also there is a text document in sketch folder called users.txt and contains the word test123123test
import processing.net.*;
boolean userExists = false;
Server myServer;
void setup(){
size(600,620);
myServer = new Server(this,5205);
}
void draw(){
Client myClient = myServer.available();
if (myClient != null) {
if (myClient.available() > 0) {
println("Noticed Message Incoming");
String mess = myClient.readString();
String[] Smess = split(mess," - ");
if (Smess[0].startsWith("req") == true) {
println("noticed request");
println(Smess[1]+"Is The User");
String[] users = loadStrings("users.txt");
for(int i = 0; i < users.length; i++){
if (users[i] == Smess[1]) {
println("userExists");
userExists = true;
}
}
if (userExists == true) {
myServer.write("AGR");
println("Sendin Agree");
} else if (userExists == false){
myServer.write("DNY");
println("Sendin Deny");
} else {
println("ERROR");
}
}
}
}
}
Answers
myClient.readString() == "AGR"
This test will always fails. See String in the Reference. Use .equals() instead.
Also
boolean
tests like like these:can be simplified a lot like this way for example: