Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/chat/Client.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,76 @@
package chat;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class Client {
public static void main(String[] args) throws Exception {

System.out.println("Client started!");

String address = "127.0.0.1";
Scanner sc = new Scanner(System.in);

int port = 12345;
Socket socket = new Socket(InetAddress.getByName(address), port);
DataInputStream dis = new DataInputStream(socket.getInputStream());
DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

System.out.println(dis.readUTF());

String str;
//Nick picking
while (true){
str = sc.nextLine();
dos.writeUTF(str);

str = dis.readUTF();
if (str.equals("run"))
break;
System.out.println(str);
}
str = dis.readUTF();
System.out.print(str);
readBuffer rd = new readBuffer(socket);
rd.start();
while (true) {
str = sc.nextLine();
if (str.equals("/exit")) {
rd.alive = false;
dos.writeUTF(str);
rd.join();
break;
}
dos.writeUTF(str);
}
}
}

class readBuffer extends Thread{
DataInputStream dis;
DataOutputStream dos;
boolean alive = true;

readBuffer(Socket socket) throws IOException {
this.dis = new DataInputStream(socket.getInputStream());
this.dos = new DataOutputStream(socket.getOutputStream());
}

@Override
public void run(){
String str = "";
while (alive){
try {
str = dis.readUTF();
} catch (IOException e) {
break;
}
System.out.println(str);
}
}
}
172 changes: 172 additions & 0 deletions src/chat/Server.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,179 @@
package chat;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;

public class Server {
public static void main(String[] args) throws Exception {
System.out.println("Server started!");
int Clients = 1;
String address = "127.0.0.1";
int port = 12345;
ServerSocket ss = new ServerSocket(port, 50, InetAddress.getByName(address));
DataBase db = new DataBase();

while (true) {
Socket socket = ss.accept();
Cloent cl = new Cloent(socket, Clients, db);
cl.start();
db.addClient(cl);
Clients++;
}



}
}

class Cloent extends Thread{
DataInputStream dis;
DataOutputStream dos;
Socket socket;
DataBase db;
int count = -1;
int nbOfMsg = 10;//how many words get at enter chat


Cloent(Socket socket, int cleintsNb, DataBase db) throws IOException {
super("Client-" + cleintsNb);
this.db = db;
this.socket = socket;
this.dis = new DataInputStream(socket.getInputStream());
this.dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("Hello, pick your Nickname pleasse!");
}

@Override
public void run(){
System.out.println("Client connected.");
String str = "";
String selfName = "";

//picking nickname
while (true){
try {
str = dis.readUTF();
System.out.println("Client wrote: " +str + " from " + Thread.currentThread().getName());
} catch (IOException e) {
e.printStackTrace();
}
if (db.getNameBase().contains(str)){
try {
dos.writeUTF("Name already used, please pick other.");
} catch (IOException e) {
e.printStackTrace();
}
}
else {
try {
db.addName(str);
selfName = str;
dos.writeUTF("run");
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}

try {
dos.writeUTF(db.getChat(nbOfMsg));
} catch (IOException e) {
e.printStackTrace();
}


while (!str.equals("/exit")){
try {
str = dis.readUTF();
} catch (Exception e) {
try {
socket.close();
break;
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
if (!str.equals("/exit")) {
db.addMessage(selfName + ": " + str);
System.out.println("Client wrote: " + str + " from " + Thread.currentThread().getName());
try {
//sending
for (Cloent a : db.getClients()) {
a.sendMsg(selfName + ": " + str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
count += getWordsCount(str);
}
try {
Thread.sleep(1000);
db.getClients().remove(this);
socket.close();
System.out.println("Client disconnected.");
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}

private int getWordsCount(String str){
return str.split("\\s+").length;
}

private void sendMsg(String msg) throws IOException {
dos.writeUTF(msg);
}
}

class DataBase implements Serializable {
private ArrayList<String> nameBase;
private ArrayList<String> messages;
private ArrayList<Cloent> clients;

DataBase(){
nameBase = new ArrayList<>();
messages = new ArrayList<>();
clients = new ArrayList<>();
messages.add("Hello from the server!");
}

synchronized void addName(String nameBase) {
this.nameBase.add(nameBase);
}

synchronized ArrayList getNameBase(){
return nameBase;
}

synchronized String getChat(int nb){
StringBuilder str = new StringBuilder();
if (nb > messages.size())
nb = messages.size();
for (int i = 0; i < nb; i++){
str.append(messages.get(messages.size() - (nb - i))).append("\n");
}
return str.toString();
}

synchronized void addMessage(String st){
messages.add(st);
}

synchronized ArrayList<Cloent> getClients() {
return clients;
}

synchronized void addClient(Cloent client){
this.clients.add(client);
}
}