Open Dj0leR opened 5 years ago
package mrezno.programiranje;
import java.awt.List; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList;
public class Server {
public static final int TCP_PORT = 9000;
public static void main(String[] args) {
try {
int clientCounter = 0;
ArrayList<String> list = new ArrayList<>();
// slusaj zahteve na datom portu
@SuppressWarnings("resource")
ServerSocket ss = new ServerSocket(TCP_PORT);
System.out.println("Server running...");
while (true) {
Socket sock = ss.accept();
System.out.println("Client accepted: " + (++clientCounter));
UserThread ut=new UserThread(sock, clientCounter,list);
ut.run();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void handleReqeust(Socket sock, int value) {
try {
BufferedReader in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
// inicijalizuj izlazni stream
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
sock.getOutputStream())), true);
// procitaj zahtev
String request = in.readLine();
// odgovori na zahtev
out.println("(" + value + ")");
// zatvori konekciju
in.close();
out.close();
sock.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
package mrezno.programiranje;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.Socket; import java.util.ArrayList;
public class UserThread extends Thread {
private Socket sock;
private int value;
private ArrayList<String> list = new ArrayList<>();
public UserThread(Socket sock,int value,ArrayList<String> list) {
this.value=value;
this.sock=sock;
this.list=list;
}
public void run() {
try {
String s="";
BufferedReader in = new BufferedReader(
new InputStreamReader(sock.getInputStream()));
// inicijalizuj izlazni stream
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
sock.getOutputStream())), true);
// procitaj zahtev
String request = in.readLine();
// odgovori na zahtev
if(request.contains("ADD")) {
request = request.substring(4);
if(!request.contains(",")) {
if(!postoji(request))
list.add(request);
}else {
String[] a =request.split(",");
for(int i =0;i<a.length;i++) {
if(!postoji(a[i])) {
list.add(a[i]);
}
}
}
s="Succes";
}else if(request.contains("LIST")) {
s = "Users: ";
for(int i=0;i<list.size();i++) {
if(i<list.size()-1)
s+= list.get(i)+",";
else
s+=list.get(i)+".";
}
}else if(request.contains("REMOVE")) {
request= request.replace("REMOVE ", "");
if(list.remove(request))
s="Removed: "+request;
else
s="User not found.";
}else if(request.contains("FIND")) {
request= request.replace("FIND ", "");
if(postoji(request))
s="Found: "+request;
else
s="User not found.";
}else if(request.contains("END")) {
s="Bye :)";
}
out.println(s);
// zatvori konekciju
in.close();
out.close();
sock.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private boolean postoji(String request) {
for(int i=0;i<list.size();i++) {
if(list.get(i).equals(request)) {
return true;
}
}
return false;
}
}
`package mrezno.programiranje;
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.util.Scanner;
import org.omg.CORBA.Request;
public class Client {
}`