I am writing a programme where I want to do a simple client server application that allows me to send messages from the client[list ,add new user,delete a user].Both at deleting and adding a user the server has a vector where it looks through to find if there is that certain user. I introduce values to check the case when there are matching values but the if doesn;t work. Any ideeas? ANd the code is:
The client
public class Client {
public static void main(String[] args) {
Socket clientSocket = null;
DataInputStream is = null;
PrintStream os = null;
DataInputStream inputLine = null;
try {
clientSocket = new Socket("localhost", 2227);
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
inputLine = new DataInputStream(new BufferedInputStream(System.in));
} catch (UnknownHostException e) {
System.err.println("Don't know about host");
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to host");
}
if (clientSocket != null && os != null && is != null) {
try {
System.out.println("The client started. Type any text. To quit it type 'Ok'.");
String responseLine;
os.println(inputLine.readLine());
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("Ok") != -1) {
break;
}
os.println(inputLine.readLine());
}
/*
* Close the output stream, close the input stream, close the socket.
*/
os.close();
is.close();
clientSocket.close();
} catch (UnknownHostException e) {
System.err.println("Trying to connect to unknown host: " + e);
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
}
And the Server:
public class Server {
public static void main(String args[]) {
class User{
String nume, parola,email;
public User(String numi ,String pari , String emai){
this.nume=numi;
this.parola = pari;
this.email = emai;
}
public String toString(){
return this.nume +" "+ this.email + " "+this.email+" ";
}
}
ServerSocket echoServer = null;
String line;
DataInputStream is;
PrintStream os;
Socket clientSocket = null;
try {
echoServer = new ServerSocket(2227);
} catch (IOException e) {
System.out.println(e);
}
System.out.println("The server started. To stop it press <CTRL><C>.");
try {
clientSocket = echoServer.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
ArrayList<User> user = new ArrayList<User>();
while (true) {
line = is.readLine();
if(line.startsWith("connect")){
String[] words = line.split(" ");
String nume = words[1];
String parola = words[2];
String email = words[3];
User a = new User(nume,parola,email);
boolean isThere = false;
for (int i = 0 ; i< user.size();i++){
if (user.get(i).equals(a)){
os.println("The user exists");
isThere= true;
os.flush();
}
}
if (isThere==false){
os.println("He doesn;t exist");
user.add(a);
os.flush();
//os.println(user.toString());
}
}
else if (line.contains("list")){
os.println(user.toString());
}
else if (line.startsWith("delete")){
String []userInfo = line.split(" ");
String nume = userInfo[1];
String parola = userInfo[2];
String email = userInfo[3];
user.remove(new User(nume,parola,email));
os.println("I deleted him");
os.flush();
}
// os.println("From server: " + line);
}
} catch (IOException e) {
System.out.println(e);
}
}
}