From 1a5e2072e5ac4d383e8da76e7f5602c69ad4e989 Mon Sep 17 00:00:00 2001 From: Sergei Khmelevskiy Date: Thu, 17 Jan 2019 20:34:32 +0300 Subject: [PATCH 1/3] 1st hello --- src/chat/Client.java | 16 ++++++++++++++++ src/chat/Server.java | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/chat/Client.java b/src/chat/Client.java index ecc8d88..41fda36 100644 --- a/src/chat/Client.java +++ b/src/chat/Client.java @@ -1,7 +1,23 @@ package chat; +import java.io.DataInput; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.InetAddress; +import java.net.Socket; + public class Client { public static void main(String[] args) throws Exception { System.out.println("Client started!"); + + String address = "127.0.0.1"; + int port = 12345; + Socket socket = new Socket(InetAddress.getByName(address), port); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + + dos.writeUTF("Hello from client!"); + String str = dis.readUTF(); + System.out.println(str); } } \ No newline at end of file diff --git a/src/chat/Server.java b/src/chat/Server.java index cd56e71..a178538 100644 --- a/src/chat/Server.java +++ b/src/chat/Server.java @@ -1,7 +1,41 @@ package chat; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; + public class Server { public static void main(String[] args) throws Exception { System.out.println("Server started!"); + + String address = "127.0.0.1"; + int port = 12345; + ServerSocket ss = new ServerSocket(port, 50, InetAddress.getByName(address)); + Socket socket = ss.accept(); + DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); + DataInputStream dis = new DataInputStream(socket.getInputStream()); + + String str = dis.readUTF(); + dos.writeUTF("Hello from server!"); + + System.out.println(str); + } -} \ No newline at end of file +} + + +/* + String[] allStr = str.split("\\s+"); + if (allStr[1].equals("joined") || allStr[1].equals("left") || allStr[1].equals("disconnected") || allStr[1].equals("connected")){ + continue; + } + if (allStr[1].equals("sent")){ + allStr[1] = ":"; + System.out.print(allStr[0]); + for (int i = 1; i < allStr.length; i++){ + System.out.print(allStr[i] + " "); + } + System.out.println(""); + }*/ \ No newline at end of file From d0cc1075fed1a81db9f572d7abbcc460a3453e77 Mon Sep 17 00:00:00 2001 From: Sergei Khmelevskiy Date: Thu, 17 Jan 2019 22:06:19 +0300 Subject: [PATCH 2/3] client-serv-party --- src/chat/Client.java | 23 ++++++++++++-- src/chat/Server.java | 72 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 85 insertions(+), 10 deletions(-) diff --git a/src/chat/Client.java b/src/chat/Client.java index 41fda36..68f30ba 100644 --- a/src/chat/Client.java +++ b/src/chat/Client.java @@ -5,19 +5,36 @@ import java.io.DataOutputStream; 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 count = -1; + int port = 12345; Socket socket = new Socket(InetAddress.getByName(address), port); DataInputStream dis = new DataInputStream(socket.getInputStream()); DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); - dos.writeUTF("Hello from client!"); - String str = dis.readUTF(); - System.out.println(str); + System.out.println(dis.readUTF()); + + String str; + while (true) { + System.out.print("Type message: "); + str = sc.nextLine(); + count += str.split("\\s+").length; + if (str.equals("exit")) { + dos.writeUTF(str); + break; + } + dos.writeUTF(str); + str = dis.readUTF(); + System.out.println(str); + } } } \ No newline at end of file diff --git a/src/chat/Server.java b/src/chat/Server.java index a178538..3ed4c10 100644 --- a/src/chat/Server.java +++ b/src/chat/Server.java @@ -2,26 +2,84 @@ import java.io.DataInputStream; import java.io.DataOutputStream; +import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; +import java.net.SocketException; 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)); - Socket socket = ss.accept(); - DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); - DataInputStream dis = new DataInputStream(socket.getInputStream()); - String str = dis.readUTF(); - dos.writeUTF("Hello from server!"); + while (true) { + Socket socket = ss.accept(); + Cloent cl = new Cloent(socket, Clients); + cl.start(); + Clients++; + } + + + + } +} - System.out.println(str); +class Cloent extends Thread{ + DataInputStream dis; + DataOutputStream dos; + Socket socket; + int count = -1; + + Cloent(Socket socket, int cleintsNb) throws IOException { + super("Client-" + cleintsNb); + this.socket = socket; + this.dis = new DataInputStream(socket.getInputStream()); + this.dos = new DataOutputStream(socket.getOutputStream()); + dos.writeUTF("Connected to server. Hello!"); + } + + @Override + public void run(){ + System.out.println("Client connected."); + String str = ""; + while (!str.equals("exit")){ + try { + str = this.dis.readUTF(); + } catch (Exception e) { + try { + socket.close(); + break; + } catch (IOException e1) { + e1.printStackTrace(); + } + e.printStackTrace(); + } + if (!str.equals("exit")) { + System.out.println("Client wrote: " +str + " from " + Thread.currentThread().getName()); + } + try { + System.out.println("Send to client: " + "You wrote " + getWordsCount(str) + " word(s)."); + dos.writeUTF("You wrote " + getWordsCount(str) + " word(s)."); + } catch (Exception e) { + e.printStackTrace(); + } + count += getWordsCount(str); + } + try { + Thread.sleep(1000); + socket.close(); + System.out.println("Client disconnected."); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } + private int getWordsCount(String str){ + return str.split("\\s+").length; } } From 14d33bc1c52ee011965ba8058fe50aa8b144e17b Mon Sep 17 00:00:00 2001 From: Sergei Khmelevskiy Date: Fri, 18 Jan 2019 17:05:38 +0300 Subject: [PATCH 3/3] useful chat --- src/chat/Client.java | 46 +++++++++++++-- src/chat/Server.java | 130 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 146 insertions(+), 30 deletions(-) diff --git a/src/chat/Client.java b/src/chat/Client.java index 68f30ba..c9769c6 100644 --- a/src/chat/Client.java +++ b/src/chat/Client.java @@ -3,6 +3,7 @@ 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; @@ -14,7 +15,6 @@ public static void main(String[] args) throws Exception { String address = "127.0.0.1"; Scanner sc = new Scanner(System.in); - int count = -1; int port = 12345; Socket socket = new Socket(InetAddress.getByName(address), port); @@ -24,16 +24,52 @@ public static void main(String[] args) throws Exception { 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) { - System.out.print("Type message: "); str = sc.nextLine(); - count += str.split("\\s+").length; - if (str.equals("exit")) { + if (str.equals("/exit")) { + rd.alive = false; dos.writeUTF(str); + rd.join(); break; } dos.writeUTF(str); - str = dis.readUTF(); + } + } +} + +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); } } diff --git a/src/chat/Server.java b/src/chat/Server.java index 3ed4c10..8c9614a 100644 --- a/src/chat/Server.java +++ b/src/chat/Server.java @@ -3,10 +3,12 @@ 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 { @@ -15,11 +17,13 @@ public static void main(String[] args) throws Exception { 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); + Cloent cl = new Cloent(socket, Clients, db); cl.start(); + db.addClient(cl); Clients++; } @@ -32,23 +36,63 @@ 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) throws IOException { + + 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("Connected to server. Hello!"); + dos.writeUTF("Hello, pick your Nickname pleasse!"); } @Override public void run(){ System.out.println("Client connected."); String str = ""; - while (!str.equals("exit")){ + 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 = this.dis.readUTF(); + str = dis.readUTF(); } catch (Exception e) { try { socket.close(); @@ -58,19 +102,23 @@ public void run(){ } e.printStackTrace(); } - if (!str.equals("exit")) { - System.out.println("Client wrote: " +str + " from " + Thread.currentThread().getName()); - } - try { - System.out.println("Send to client: " + "You wrote " + getWordsCount(str) + " word(s)."); - dos.writeUTF("You wrote " + getWordsCount(str) + " word(s)."); - } catch (Exception e) { - 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) { @@ -81,19 +129,51 @@ public void run(){ 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 nameBase; + private ArrayList messages; + private ArrayList clients; -/* - String[] allStr = str.split("\\s+"); - if (allStr[1].equals("joined") || allStr[1].equals("left") || allStr[1].equals("disconnected") || allStr[1].equals("connected")){ - continue; - } - if (allStr[1].equals("sent")){ - allStr[1] = ":"; - System.out.print(allStr[0]); - for (int i = 1; i < allStr.length; i++){ - System.out.print(allStr[i] + " "); + 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"); } - System.out.println(""); - }*/ \ No newline at end of file + return str.toString(); + } + + synchronized void addMessage(String st){ + messages.add(st); + } + + synchronized ArrayList getClients() { + return clients; + } + + synchronized void addClient(Cloent client){ + this.clients.add(client); + } +} \ No newline at end of file