Can you help me please?? I watched your tutorial but I still don't understand
I have code for Server,Client and ClientThread !
Code for Server:
import java.io.*;
import java.net.*;
public class ChatServer{
static KlijentNit klijenti[] = new KlijentNit[10];
public static void main(String args[]) {
int port = 2222;
if(args.length>0)port = Integer.parseInt(args[0]);
Socket clientSocket = null;
try {
ServerSocket serverSocket = new ServerSocket(port);
while(true){
clientSocket = serverSocket.accept();
for(int i=0; i<=klijenti.length; i++){
if(klijenti==null)
{
klijenti = new KlijentNit(clientSocket, klijenti);
klijenti.start();
break;
}
}
}
}
catch (IOException e) {
System.out.println(e);
}
}
}
Code for Client:
import java.io.*;
import java.net.*;
static Socket clientSocket = null;
static PrintStream os = null;
static BufferedReader is = null;
static BufferedReader ulazKonzola = null;
static boolean kraj = false;
static char username;
static char password;
public static void main(String[] args) {
try {
int port = 2222;
if(args.length>0)port = Integer.parseInt(args[0]);
clientSocket = new Socket("localhost",port); ulazKonzola = new BufferedReader(new
InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
new Thread(new ChatKlient()).start();
while (!kraj) {
os.println(ulazKonzola.readLine());
}
clientSocket.close();
} catch (UnknownHostException e) {
System.err.println("Don't know about host ");
}catch (IOException e) {
System.err.println("IOException: " + e);
}
}
public void run() {
String linijaOdServera;
try {
while ((linijaOdServera = is.readLine()) != null) {
System.out.println(linijaOdServera);
if (linijaOdServera.indexOf("*** Dovidjenja") == 0) {
kraj = true;
return;
}
}
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
Code for ClientThread:
import java.io.*;
import java.net.*;
public class KlijentNit extends Thread {
BufferedReader is = null;
PrintStream os = null;
Socket soket = null;
KlijentNit[] klijenti;
public KlijentNit(Socket soket, KlijentNit[] klijent) {
this.soket = soket;
this.klijenti = klijent;
}
public void run() {
String linija;
String ime;
try {
is = new BufferedReader(new InputStreamReader(soket.getInputStream()));
os = new PrintStream(soket.getOutputStream());
os.println("Unesite ime.");
ime = is.readLine();
os.println("Dobrodosao/la " + ime + ".\nZa izlaz unesite /quit");
for (int i = 0; i <= 9; i++) {
if (klijenti != null && klijenti != this) {
klijenti.os.println("*** Novi korisnik: " + ime + " je usao u chat sobu !!! ***");
}
}
while (true) {
linija = is.readLine();
if (linija.startsWith("/quit")) {
break;
}
for (int i = 0; i <= 9; i++) {
if (klijenti != null) {
klijenti.os.println("<" + ime + "> " + linija);
}
}
}
for (int i = 0; i <= 9; i++) {
if (klijenti != null && klijenti != this) {
klijenti.os.println("*** Korisnik " + ime + " izlazi iz chat sobe !!! ***");
}
}
os.println("*** Dovidjenja " + ime + " ***");
soket.close();
} catch (IOException ex) {
ex.printStackTrace();
for (int i = 0; i <= 9; i++) {
if (klijenti == this) {
klijenti = null;
}
}
}
}
}
I want to make server/client chat,for a many clients,but each of them must has a Inbox(for private messages,for communication "client to client"),every client must first register(username,password) and I have a base (for example. txt file) which will keep the names of all users and their messages.
PLEASE HELP ME.