I'm a beginner in Java and I have an assignment to build P2p File Sharing Java application. I started by creating server and client. Client can send messages to server and server responds. I believe the next step should be inserting the Thread into the server class. I read all about it and tried it bud I just can't pull it off. I hope someone can help me. Here is server class:
import java.net.*;
import java.io.*;
import java.util.*;
public class Server {
private static ServerSocket serverskiSoket;
private final static int PORT = 3334;
public static void main(String[] args) {
System.out.println("Server se povezuje na port: "+PORT);
try {
serverskiSoket = new ServerSocket(PORT);
System.out.println("Server aktivan: " + serverskiSoket);
System.out.println("Ceka se klijent ...");
} catch (IOException ex) {
String dodatnaPoruka = ex.getMessage().toString();
if (dodatnaPoruka.equals("Address already in use: JVM_Bind"))
System.out.println("Nemoguce je povezati se na port "+ PORT +" jer je zauzet od strane drugog servera.");
System.exit(1);
}
do {
handleClient();
} while(true);
}
private static void handleClient() {
Socket link = null;
try {
link = serverskiSoket.accept();
System.out.println("Klijent povezan: " + link);
Scanner ulazniTok = new Scanner(link.getInputStream());
PrintWriter izlazniTok = new PrintWriter(link.getOutputStream(), true);
int brojPoruka = 0;
String poruka = ulazniTok.nextLine();
while(!poruka.equals("zatvori")) {
System.out.println("Klijent kaze: " + poruka);
brojPoruka++;
izlazniTok.println("Poruka: " + brojPoruka + ": " + poruka);
poruka = ulazniTok.nextLine();
}
izlazniTok.println(brojPoruka + " poruka poslato.");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
System.out.println("Konekcija se zatvara...");
link.close();
} catch(IOException ioEx) {
System.out.println("Diskonekcija nije moguca! \nRazlog: " + ioEx.getMessage());
System.exit(1);
}
}
}
}
and here is Client class:
import java.net.*;
import java.io.*;
import java.util.*;
public class Klijent {
private static InetAddress host;
private static final int PORT = 3334;
public static void main(String[] args) {
System.out.println("Povezivanje klijenta u toku. Molim sacekajte...");
try {
host = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
System.out.println("ID hosta nije pronadjen");
System.exit(1);
}
pristupiServeru();
}
private static void pristupiServeru() {
Socket link = null;
try {
link = new Socket(host, PORT);
String IPAdresa = StringCutter.RaseciString(host.toString());
System.out.println("Povezan na host cija je IP adresa: "+IPAdresa+", a port: "+PORT);
Scanner ulazniTok = new Scanner(link.getInputStream());
PrintWriter izlazniTok = new PrintWriter(link.getOutputStream(), true);
Scanner unosKorisnika = new Scanner(System.in);
String poruka, odgovor;
do {
System.out.println("Unesite poruku: ");
poruka = unosKorisnika.nextLine();
izlazniTok.println(poruka);
odgovor = ulazniTok.nextLine();
if (!odgovor.contains("primljeno"))
System.out.println("Rekli ste serveru: " + odgovor);
else System.out.println(odgovor);
} while (!poruka.equals("zatvori"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
System.out.println("\n*Zatvara se konekcija sa serverom...*");
link.close();
} catch (IOException ex){
System.out.println("Diskonekcija je nemoguca");
System.exit(1);
}
}
}
}