diff --git a/homework6/homework6.iml b/homework6/homework6.iml new file mode 100644 index 0000000..9465dd8 --- /dev/null +++ b/homework6/homework6.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/homework6/out/production/homework6/ChatClient.class b/homework6/out/production/homework6/ChatClient.class new file mode 100644 index 0000000..15f6c64 Binary files /dev/null and b/homework6/out/production/homework6/ChatClient.class differ diff --git a/homework6/out/production/homework6/ChatServer.class b/homework6/out/production/homework6/ChatServer.class new file mode 100644 index 0000000..c29c3cb Binary files /dev/null and b/homework6/out/production/homework6/ChatServer.class differ diff --git a/homework6/out/production/homework6/Client$1.class b/homework6/out/production/homework6/Client$1.class new file mode 100644 index 0000000..4741b53 Binary files /dev/null and b/homework6/out/production/homework6/Client$1.class differ diff --git a/homework6/out/production/homework6/Client$2.class b/homework6/out/production/homework6/Client$2.class new file mode 100644 index 0000000..28d4e64 Binary files /dev/null and b/homework6/out/production/homework6/Client$2.class differ diff --git a/homework6/out/production/homework6/Client.class b/homework6/out/production/homework6/Client.class new file mode 100644 index 0000000..521aba6 Binary files /dev/null and b/homework6/out/production/homework6/Client.class differ diff --git a/homework6/src/ChatClient.java b/homework6/src/ChatClient.java new file mode 100644 index 0000000..4fe7b57 --- /dev/null +++ b/homework6/src/ChatClient.java @@ -0,0 +1,28 @@ +import java.io.IOException; +import java.net.Socket; + + public class ChatClient { + + private final String SERVER_ADDR = "localhost"; + private final int SERVER_PORT = 8189; + + public ChatClient() { + try { + Socket sock = new Socket(SERVER_ADDR, SERVER_PORT); + new Client(sock, "Клиент"); + while(true){ + if(sock.isClosed()){ + break; + } + } + System.exit(0); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + new ChatClient(); + } + } + diff --git a/homework6/src/ChatServer.java b/homework6/src/ChatServer.java new file mode 100644 index 0000000..984c5ed --- /dev/null +++ b/homework6/src/ChatServer.java @@ -0,0 +1,39 @@ +import java.io.IOException; +import java.net.ServerSocket; +import java.net.Socket; + + public class ChatServer { + + public ChatServer(){ + ServerSocket serv = null; + Socket sock = null; + try { + serv = new ServerSocket(8189); + System.out.println("Сервер запущен, ожидаем подключения..."); + sock = serv.accept(); + System.out.println("Клиент подключился"); + new Client(sock, "Сервер"); + while(true){ + if(sock.isClosed()){ + break; + } + } + serv.close(); + System.exit(0); + } catch (IOException e) { + System.out.println("Ошибка инициализации сервера"); + } finally { + try { + serv.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + } + + public static void main(String[] args) { + new ChatServer(); + } + } + diff --git a/homework6/src/Client.java b/homework6/src/Client.java new file mode 100644 index 0000000..1f5f1b0 --- /dev/null +++ b/homework6/src/Client.java @@ -0,0 +1,63 @@ +import java.io.IOException; +import java.io.PrintWriter; +import java.net.Socket; +import java.util.Scanner; + +public class Client { + private Scanner in; + private Scanner input; + private PrintWriter out; + private Thread threadIn; + private Thread threadOut; + public Client(Socket sock, String name) { + try { + in = new Scanner(sock.getInputStream()); + input = new Scanner(System.in); + out = new PrintWriter(sock.getOutputStream()); + } catch (IOException e) { + e.printStackTrace(); + } + + threadOut = new Thread(new Runnable() { + @Override + public void run() { + while (true) { + if (input.hasNext()) { + String q = input.next(); + sendMsg(name+": "+q); + if (q.equalsIgnoreCase("close")) break; + } + } + close(sock); + + } + }); + threadOut.start(); + threadIn = new Thread(new Runnable() { + @Override + public void run() { + while (true) { + if (in.hasNext()) { + String w = in.nextLine(); + System.out.println(w); + if (w.contains("close")) break; + } + } + close(sock); + } + }); + threadIn.start(); + } + private void sendMsg(String w) { + out.println(w); + out.flush(); + } + private void close(Socket sock){ + threadIn.interrupt(); + threadOut.interrupt(); + try { + sock.close(); + } catch (Exception e) {} + + } +} \ No newline at end of file