Connect Server to multiple clients - java

I have created a client/server application. Now, clients can send requests to the server and receive server response. Now I want to notify all connected clients to perform some actions. How can I do that?

Register all connected clients on the server and store references to clients in the List instance.
If you want to inform your clients asynchronously then open ServerSocket instance on your every client and accept connections only from the server they are using.
And in this case your server acts as a client and sends a request to all connected clients like a client sends request to the server.

Related

Java multiple connection to server in dynamic ports

I need to have a UDP server which allow me to receive/send informations from/to clients which dynamically will open a socket with a free port (so it will be differente from device and device). The client will send and receive in the same port, so the server must be able to communicate with it.
how could I set the server to stay open in every port? if I had 250 thousand users how could I handle them without tails problem and preventing the port to be occupied from another client?
I thought about open every port with different sockets in a different thread, but I don't know if this is a correct way.
A UDP Server can listen and be open on only one port. All clients can send data to that port. The server will have to handle each data and respond if needed to the peer who sent its data. This should happen even if more than one client wish to send data to server. In UDP context one client will not hog the server port.(Unless the application is badly written).

data sending server to different client data

I am doing a project of Restaurant ordering system in that I gave order by using different client and process that order and that order can send different system, e.g:
kitchen1
Kitechn2, and
kitchen3
First I can create different client by using rmi threading concept an and they can send (means all client) data to my server i want to sent that data into different client
How can I do that? I do create different client and send data to the server suggest how can I do that?
RMI is a synchronous (request/response) protocol - the client sends a request to the server, which it can respond to.
The server cannot arbitrarily send more data to the client.
The simplest way to asynchronous communication in Java via JMS using a message broker like Active MQ.
The process would go something like this:
the server starts and connects to its incoming request queue.
client 1 creates a temporary queue and registers with the server via the request queue passing the name of its temporary queue.
the server stores the client and the name of its termporary queue.
client 2 does the same and server stores the client and the name of its termporary queue.
client 1 sends a message to the server causing the server to send a message to client 2, which is does via the temporary queue that client 2 registered with the server.
client 2 reponds to the server causing the server to send a message to client 1, which is does via the temporary queue that client 1 registered with the server.
This can go on until one or both client shuts down, at which point their temporary queue are closed and the server can no longer send messages to that client (though it's best the client de-registers itself).

Communication between two clients of web application

Let's say I want to send a message from one client to another. How should I approach this problem? Obviously I will have to send this message to server, but what's next? I have few ideas, but every idea seems to be wrong.
thanks
Client1 - send message for client2 to server
Client2 - check any period of time for the messages
OR
Client2 - Open Websocket to the server.
Client1 - send message for client2 to server
Server - push message to client2
direct client-to-client communication my be very difficult due to client firewalls.
Look at the tutorial for sockets in java
http://docs.oracle.com/javase/tutorial/networking/sockets/
Also you don't necessarily need a server. You can have the clients have both an incoming and outgoing channel and do it that way.
So
Client1 sends on its outgoing to Client2's incoming
Client2 hears on its incoming and responds on its outgoing to Client1's incoming
Client1 hears on its incoming
You can use the standard Java JMS approach to send asynchronous messages between applications. Read more at : http://java.sun.com/developer/technicalArticles/Ecommerce/jms/
If by web application you mean HTTP based, than you must know HTTP is a request based protocol. In other words, the server only responds to HTTP requests that come from the clients (browsers, most of the time), so after a client sends a message, all the other clients that want to receive that message must ask for it, i.e. make a request to the server. Typically, this is achieved using an auto-refreshed HTML page.

Java socket programming: Is it possible for the server socket to initiate a communication?

1, my server has a ServerSocket to accept client connection
2, my client create a socket and and send message to the server.
3, the server gets the message and reply to the client.
So it's always the client that initiate the communication.
Say some time later after initial client connection, the server wants to send another message to the client, is it possible to do so?
If you keep the socket connection from the client open on the server yes. The client would need to be always listening for data coming in.
Otherwise the client will have to poll in intervals to the server if the connection on the server is closed after a message is sent.
The client must always initiate the connection, that's pretty much the only distinction between a server and a client (a program can be both be a server and a client).
However once a connection is established, as long as it's not closed either side can send data to the other (assuming the other side is listening for data).

client server interaction question

I am trying to implement a minimal chat server in java over regular TCP protocol. The chat server will listen on a specific port. The question I have is if there are multiple clients sending messages to the same port, can the server distinguish between the clients and respond to each individually if the messages do not contain the IP address or destination name of the client?
to make my question a bit more clear, suppose the server gets a packet that contains only
"user: abc to-user:efg message:"Hello""
Can I find out in java the address of the client who sent the packet and respond back to the same address or will I need to include some identifier in the message itself like "sender-ip = 1.1.1.1"
Multiple clients will never send data over the same port. The only time your clients will talk over the same port is when they will connect to the server. In the server, whenever the ServerSocket receives a connection it returns a new Socket. This socket is a combination of the following : Server IP+ServerPort and Client IP+Client Port. The Server IP and the Server Port will be same for each socket; what differs is the client IP and Port. Usually this socket is passed to a new thread for further communication while the ServerSocket goes back to listen to incoming connections. Once you have a reference to the socket you can call socket..getInetAddress().getHostAddress() to get the remote IP and socket.getPort() to get the port of the respective client.
Yes, each connection will be separate - you'll have a different stream to read from for each connection. It's up to you to associate the relevant user information with the connection though.

Categories