Kullanim
Terminal/Komut Istemcisinden calistirilabilir.
Server calistirilmali:
java ./Chat/Server
Client icin:
java ./Chat/Client
Server Kodları:
package Chat.Server;
import java.io.*;
import java.net.*;
import java.util.*;
public class Server{
private int port;
private Set<String> kullaniciAdlari = new HashSet<>();
private Set<KullaniciThread> kullaniciThreads = new HashSet<>();
public Server(int port) {
this.port = port;
}
public void calistir() {
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Serverin dinledigi port: " + port);
while(true) {
Socket socket = serverSocket.accept();
System.out.println("Yeni kullanici baglandi");
KullaniciThread yeniKullanici = new KullaniciThread(socket, this);
kullaniciThreads.add(yeniKullanici);
yeniKullanici.start();
}
} catch (IOException e) {
System.out.println("Server hatasi: " + e.getMessage());
e.printStackTrace();
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Syntax: Java ChatApp Server <port-number>");
System.exit(0);
}
int port = Integer.parseInt(args[0]);
Server server = new Server(port);
server.calistir();
}
void yayin(String msg, KullaniciThread hariciKullanici) {
for (KullaniciThread secKullanici : kullaniciThreads) {
if (secKullanici != hariciKullanici){
secKullanici.msgGonder(msg);
}
}
}
void ekleKullaniciAdi(String kullaniciAdi) {
kullaniciAdlari.add(kullaniciAdi);
}
void cikarKullanici(String kullaniciAdi, KullaniciThread secKullanici){
boolean cikarildi = kullaniciAdlari.remove(kullaniciAdi);
if (cikarildi) {
kullaniciThreads.remove(secKullanici);
System.out.println("Kullanici " + kullaniciAdi + "Serveri terk etti.");
}
}
Set<String> getKullaniciAdlari(){
return this.kullaniciAdlari;
}
boolean varKullanicilar() {
return !this.kullaniciAdlari.isEmpty();
}
}
package Chat.Server;
import java.io.*;
import java.net.*;
public class KullaniciThread extends Thread {
private Socket socket;
private Server server;
private PrintWriter writer;
public KullaniciThread(Socket socket, Server server){
this.socket = socket;
this.server = server;
}
public void calistir(){
try{
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
OutputStream output = socket.getOutputStream();
writer = new PrintWriter(output, true);
ciktiKullanicilar();
String kullaniciAdi = reader.readLine();
server.ekleKullaniciAdi(kullaniciAdi);
String serverMsg = "Yeni kullanici baglandi: " + kullaniciAdi;
server.yayin(serverMsg, this);
String clientMsg;
do {
clientMsg = reader.readLine();
serverMsg = "[" + kullaniciAdi + "]" + clientMsg;
server.yayin(serverMsg, this);
} while (!clientMsg.equals("{cikis}"));
server.cikarKullanici(kullaniciAdi, this);
socket.close();
serverMsg = kullaniciAdi + " cikis yapti.";
server.yayin(serverMsg, this);
} catch (IOException e) {
System.out.println("Kullanici Thread hatasi: " + e.getMessage());
e.printStackTrace();
}
}
void ciktiKullanicilar() {
if (server.varKullanicilar()) {
writer.println("Baglanan kullanicilar: " + server.getKullaniciAdlari());
} else {
writer.println("Baska kullanici baglanmadi.");
}
}
void msgGonder(String msg){
writer.println(msg);
}
}
Client Kodları:
package Chat.Client;
import java.io.*;
import java.net.*;
public class Client {
private String hostname;
private int port;
private String kullaniciAdi;
public Client(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public void calistir(){
try{
Socket socket = new Socket(hostname, port);
System.out.println("Server'e baglanti saglandi");
new ThreadOku(socket, this).start();
new ThreadYaz(socket, this).start();
} catch (UnknownHostException e) {
System.out.println("Server bulunamadi: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO Hatasi:" + e.getMessage());
}
}
void setKullaniciAdi(String kullaniciAdi){
this.kullaniciAdi = kullaniciAdi;
}
String getKullaniciAdi(){
return this.kullaniciAdi;
}
public static void main(String[] args) {
if (args.length < 2) return;
String hostname = args[0];
int port = Integer.parseInt(args[1]);
Client client = new Client(hostname, port);
client.calistir();
}
}
package Chat.Client;
import java.io.*;
import java.net.*;
public class ThreadYaz extends Thread{
private PrintWriter writer;
private Socket socket;
private Client client;
public ThreadYaz(Socket socket, Client client) {
this.socket = socket;
this.client = client;
try {
OutputStream output = socket.getOutputStream();
writer = new PrintWriter(output, true);
} catch (IOException e) {
System.out.println("Cikti Yayininda Hata: " + e.getMessage());
e.printStackTrace();
}
}
public void calistir() {
Console console = System.console();
String kullaniciAdi = console.readLine("\nKullanici Adinizi girin: ");
client.setKullaniciAdi(kullaniciAdi);
writer.println(kullaniciAdi);
String text;
do {
text = console.readLine("[" + kullaniciAdi + "]: ");
writer.println(text);
} while (!text.equals("{cikis}"));
try {
socket.close();
} catch (IOException e) {
System.out.println("Server yazdirma Hatasi: " + e.getMessage());
}
}
}
package Chat.Client;
import java.io.*;
import java.net.*;
public class ThreadOku extends Thread {
private BufferedReader reader;
private Client client;
private Socket socket;
public ThreadOku(Socket socket, Client client) {
this.client = client;
try {
InputStream input = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(input));
} catch (IOException e) {
System.out.println("Girdi Yayininda Hata: " + e.getMessage());
e.printStackTrace();
}
}
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public void calistir() {
while (true) {
try {
String cevap = reader.readLine();
System.out.println("\n" + cevap);
if (client.getKullaniciAdi() != null) {
System.out.println("[" + client.getKullaniciAdi() + "]: ");
}
} catch (IOException e) {
System.out.println("Server okuma hatasi: " + e.getMessage());
e.printStackTrace();
break;
}
}
}
}