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
11 changes: 11 additions & 0 deletions homework6/homework6.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file not shown.
Binary file not shown.
Binary file added homework6/out/production/homework6/Client$1.class
Binary file not shown.
Binary file added homework6/out/production/homework6/Client$2.class
Binary file not shown.
Binary file added homework6/out/production/homework6/Client.class
Binary file not shown.
28 changes: 28 additions & 0 deletions homework6/src/ChatClient.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

39 changes: 39 additions & 0 deletions homework6/src/ChatServer.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

63 changes: 63 additions & 0 deletions homework6/src/Client.java
Original file line number Diff line number Diff line change
@@ -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) {}

}
}