refactor: add arguments support to run client or server
This commit is contained in:
28
.project
Normal file
28
.project
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>ClientServer</name>
|
||||||
|
<comment>Project ClientServer created by Buildship.</comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1743445961452</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
||||||
13
.settings/org.eclipse.buildship.core.prefs
Normal file
13
.settings/org.eclipse.buildship.core.prefs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
arguments=--init-script /home/xeon0x/.var/app/dev.zed.Zed/data/zed/extensions/work/java/jdtls/jdt-language-server-1.46.0-202503271314/configuration/org.eclipse.osgi/58/0/.cp/gradle/init/init.gradle
|
||||||
|
auto.sync=false
|
||||||
|
build.scans.enabled=false
|
||||||
|
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
||||||
|
connection.project.dir=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
gradle.user.home=
|
||||||
|
java.home=/usr/lib64/jvm/java-21-openjdk-21
|
||||||
|
jvm.arguments=
|
||||||
|
offline.mode=false
|
||||||
|
override.workspace.settings=true
|
||||||
|
show.console.view=true
|
||||||
|
show.executions.view=true
|
||||||
36
README.md
36
README.md
@@ -1,3 +1,39 @@
|
|||||||
|
# ClientServer
|
||||||
|
|
||||||
|
## Build
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Run
|
||||||
|
|
||||||
|
### Client
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew run --args='client'
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew runServer
|
||||||
|
```
|
||||||
|
|
||||||
|
### Server
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew run --args='server'
|
||||||
|
```
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew runServer
|
||||||
|
```
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
```mermaid
|
```mermaid
|
||||||
sequenceDiagram
|
sequenceDiagram
|
||||||
Note over Client, Serveur: DatagramSocket("localhost", 66666)
|
Note over Client, Serveur: DatagramSocket("localhost", 66666)
|
||||||
|
|||||||
19
app/.classpath
Normal file
19
app/.classpath
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="bin/main" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="gradle_scope" value="main"/>
|
||||||
|
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="bin/test" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="gradle_scope" value="test"/>
|
||||||
|
<attribute name="gradle_used_by_scope" value="test"/>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21/"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
|
||||||
|
<classpathentry kind="output" path="bin/default"/>
|
||||||
|
</classpath>
|
||||||
34
app/.project
Normal file
34
app/.project
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>app</name>
|
||||||
|
<comment>Project app created by Buildship.</comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1743445961456</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
||||||
2
app/.settings/org.eclipse.buildship.core.prefs
Normal file
2
app/.settings/org.eclipse.buildship.core.prefs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
connection.project.dir=..
|
||||||
|
eclipse.preferences.version=1
|
||||||
@@ -40,3 +40,19 @@ tasks.named('test') {
|
|||||||
// Use JUnit Platform for unit tests.
|
// Use JUnit Platform for unit tests.
|
||||||
useJUnitPlatform()
|
useJUnitPlatform()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
task runServer(type: JavaExec) {
|
||||||
|
description = 'Runs the server'
|
||||||
|
group = 'application'
|
||||||
|
mainClass = application.mainClass
|
||||||
|
classpath = sourceSets.main.runtimeClasspath
|
||||||
|
args = ['server']
|
||||||
|
}
|
||||||
|
|
||||||
|
task runClient(type: JavaExec) {
|
||||||
|
description = 'Runs the client'
|
||||||
|
group = 'application'
|
||||||
|
mainClass = application.mainClass
|
||||||
|
classpath = sourceSets.main.runtimeClasspath
|
||||||
|
args = ['client']
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package clientserver;
|
package clientserver;
|
||||||
|
|
||||||
|
import clientserver.client.Client;
|
||||||
|
import clientserver.server.ClientHandler;
|
||||||
|
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public String getGreeting() {
|
public String getGreeting() {
|
||||||
@@ -7,8 +10,28 @@ public class App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(new App().getGreeting());
|
if (args.length == 0) {
|
||||||
ClientHandler.main(new String[] {});
|
System.out.println(
|
||||||
// MistralDirectAPI.main(new String[] {});
|
"Please use --args='client' or --args='server'."
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String mode = args[0];
|
||||||
|
|
||||||
|
switch (mode.toLowerCase()) {
|
||||||
|
case "client":
|
||||||
|
// Run the client
|
||||||
|
System.out.println("Starting client...");
|
||||||
|
Client.main(new String[] {});
|
||||||
|
break;
|
||||||
|
case "server":
|
||||||
|
// Run the server
|
||||||
|
System.out.println("Starting server...");
|
||||||
|
ClientHandler.main(new String[] {});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Unknown mode: " + mode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package clientserver;
|
package clientserver.client;
|
||||||
|
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
// 1 - Création du canal avec un port libre
|
// 1 - Création du canal avec un port libre
|
||||||
@@ -16,15 +17,26 @@ public class Client {
|
|||||||
String message = "Hello Server";
|
String message = "Hello Server";
|
||||||
byte[] envoyees = message.getBytes();
|
byte[] envoyees = message.getBytes();
|
||||||
DatagramPacket paquetEnvoye = new DatagramPacket(
|
DatagramPacket paquetEnvoye = new DatagramPacket(
|
||||||
envoyees, envoyees.length, adresseServeur, portServeur);
|
envoyees,
|
||||||
|
envoyees.length,
|
||||||
|
adresseServeur,
|
||||||
|
portServeur
|
||||||
|
);
|
||||||
socketClient.send(paquetEnvoye);
|
socketClient.send(paquetEnvoye);
|
||||||
System.out.println("Message envoyé au serveur");
|
System.out.println("Message envoyé au serveur");
|
||||||
|
|
||||||
// 3 - Recevoir
|
// 3 - Recevoir
|
||||||
byte[] recues = new byte[1024]; // tampon de réception
|
byte[] recues = new byte[1024]; // tampon de réception
|
||||||
DatagramPacket paquetRecu = new DatagramPacket(recues, recues.length);
|
DatagramPacket paquetRecu = new DatagramPacket(
|
||||||
|
recues,
|
||||||
|
recues.length
|
||||||
|
);
|
||||||
socketClient.receive(paquetRecu);
|
socketClient.receive(paquetRecu);
|
||||||
String reponse = new String(paquetRecu.getData(), 0, paquetRecu.getLength());
|
String reponse = new String(
|
||||||
|
paquetRecu.getData(),
|
||||||
|
0,
|
||||||
|
paquetRecu.getLength()
|
||||||
|
);
|
||||||
|
|
||||||
if (reponse.startsWith("PORT:")) {
|
if (reponse.startsWith("PORT:")) {
|
||||||
int newPort = Integer.parseInt(reponse.substring(5));
|
int newPort = Integer.parseInt(reponse.substring(5));
|
||||||
@@ -35,7 +47,11 @@ public class Client {
|
|||||||
String messagePort = "Message au nouveau port";
|
String messagePort = "Message au nouveau port";
|
||||||
byte[] envoyeesPort = messagePort.getBytes();
|
byte[] envoyeesPort = messagePort.getBytes();
|
||||||
DatagramPacket paquetPort = new DatagramPacket(
|
DatagramPacket paquetPort = new DatagramPacket(
|
||||||
envoyeesPort, envoyeesPort.length, adresseServeur, newPort);
|
envoyeesPort,
|
||||||
|
envoyeesPort.length,
|
||||||
|
adresseServeur,
|
||||||
|
newPort
|
||||||
|
);
|
||||||
socketClient.send(paquetPort);
|
socketClient.send(paquetPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package clientserver;
|
package clientserver.common;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package clientserver;
|
package clientserver.server;
|
||||||
|
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
import java.net.DatagramSocket;
|
import java.net.DatagramSocket;
|
||||||
@@ -7,7 +7,7 @@ import java.net.InetSocketAddress;
|
|||||||
|
|
||||||
public class ClientHandler {
|
public class ClientHandler {
|
||||||
|
|
||||||
static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int port = 6666;
|
int port = 6666;
|
||||||
boolean running = true;
|
boolean running = true;
|
||||||
DatagramSocket socketServer = null;
|
DatagramSocket socketServer = null;
|
||||||
@@ -94,43 +94,4 @@ public class ClientHandler {
|
|||||||
System.out.println(e);
|
System.out.println(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// public static void main(String[] args) {
|
|
||||||
// try {
|
|
||||||
// // 1 - Création du canal
|
|
||||||
// DatagramSocket socketServeur = new DatagramSocket(null);
|
|
||||||
// // 2 - Réservation du port
|
|
||||||
// InetSocketAddress adresse = new InetSocketAddress(
|
|
||||||
// "localhost",
|
|
||||||
// 6666
|
|
||||||
// );
|
|
||||||
// socketServeur.bind(adresse);
|
|
||||||
// byte[] recues = new byte[1024]; // tampon d'émission
|
|
||||||
// byte[] envoyees; // tampon de réception
|
|
||||||
// // 3 - Recevoir
|
|
||||||
// DatagramPacket paquetRecu = new DatagramPacket(
|
|
||||||
// recues,
|
|
||||||
// recues.length
|
|
||||||
// );
|
|
||||||
// socketServeur.receive(paquetRecu);
|
|
||||||
// InetAddress adrClient = paquetRecu.getAddress();
|
|
||||||
// int prtClient = paquetRecu.getPort();
|
|
||||||
// System.out.println(
|
|
||||||
// "Nouveau client : @" + adrClient + ":" + prtClient
|
|
||||||
// );
|
|
||||||
// // 4 - Émettre
|
|
||||||
// String reponse = "Serveur RX302 ready";
|
|
||||||
// envoyees = reponse.getBytes();
|
|
||||||
// DatagramPacket paquetEnvoye = new DatagramPacket(
|
|
||||||
// envoyees,
|
|
||||||
// envoyees.length,
|
|
||||||
// adrClient,
|
|
||||||
// prtClient
|
|
||||||
// );
|
|
||||||
// socketServeur.send(paquetEnvoye);
|
|
||||||
// // 5 - Libérer le canal
|
|
||||||
// socketServeur.close();
|
|
||||||
// } catch (Exception e) {
|
|
||||||
// System.err.println(e);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user