Client to specific client communication through a server - java

I have successfully implemented a server -client application where i have maintained a list of clients connected to the server in an array.What i do not understand is how will the client tell the server about the client the message is sent for .Like if server A has 3 clients connected lets say a,b and c.Now if a wants to send a message to c how will it tell the server about it?
i want the client to send a request to server asking to connect to client c ,the server asks client c for permission and if c accepts a and c can send messages back and forth until one of them disconnects

For IM (Instant Messaging), you can look at XMPP Servers and clients, which are very popular in chat application, even gtalk works on them.
XMPP Server : jabber server.

The common practice is exchanging all the messages (in a given protocol) through a server and which intermediates everything. If you want direct communication between the clients, take a look at p2p or peer-to-peer (http://en.wikipedia.org/wiki/Peer-to-peer).
Your question is very similar to this one: Client-Server-Client communication using Sockets

Related

Seeing multiple servers on a network

I am busy with a project where I'm creating a basic client/server chat application which allows a user to create a server on their local network on a port of their choice and then have multiple clients connect to that server by specifying the IP and port number of the server(So far so good).
I would like to how I can have clients see all the possible servers they can connect to on their local network when there are multiple servers running on their local network over different ports and then allowing them to connect to one. I am using basic Java socket programming for this project.
You could have
use a UDP based protocol where each server publishes it's IP:port every second.
you could have a service where each server registers. You could chat with that to get the list of all services.
The nice approach of the later option is you can use one of your chat services for server discovery. When you want to get the list, you send a message to a channel on that server which all the servers are listening to and they respond with a chat message.

KryoNet - Sending packets to Android clients

I am developing an Android app which has to connect clients to my server for sending/receiving different sort of packets. To achieve that, I am using the KryoNet library. My clients connect themself to the server and send their own packets successfully. But my problem is that the server seems to failed at responding to them. I have registered these packets in the same order on the server side and the client side and I use the connection parameter of the overrided reveived method from kryonet.Listener to respond (e.g. connection.sendUDP(...)). I have an Android 5.0.2, I use the 2.22.0-RC1 version of KryoNet both on the server side and the client side... With the option Log.set(Log.LEVEL_DEBUG) enabled, I can see that the server starts to write a packet
(DEBUG: [kryo] write: PacketMovement) but I receive nothing.
I would really appreciate any help on this issue.
The answer for people having the same problem : https://github.com/EsotericSoftware/kryonet/pull/111.

Load balancer, sockets and java

I've got a project to work on and I need to build some client - server applications where I can send messages and whatever, in Java. One goal of the project is to deal with failover. When a client is connect to a server and the server dies, it automatically connects to the backup server. What I want to do is not required but I want to implement a load balancer so multiple clients get connected to the prefered server.
The connection between a client and the server must be with TCP sockets.
This is a schematic of the network architecture:
client connects to the load balancer (udp or tcp, I don't know the
best one for this situation).
load balancer decides the which server should that client connect (the most empty one)
the client creates a TCP connection with the specified server
My question is:
How should the load balancer work to get a client to connect to specific server? Send information of the server (ip, port) to the client and the clients creates another socket with the new ip? (blue line).
Or is there a way for the load balancer to connect those two end points (server #1 <-> client #1), without having to send information to the client?
PS: I'm asking you because it seems unnecessary for each client to have to create 2 sockets to get connect to the server (first socket black line, second socket blue line)
i think after the client sends information to the load balancer,its best if the load balancer sends that information to the server which it decides instead of sending something back to client and client connecting again to server

Client Server Apps+java

I have a java requirment contains both client and server side program.
Server side
Server program frequently check the data base and checks if a new order came, if order came it check the order and send it to the corresponding client machine using IP address and port.The client machines are out side the LAV and has static IP address.
Client side
Client program listen a its on port , when an order came, read it and process.
For implementing these app, which java package is best,java socket communication or any other.Anybody know please suggest one.
Help is highly appreciated,
Thanks,
vks.
Don't go for low level programming like Sockets etc. Use RMI. Your program will have following two entities
Server side :
An RMI Client for calling client machine to send update after checking the database
Client side :
An RMI server application listening for Server update requests and do processing.
If you are new to RMI check out this tutorial . You can search for better tutorials if don't find these good enough :).
I remember I had to do something similar in the university and I used JMS (Java Messaging Service), documented here:
http://www.oracle.com/technetwork/java/jms/index.html
The Server will create the messages from the DB by checking it periodically and will send messages to the clients which will process the info.

Network discovery in Java using multicasting

I'm trying to make a client/server Java App. Both client and server will be running on the same wi-fi network. Server will be running on a specific port that client is aware of.
I am planning to send a multicast message from client through the network for that specific port to discover the server. However, I'm not too sure how I can find out which IP in my network received my message.
Do I need to create a socket on the client and listen to incoming packets once I send my multicast message in case server replies back?
Thanks in advance.
(1)server listens on a pre-arranged port
DatagramSocket s = new DatagramSocket(8888);
s.receive //(1)
s.send //(2)
(3)client sends a message to the port, on the broadcast IP, 255.255.255.255
DatagramSocket c = new DatagramSocket();
c.send(255.255.255.255:8888,msg) //(3)
c.receive //(4)
the client binds to a port too. we didn't specify it, so it's random chosen for us.
(3) will broadcast the message to all local machines, server at (1) receives message, with the client IP:port.
(2) server sends response message to client IP:port
(4) client gets the reponse message from server.
I would strongly recommend using JGroups. It has a lot of features and it will do all the UDP stuff. JBoss uses it for their clustering.
You can try using java.net.MulticastSocket (available since Java 1.1). If you don't need the rich feature sets of libs like jgroups, hazelcast etc. that plain Java API might serve you well enough.
See also example pages here and here.
You could try using SSDP. It's what UPnP devices use to discover each other. It's multicast on port 1900 and just uses really simple packets to send around IPs and service information.
Cling is a UPnP lib you can pull from. Note I'm not recommending you move to UPnP - just the discovery protocol used.

Categories

Resources