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 homework7/homework7.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.
23 changes: 23 additions & 0 deletions homework7/out/production/homework7/hw7/client/FXMLDocument.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="256.0" prefWidth="348.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="hw7.client.FXMLDocumentController">
<children>
<VBox layoutX="0.0" layoutY="0.0" prefHeight="219.0" prefWidth="348.0">
<children>
<TextArea fx:id="TextArea" editable="false" prefHeight="219.0" prefWidth="348.0" promptText="" wrapText="true" />
</children>
</VBox>
<HBox layoutX="0.0" layoutY="219.0" prefHeight="37.0" prefWidth="348.0">
<children>
<TextField fx:id="TextField" onAction="#sendMsg" prefHeight="37.0" prefWidth="288.0" promptText="Введите текст" />
<Button fx:id="sendButton" mnemonicParsing="false" onAction="#sendMsg" prefHeight="37.0" prefWidth="99.0" text="Отправить" />
</children>
</HBox>
</children>
</AnchorPane>
Binary file not shown.
Binary file not shown.
Binary file not shown.
17 changes: 17 additions & 0 deletions homework7/src/hw7/Runner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hw7;

import hw7.client.Client;
import hw7.server.MyServer;
import javafx.application.Application;

public class Runner {
public static void main(String[] args) {
new Thread(() -> new MyServer()).start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
new Thread(() -> Application.launch(Client.class, args)).start();
}
}
24 changes: 24 additions & 0 deletions homework7/src/hw7/client/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package hw7.client;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Client extends Application {

@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

stage.setScene(new Scene(root));
stage.setTitle("ChatWindow");
stage.show();
}

public static void main(String[] args) {
launch(args);
}

}
23 changes: 23 additions & 0 deletions homework7/src/hw7/client/FXMLDocument.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="256.0" prefWidth="348.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/2.2" fx:controller="hw7.client.FXMLDocumentController">
<children>
<VBox layoutX="0.0" layoutY="0.0" prefHeight="219.0" prefWidth="348.0">
<children>
<TextArea fx:id="TextArea" editable="false" prefHeight="219.0" prefWidth="348.0" promptText="" wrapText="true" />
</children>
</VBox>
<HBox layoutX="0.0" layoutY="219.0" prefHeight="37.0" prefWidth="348.0">
<children>
<TextField fx:id="TextField" onAction="#sendMsg" prefHeight="37.0" prefWidth="288.0" promptText="Введите текст" />
<Button fx:id="sendButton" mnemonicParsing="false" onAction="#sendMsg" prefHeight="37.0" prefWidth="99.0" text="Отправить" />
</children>
</HBox>
</children>
</AnchorPane>
67 changes: 67 additions & 0 deletions homework7/src/hw7/client/FXMLDocumentController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package hw7.client;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.*;
import java.net.*;
import java.io.*;

public class FXMLDocumentController implements Initializable {

private Socket socket;

private DataInputStream in;
private DataOutputStream out;

@FXML
private Button sendButton;

@FXML
private TextArea TextArea;

@FXML
private TextField TextField;

@FXML
private void sendMsg(ActionEvent event) {
String str = TextField.getText();
try
{
out.writeUTF(str);
TextField.clear();
TextField.requestFocus();
}
catch (IOException ex)
{
ex.getStackTrace();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {

try {
socket = new Socket("localhost", 12345);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());

new Thread(() -> {
try {
while(true)
{
String str = in.readUTF();
TextArea.appendText(str + "\n");
}
}
catch (IOException ex) { ex.printStackTrace(); }
}).start();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
86 changes: 86 additions & 0 deletions homework7/src/hw7/server/ClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package hw7.server;

import java.net.*;
import java.io.*;

public class ClientHandler {

private MyServer server;
private Socket socket;
private DataInputStream in;
private DataOutputStream out;
private String clientName;

private static int counter = 0;

public ClientHandler(MyServer server, Socket socket)
{
try{
counter++;
this.clientName = "user" + Integer.toString(counter);
this.server = server;
this.socket = socket;
this.in = new DataInputStream(socket.getInputStream());
this.out = new DataOutputStream(socket.getOutputStream());


new Thread(()-> {

try
{
while(true)
{
String str = in.readUTF();

System.out.println("A message from a client: " + str);

if(str.equalsIgnoreCase("/end"))
{ break; }
if(str.startsWith("/w"))
{
String to = str.split(" ")[1];
String msg = str.split(" ")[2];
server.wisperMsg(this, to, msg);
} else {
server.broadcastMsg("[" + this.clientName + "] " + str);
}
out.flush();
}
}
catch(IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{ in.close(); }
catch(IOException ex) { ex.printStackTrace(); }
try
{ out.close(); }
catch(IOException ex) { ex.printStackTrace(); }
try
{ socket.close(); }
catch(IOException ex) { ex.printStackTrace(); }

server.remove_client(this);
}

}).start();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}

public String getClientName() {
return this.clientName;
}

public void sendMsg(String msg)
{try
{ out.writeUTF(msg); }
catch(IOException ex){ex.printStackTrace(); }
}
}
53 changes: 53 additions & 0 deletions homework7/src/hw7/server/MyServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package hw7.server;

import java.net.*;
import java.io.*;
import java.util.Vector;
public class MyServer {

private Vector<ClientHandler> clients;

public MyServer()
{
try
{ ServerSocket serv_socket = new ServerSocket(12345);
clients = new Vector<>();

while(true) {
System.out.println("Waiting for a new client!");
Socket socket = serv_socket.accept();

ClientHandler cl = new ClientHandler(this, socket);
add_client(cl); }
}
catch(IOException ex) { ex.printStackTrace(); }
}

public void add_client(ClientHandler client)
{
clients.add(client);
}

public void remove_client(ClientHandler client)
{
clients.remove(client);
}

public void broadcastMsg(String msg)
{
for(ClientHandler client: clients) {
client.sendMsg(msg);
}
}

public void wisperMsg(ClientHandler from, String to, String msg)
{
for (ClientHandler client: clients) {
if(client.getClientName().equals(to)) {
client.sendMsg("[W from: " + from.getClientName() + "] " + msg);
break;
}
}
from.sendMsg("[W to: " + to + "] " + msg);
}
}