Index, added simple code for connection.
This commit is contained in:
39
.gitignore
vendored
Normal file
39
.gitignore
vendored
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
.kotlin
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
10
.idea/.gitignore
generated
vendored
Normal file
10
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Ignored default folder with query files
|
||||||
|
/queries/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
7
.idea/encodings.xml
generated
Normal file
7
.idea/encodings.xml
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
14
.idea/misc.xml
generated
Normal file
14
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="ms-21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
17
pom.xml
Normal file
17
pom.xml
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>pl.mrusek</groupId>
|
||||||
|
<artifactId>SimpleChat</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
84
src/main/java/ChatServer.java
Normal file
84
src/main/java/ChatServer.java
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.net.ServerSocket;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
|
||||||
|
public class ChatServer implements Runnable {
|
||||||
|
private int port = 9000;
|
||||||
|
private ArrayList<Socket> clientSockets = new ArrayList<Socket>();
|
||||||
|
|
||||||
|
public ChatServer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ChatServer(int port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
private User getOrCreateUser(String username) {
|
||||||
|
return new User(username);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SendMessage(User user, String msg) {
|
||||||
|
try {
|
||||||
|
for (Socket client : clientSockets) {
|
||||||
|
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
|
||||||
|
|
||||||
|
out.println(user.getUsername() + " " + msg);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleClient(Socket socket) {
|
||||||
|
try {
|
||||||
|
clientSockets.add(socket);
|
||||||
|
|
||||||
|
String msg;
|
||||||
|
User user = null;
|
||||||
|
|
||||||
|
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||||
|
|
||||||
|
out.println("Login: /login username");
|
||||||
|
while ((msg = in.readLine()) != null) {
|
||||||
|
if (user != null) {
|
||||||
|
SendMessage(user, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
if (msg.toLowerCase().startsWith("/login")) {
|
||||||
|
String username = msg.substring(6).strip();
|
||||||
|
user = getOrCreateUser(username);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
clientSockets.remove(socket);
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
ServerSocket serverSocket = new ServerSocket(port);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
Socket clientSocket = serverSocket.accept();
|
||||||
|
|
||||||
|
Thread thread = new Thread(() -> handleClient(clientSocket));
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/main/java/Client.java
Normal file
62
src/main/java/Client.java
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
private Socket clientSocket;
|
||||||
|
private PrintWriter out;
|
||||||
|
private BufferedReader in;
|
||||||
|
|
||||||
|
public void startConnection(String ip, int port) throws IOException {
|
||||||
|
clientSocket = new Socket(ip, port);
|
||||||
|
out = new PrintWriter(clientSocket.getOutputStream(), true);
|
||||||
|
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendMessage(String message) throws IOException {
|
||||||
|
out.println(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String loadMessage() throws IOException {
|
||||||
|
String resp = in.readLine();
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopConnection() throws IOException {
|
||||||
|
in.close();
|
||||||
|
out.close();
|
||||||
|
clientSocket.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws IOException {
|
||||||
|
Client client = new Client();
|
||||||
|
client.startConnection("127.0.0.1", 9000);
|
||||||
|
|
||||||
|
String msg;
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
try {
|
||||||
|
String resp;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
resp = client.loadMessage();
|
||||||
|
System.out.println(resp);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
msg = scanner.nextLine();
|
||||||
|
client.sendMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// client.stopConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/main/java/Main.java
Normal file
10
src/main/java/Main.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ChatServer chatServer = new ChatServer(9000);
|
||||||
|
|
||||||
|
|
||||||
|
Thread serverThread = new Thread(chatServer);
|
||||||
|
serverThread.start();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/main/java/User.java
Normal file
16
src/main/java/User.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
public class User {
|
||||||
|
|
||||||
|
private String username;
|
||||||
|
|
||||||
|
public User(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user