How can we get port 8080 from adempiere so as to give it dynamically - sockets

I have a message sending program from server to client which is working inside adempiere. Here I have to give supply port: 8080 dynamically, ie. port must not be hardcoded. Now I am hard coding port 8080 at serversocket and socket
Server
ServerSocket srvr = new ServerSocket(8080, 1, InetAddress.getByName(mSession.getRemote_Addr()));
Client
Socket skt = new Socket(ip.getHostAddress(), 8080);
Please suggest a method rebel to this hard coding.
Please help me.

The web port is part of the configuration data that is used when the setup process is run but it isn't accessed by the server/client once the setup is complete. To access the data, you will need to load the configuration data again like this:
int webPort = 8080;
ConfigurationData data = new ConfigurationData(null);
if (data.load()) {
webPort = data.getAppsServerWebPort ();
}
ServerSocket srvr = new ServerSocket(webPort, 1, InetAddress.getByName(mSession.getRemote_Addr()));

Related

WiFiP2PManager Connect WiFiP2PConfig - pass port number to client?

I've implemented a Xamarin app that successfully peers to another instance of itself running on another device using WiFiP2PManager, OnPeersAvailable and OnConnectionInfoAvailable (etc.).
My challenge now is I'd like the group owner to specify the PORT the client(s) will use to connect.
SERVER
var serverSocket = new ServerSocket(port); <<< PASS THIS PORT NUMBER TO THE CLIENT.
client = serverSocket.Accept();
CLIENT
client = new Socket();
InetSocketAddress socketAddress = new InetSocketAddress(address, port); <<< PORT PASSED FROM THE SERVER
client.Connect(socketAddress);
Is there any way I can pass additional information to the peer that it can use for connection, such as the port number?
Thanks
-John

What is the difference between the Source Port and the StunServerPort

I am developing a peer to peer call. I am using de.javawi.jstun.test .
I found this constructor in de.javawi.jstun.test.DiscoveryTest .
public DiscoveryTest(InetAddress sourceIaddress, int sourcePort, String stunServer, int stunServerPort) {
this.sourceIaddress = sourceIaddress;
this.sourcePort = sourcePort;
this.stunServer = stunServer;
this.stunServerPort = stunServerPort;
}
My question is What is the difference between the Source Port and the StunServerPort??
stunServerPort is the port the STUN server listens on for incoming binding requests. This is typically one of the standard STUN ports: 3478 or 3479.
sourcePort is the port the client behind a NAT has obtained locally to create a socket with. Most often, the client attempting to do P2P will ask the OS to randomly pick an available local port to send/receive from. You can probably pass 0 for sourcePort and let it pick the port for you as well. Or if you already have a socket, use the same port as your local, and DiscoveryTest will set the reuseaddr flag so it can have a socket co-exist.

I can't get the client script to connect to the localhost server, the TCP connection never happens

I wrote a simple client side program that creates a socket using
CFSteamCreatePairWithSocketToHost function
and connects to the server that runs on the local host on port 8080. It creates the socket just fine but it never connects to the server. I wrote the server in C. It didn't work and gave me a
kCFErrorDomainCFNetwork error 72000
and the only information that relays is that apparently the TCP connection couldn't be made don't know why though. So I tried to write the client side script in C too and added it to my Swift project bridging header and all but it still doesn't connect. It creates the socket just fine but it fails to connect to the server and I have no idea why.
But the same C client script worked when I compiled it using clang and ran it but didn't connect when I ran it with my swift project in Xcode. Is my mac blocking the libraries from making a TCP connection or something?
I don't even know what to search for. The only thing I found was an issue on a Github library called starscream which had the same errors I had and I'm not even using that library and the reply there was "the only thing we can discern from this error is that the TCP connection was unsuccessful".
Here's the code I used to connect to the server using Swift 4. The server is running on port 8080 on localhost.
class client:NSObject {
var inputstream = InputStream!
var outputstream = OutputStream!
func setupNetworkCom() {
var readstream = Unmanaged<CFReadStream>?
var writestream = Unmanaged<CFWriteStream>?
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, "localhost" as CFString, 8080, &readstream, &writestream)
inputstream = readstream!.takeRetainedValue()
outputstream = writestream!.takeReatainedValue()
inputstream.schedule(in: .current, forMode: .common)
outputstream.schedule(in: .current, forMode: .common)
inputstream.open()
outputstream.open()
}
}
I've also tried replacing "localhost" with "127.0.0.1" which is the IP I specified for the server to run on but it still doesn't work.
click on your project settings and go to capabilities there you'll see the app sandbox. make sure it's turned on and then enable incoming connections and outgoing connections.

LibGDX: Error making a socket connection to *ip-adress*

I want to make 2 devices communicate via sockets.
I use this code for the client socket:
Socket socket = Gdx.net.newClientSocket(Net.Protocol.TCP, adress, 1337, socketHints);
(SocketHints: timeout = 4000)
I get a GdxRuntimeException each time this line is being executed. What is wrong with the socket?
Screenshot of stack trace
You get that message because the socket couldn't be opened.
Note the last line about the return in the API:
newClientSocket:
Socket newClientSocket(Net.Protocol protocol,
java.lang.String host,
int port,
SocketHints hints)
Creates a new TCP client socket that connects to the given host and port.
Parameters:
host - the host address
port - the port
hints - additional SocketHints used to create the socket. Input null to use the default setting provided by the system.
Returns:
GdxRuntimeException in case the socket couldn't be opened
Try doing some debugging to find out why you are getting this error.
Is the port already in use? Are you trying to open more than one connection on the same port? Is the server IP valid? Maybe something else is causing the issue?

Setup a datastreaming server in processing

I want to setup a datastreaming server in Processing, so the Client sends a String to the Server and the Server answeres it. For example Client - Server "Cupcake" then Server - Client "Cupcakce sounds funny" so the Server answeres the string. I tried this with the UDP library and opened the port on the server. But when the server had to answer the Clinet it did'nt work, because I can't open the client's ports. Any solutions?
Sounds like you need two-way communication.
Using UDP you would need two sketches that are both UDP servers and clients.
e.g.
sketch #1 listens on port 12000
sketch #1 sends data on port 12001
sketch #2 listens on port 12001
sketch #2 sends data on port 12000
You can also use TCP sockets.
As the Server you can use Examples > Libraries > Network > ChatServer
I'm surprised there's no ChatClient example, but you can get away with something like this:
import javax.swing.*;
import processing.net.*;
int port = 10002;
Client myClient;
void setup()
{
size(400, 400);
textFont(createFont("SanSerif", 16));
myClient = new Client(this, "localhost", port); // Starts a client on port 10002
background(0);
}
void draw()
{
background(0);
text("client - press ENTER to type\nconnected:"+myClient.active(), 15, 45);
}
void keyReleased() {
if (keyCode == ENTER) {
String message = JOptionPane.showInputDialog(null, "message: ", "TCP Client messaging", JOptionPane.QUESTION_MESSAGE);
println(message);
if (myClient.active() && message != null) {
myClient.write(message);
}
}
}
Note: The server must be running before the client so the client can connect.
Be sure to checkout the difference between UDP and TCP protocols to work out which one makes most sense to use in your case (especially if you pan to use more clients).
Another option worth looking into is WebSockets. This would allow you to have a WebSocket server in Processing and the client could either be another Processing sketch or simply any browser with WebSocket support(e.g. most modern)