Skip to content

Commit

Permalink
[CHORE]: Shown inet address in the server side terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmjr committed May 8, 2024
1 parent 74482cf commit f1fd39b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.mjrt.terminal.localchat.localserver;

import com.mjrt.terminal.localchat.Connection;
import com.mjrt.terminal.localchat.Messenger;import java.io.IOException;
import com.mjrt.terminal.localchat.Messenger;
import com.mjrt.terminal.localchat.util.IpManager;

import java.io.IOException;

import static com.mjrt.terminal.localchat.Options.printMessageL;

Expand All @@ -13,7 +16,7 @@ private Server() {}
public void createConnection(int port) throws IOException {
initializeAttributes();
var serverSocket = Connection.createConnection(port);
printMessageL("Serving in ip: " + serverSocket.getInetAddress().getHostAddress() + ", port: " + port + "\n");
printMessageL("Serving in ip: " + IpManager.getInetAddress() + ", port: " + port + "\n");
socket = serverSocket.accept();
bindMessageObtainer();
bindMessageSender();
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/mjrt/terminal/localchat/util/IpManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mjrt.terminal.localchat.util;

import org.jetbrains.annotations.Nullable;

import java.net.NetworkInterface;
import java.net.SocketException;

public class IpManager {
public static @Nullable String getInetAddress() {
try {
for (var en= NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
var networkInterface = en.nextElement();
if (networkInterface.getName().contains("wlan") || networkInterface.getName().contains("ap")) {
for (var enumInetAddresses = networkInterface.getInetAddresses();enumInetAddresses.hasMoreElements();) {
var inetAddress = enumInetAddresses.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress.getAddress().length == 4)
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
System.err.println("Error: " + e.getMessage());
}
return null;
}
}

0 comments on commit f1fd39b

Please sign in to comment.