Skip to content
This repository was archived by the owner on Aug 6, 2024. It is now read-only.

Commit 4686364

Browse files
committed
udp protocol
1 parent b87a9f4 commit 4686364

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

build

100644100755
File mode changed.

src/main/java/ir/xenoncommunity/MainRunner.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,27 @@ public MainRunner(CommandLineParser parser) {
2323

2424
public void run() {
2525
this.isDebug = Main.runner.parser.get("--debug", Boolean.class);
26-
val ip = Main.runner.parser.get("--ip", String.class);
26+
val ip = Main.runner.parser.get("--ip", String.class);
2727
val port = Main.runner.parser.get("--port", Integer.class);
2828
val maxThreads = Main.runner.parser.get("--threads", Integer.class);
2929
val isResult = Main.runner.parser.get("--sendResult", Boolean.class);
3030
val isKeepAlive = Main.runner.parser.get("--keepAlive", Boolean.class);
31+
val udp = Main.runner.parser.get("--udp", Boolean.class);
3132
if(this.isDebug()){
3233
getLogger().setSection("DEBUG");
3334
getLogger().print(Logger.LEVEL.INFO, "ip is: " + ip);
3435
getLogger().print(Logger.LEVEL.INFO, "port is: " + port);
3536
getLogger().print(Logger.LEVEL.INFO, "maxThreads is: " + maxThreads);
3637
getLogger().print(Logger.LEVEL.INFO, "isResult is: " + isResult);
3738
getLogger().print(Logger.LEVEL.INFO, "isKeepAlive is: " + isKeepAlive);
39+
getLogger().print(Logger.LEVEL.INFO, "udp is: " + udp);
3840
}
3941
for(int i = 0; i <= maxThreads; i++){
4042
if(this.isDebug)
4143
getLogger().print(Logger.LEVEL.INFO, "adding new thread. max: " + maxThreads);
4244
getTaskManager().add(new Thread(() -> {
4345
while (true)
44-
socketUtils.connect(ip, port, isResult, isKeepAlive);
46+
socketUtils.connect(ip, port, isResult, isKeepAlive, udp);
4547
}));
4648
}
4749
getLogger().print(Logger.LEVEL.INFO, "doing tasks...");

src/main/java/ir/xenoncommunity/utils/CommandLineParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public CommandLineParser(String[] args) {
1313
@Nullable
1414
public <T> T get(String arg, Class<T> type) {
1515
for (int i = 0; i < args.length ; i++) {
16-
if(args[i + 1].startsWith("-"))
16+
if(args[i].equals(arg) && args[i + 1].startsWith("-"))
1717
return (T) Boolean.TRUE;
1818
else if (args[i].equals(arg))
1919
return convertToType(args[i + 1], type);
Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
package ir.xenoncommunity.utils;
22

33
import ir.xenoncommunity.Main;
4-
import lombok.SneakyThrows;
54
import lombok.val;
5+
6+
import java.net.DatagramSocket;
7+
import java.net.InetAddress;
68
import java.net.Socket;
79

810
public class SocketUtils {
9-
@SneakyThrows
10-
public void connect(final String ipIn, final int port, final boolean sendResult, final boolean keepAlive){
11-
try(val socket = new Socket(ipIn, port)){
12-
Main.runner.getLogger().setSection("CONNECT");
13-
if (sendResult && socket.isConnected()) {
14-
Main.runner.getLogger().print(Logger.LEVEL. INFO, String.format("Connected! ip: %s, port: %s, threads: %s\n", ipIn, port, Main.runner.getTaskManager().tasks.size()));
15-
}
16-
if (keepAlive) {
17-
socket.setKeepAlive(true);
18-
} else{
19-
socket.close();
11+
public void connect(final String ipAddress, final int port, final boolean sendResult, final boolean keepAlive, final boolean udp) {
12+
Main.runner.getLogger().setSection("CONNECT");
13+
try {
14+
if (udp) {
15+
if (keepAlive) {
16+
Main.runner.getLogger().print(Logger.LEVEL.ERROR, "Can't use keepAlive for UDP protocol.");
17+
throw new IllegalStateException("UDP is used with keepAlive.");
18+
}
19+
try (val socket = new DatagramSocket(port, InetAddress.getByName(ipAddress))) {
20+
if (sendResult && socket.isConnected()) {
21+
Main.runner.getLogger().print(Logger.LEVEL.INFO, String.format("Connected! ip: %s, port: %s, threads: %s\n", ipAddress, port, Main.runner.getTaskManager().tasks.size()));
22+
}
23+
}
24+
} else {
25+
try (val socket = new Socket(ipAddress, port)) {
26+
if (sendResult && socket.isConnected()) {
27+
Main.runner.getLogger().print(Logger.LEVEL.INFO, String.format("Connected! ip: %s, port: %s, threads: %s\n", ipAddress, port, Main.runner.getTaskManager().tasks.size()));
28+
}
29+
if (keepAlive) {
30+
socket.setKeepAlive(true);
31+
} else
32+
socket.close();
33+
}
2034
}
35+
} catch (final Exception e) {
36+
Main.runner.getLogger().print(Logger.LEVEL.ERROR, e.getMessage());
2137
}
2238
}
2339
}

0 commit comments

Comments
 (0)