Jetty9 multiple listeners - jetty

I am trying to port my Java app which uses Jetty 8 to Jetty 9.
With Jetty8 I was able to instantiate 2 jetty "servers" listening on different ports, however with Jetty9 only the first server will listen to the port.
// first server
Server server1 = new Server();
// add http + port 80
server1.start();
server1.join();
// second server
Server server2 = new Server();
// add http + port 8080
server2.start();
server2.join();
it seems server2 is never started, do I need to use some kind of threading with Jetty9?

server1.join() will block until the server is stopped! So the code for server2 is never being executed until you stop server1.
Move the server1.join() to server2.join() (to the end) and it should work as expected:
// first server
Server server1 = new Server();
// add http + port 80
server1.start();
// second server
Server server2 = new Server();
// add http + port 8080
server2.start();
server1.join();
server2.join();

Related

Two process bind and listen on the same address and port, but how to make sure both of them can receive the incoming message?

I have a server application, part of its code like this:
socket = socket(...); // create a socket
setsockopt(RE_USEADDR); // set the socket option RE_USEADDR
bind(socket, 127.0.0.1, 8080); // bind the socket to the IP address and Port
listen(); // listen
Here is the question:
I launch the server application twice, on the same machine, same user.
Both of them can work well.
Launch the client application, which connects to 127.0.0.1:8080
Send some contents to the server.
However only one of the server application can receive the message from client.
Is there anyway to make sure the two server applications all receive the message from client.
If there is, please explain how and why in TCP and UDP.
Thank you very much

What ip should I connect socket to when communicating between two machines(ZeroMQ)

What IP adress should be used to connect to server running on my pc from other machines? In my server, I call zmq_bind to listen for all incoming connections, like this:
zmq_bind(socket, "tcp://*:12781");
In my client, I try to connect to the server by:
string IP = "127.0.0.1"; //what should be here?
string connection_string = "tcp://" + IP + ":12781";
zmq_connect(socket, connection_string);
My program works already when the server and client are running on the same computer(IP is 'localhost', '127.0.0.1' or '192.168.1.13'). What IP should I use when connecting from other computers? I already tried my IPV4 which I found here, it didn't work.
Thanks for help.

How to run a socket and a websocket server on the same port?

I'm working on a server, which is listening on the port 80
I would like to enable both native and websocket clients to connect to my server.
It works only, if websockify runs at a different port, and forwards the trafic to the socket server.
Unfortunately websockify isn't well documented, and there are no tutorials available.
Where should I start, if I would like to create a single server only, which is listening on only one port, and accepts both websocket and native TCP sockets?
If your server is listening for connections on port 80, is it talking http? Because if not, don't be listening on port 80: Port 80 is well established as carrying http traffic.
Next - an ipaddress and port together are the unique identifiers of an endpoint. If a remote client connects to your server on port 80, other than the destination ip and port there is no other information that the networking layer has to identify which application, listening on port 80, deserves the packet. Given that provisioning multiple ip addresses is quite hard - impossible over a NAT - the only information thats really available to route the packet to the correct listener is the port. So you just can not have two applications listening on the same port.
Lastly websockets behave like native sockets, AFTER an initial HTTP negotiation. This means that, instead of using websocksify, you could teach your native server application to detect an attempt to connect by a websocket client and optionally perform the initial negotiation before going into 'native' mode.
Writing Websocket Servers gives a brief breakdown of what your native server would need to implement.
If you take a look of WebSocket, you will see that it's a protocol over TCP layer. Thus, your server socket can bind only once to the port 80 and it's up to you either you will use plain TCP, WebSocket or your custom protocol. There is no magic which enables switching from WebSocket to TCP and vice versa.

Multiple client program

I was wondering if I have a single server application and a bunch of clients connected. What if the first client asks for some data and then in the middle of that the second client asks for data. What would happen? Will the first client connection be broken?
I am using IndyServer Indy Client TCP.
A TCP server opens a port where clients can connect. A connection is defined by the four components client IP / client port / server IP / server port.
Indy TCP servers process connections in separate threads. So as long as your connection processing code is thread-safe, the processing will be separated and the threads will not interfere with each other.

Running client and server on same machine

I have both a client and server application using UDP port 25565.
In order to run these on the same machine, because only one application may bind itself to port 25565, does this mean that it is necessary for me to use two separate ports for transmitting data between the applications?
What I have in mind is the following -
Client -> 25565 -> Server
Client <- 25566 <- Server
Is this the only solution or is there another way of handling this?
Your server application open a port and wait for client to connect.
Client need to know this port in advance so it can establish a connection to the desired service.
Client can use any available ports to initiate this connection (better to use ports > 1000).
The server sees in the incomming packet wich port the client is using, so it will send anwser to it. No need to specify it in your design.
After handshake the TCP/IP connection is then identified by these 4 values : server IP, server port, client IP, client port.
No other connection could have the same four values.
To answer your question. A TCP/IP connection is bi-directional, once established, the server can send data to the client and the other way around.
I would draw the scheme like this :
SERVER port 25565 <-> CLIENT port 25566 (or any other port)
Well, no. Only the server needs to listen on the port 25565 - the client will just connect to that port. There is no reason to specify which client the port should 'use' to connect to that port. Also, once the server has accepted the connection, the port can listen for other requests.
The whole point of separate UDP ports is to eliminate conflicts among applications listening to incoming packets. Changing one of these ports is probably the best solution.
However, if you really want both programs to listen on the same port you will need to use virtual network interfaces such as TUN/TAP (there is a Windows port). Then both applications will bind to the port with tha same number but on the different network interfaces.