Mam problem z aplikacją klient-serwer. Jej zadanie jest takie: klient pisze do serwera: Jan Kowalski 234 a Serwer dodaje obiekt "Osoba" do wektora, i cały wektor wysyła klientowi. Jeśli klient wysle do serwera: WYJDZ Jan Kowalski 234 to serwer powinien usunąć tą osobę z wektora i wysłać wektor bez tej osoby do klienta ...
Nie działa mi usuwanie klientów, w ogóle serwer nie reaguje na komende 'wyjdz' :(
Pomoze ktoś? A, i jak ograniczyć liczbę klientów do 5?
package javaapplication48;
import java.io.*;
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
public class Server {
public static void main(String argv[]) throws IOException {
List<Osoba> ls = new ArrayList<Osoba>();
ServerSocket ss = new ServerSocket(1234);
System.out.println("starting...");
while (true) {
new ServerConnection(ss.accept(), ls).start();
System.out.println("new connection");
}
}
}
class ServerConnection extends Thread {
Socket client;
List<Osoba> ls;
int i = 0;
ServerConnection(Socket client, List<Osoba> ls) throws SocketException {
this.client = client;
this.ls = ls;
setPriority(NORM_PRIORITY - 1);
}
@Override
public void run() {
boolean connection = true; // connection tester for current client
try {
while (connection) {
BufferedReader in = new BufferedReader(
new InputStreamReader(client.getInputStream(), "UTF-8"));
OutputStream out = client.getOutputStream();
PrintWriter pout = new PrintWriter(
new OutputStreamWriter(out, "UTF-8"), true);
String s;
while ((s = in.readLine()) != null) {
s = s.trim();
if (s.startsWith("WYJDZ")) {
System.out.println("WYJDZ");
String tmp[] = s.split(" ");
if (ls.remove(new Osoba(tmp[1], tmp[2], tmp[3]))) {
System.out.println("USUNALEM");
}
Iterator it = ls.iterator();
while (it.hasNext()) {
String value = it.next().toString();
System.out.println(value);
pout.write(value);
pout.flush();
}
} else {
String tmp[] = s.split(" ");
if (ls.add(new Osoba(tmp[0], tmp[1], tmp[2]))) {
System.out.println("DODAŁEM");
}
Iterator it = ls.iterator();
while (it.hasNext()) {
String value = it.next().toString();
System.out.println(value);
pout.write(value);
pout.flush();
}
}
}
}
} catch (IOException e) {
System.out.println("I/O error " + e);
}
}
}
class Osoba {
String imie;
String nazwisko;
String tel;
public Osoba(String imie, String nazwisko, String tel) {
this.imie = imie;
this.nazwisko = nazwisko;
this.tel = tel;
}
public String getnazwisko() {
return nazwisko;
}
public void setnazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public String getimie() {
return imie;
}
public void setimie(String imie) {
this.imie = imie;
}
public String gettel() {
return tel;
}
public void settel(String tel) {
this.tel = tel;
}
//przygotowuje do wyslania obiekt Osoba
@Override
public String toString() {
return imie + " " + tel + " " + nazwisko;
}
}