I'm trying to write an Asynchronous UDP server using Java NIO ,
My problem is to find an identifier per client like IP address or somthing like that .
according to this article :http://thushw.blogspot.com/2011/06/asynchronous-udp-server-using-java-nio.html
In UDP there is only one socket for communicate with all clients , I want to hold an Hushtable that will save the data per client in buuffers . somthing like ( IP , ClientBuffer ) .
for every receive of data that a spcific client is sending i want to add it to his buffer .
I notice thet in datagramPacket i can recive the IP ,by the method datagramPacket.getSocketAddress() but i did'nt find the way to get the datagramPacket from the chanel ,we are only get the data , not the whole package with the addresse.
Thanks in advance!!!!
Rivka :)
You can only use the DatagramChannel.read() method if it is connected to the client, so you must already know the remote address:port. Otherwise you have to call receive(), when you get the remote ip:port in the DatagramPacket. So either way you have it.
Related
I have a configuration with the following server/clients :
One server with two bound sockets, a REP and a ROUTER
A client (we will call it a worker) that stays connected to the ROUTER socket
Another (real) client that connects on the REP socket.
I want the server to be able to tell the real client to connect (directly or somehow through the server) to a websocket, opened on the worker client. But it seems, I cannot retrieve the worker's IP-address from a ZeroMQ socket.
How could I achieve this, without some dirty IP-address retrieve hacks?
How could I achieve this, without some dirty IP-address retrieve hacks?
The best would be to use an explicitly communicated IP-address dialogue / handshaking between the server and the worker which would take place upon their setup / initialisation, in which the worker adviced these configuration details to server, upon having been asked to provide a such answer.
Given that, the "new"-real-client .connect()-s it's REQ onto the server's REP, and asks the server about where to go next, the server thus can answer this and the "new"-real-client will get received this way a legitimate IP-address:port# and any additionally needed details for any additional TCP/IP-L3 service establishment and use.
That simple :o) distributed-system
Design-side Epilogue:Because there are some further, design-side implications, hardwired inside of each type of the ZeroMQ sockets' Access-Point, it might be found more appropriate to serve a separate REP-AccessPoint on the server side, so as not to subordinate each "new"-real-client to become dependent upon a presence of events outside of the domains of control of both the server and such "new"-real-client, but to rather allow both such REQ/REP-endpoints to enjoy the independence of anything but their temporally (semi-)private details (re-)negotiation(s).
My job is to write a distributed client/server application with some concurrent tasks. So i decided to use akka.net for the concurrency issues. To implement the ipc between server and client akka remote is used. For some reasons there may run more than one client of the same type on a workstation. So i configured these clients for dynamic assignment of a tcp port. This worked fine for sending messages to the server.
My problem is to push some information to the clients. To accomplish this task an actor on the client exist. Now the server creates a reference for this actor. Therefor it needs the port the client is listening on . My idea is to send the tcp port the client uses to the server in some sort of connection procedure using a actor on the server.
After searching for some hours I didn't find any hint where to find the dynamically assigned tcp port. So how would the client get the assigned tcp port?
Ok, I could use akka.cluster. But using akka.cluster is breaking a fly on the wheel, I think. And if it solves my issue reamins to be seen.
Two suggestions, assuming that it is your client that makes the first contact with the server.
I'd have the server keep track of which clients are connected. I'd probably have a heartbeat message that gets sent once every few seconds from each client system. This way you can store an IActorRef for each alive client and send messages back without the need for finding the port. IActorRefs are preferable wherever possible for location transparency.
If you actually need to explicitly find the port, you may be able to extract it from the Path property of the IActorRef of one of the actors on the client system.
Thanks to patricks suggestions my issue is solved.
The solution is to extract the needed information from the senders path available while executing the hello message. With this information the server is able to maintain a list of all connected clients and theire network address.
Thanks a lot # patrick.
Regards Gregor
I need to create an application that:
Has one server
With a client that connects to the server and sends 8 longs (data from 8 sensors: rain, air humidity, wind speed...) 1 sensor data / long (sensor data is acquired from a custom USB device)
User clients. The end user runs this type of client to connect to the server for data retrieval from the sensors.
I used Qt before, creating Client-server applications with just one type of client. And I managed to create this application too, just at a smaller scale (used 5 words, and clients were connected simultaneously to the server). I used the Qt network examples fortune threaded server and http://goo.gl/srypT and blocking fortune client example.
How can i identify which client is which? (since they have different ip everytime they connect to internet). On my small scale application, I created some kind of protocol, but there must be a more efficient way to do this.
I assume that you want to identify the client type ("sensor client" vs. "user client"), not individual client instances.
The straightforward way to do this is to implement a protocol, as mentioned in the question. For your use case, this could be very simple:
let the "sensor client" send a "write" command (one character like "w" would be sufficient) followed by your sensor data. The server then receives the "w" command and knows that he needs to read sensor data from the client.
let the "user client" send a "read" command (e.g. the character "r"). When the server receives the "r" command it knows that it needs to send data to the client.
If, for whatever reason, you do not want to implement even such a simple protocol, you could also set up two separate QTcpServer instances which listen at different ports, lets say 8192 and 8193. Your "sensor client" would then connect to port 8192, and the server knows by the port number that the client will send data. Your "user clients" would connect to port 8193, and the server knows that the clients expect data and will send the required data.
In any case, you should be aware that there is no authentication and authorization involved, and any client who knows the simple protocol and/or the port numbers can send and receive data.
To identify a client, you have to use some kind of client ID. Usually, some kind of hash (a MD5 digest, a UUID or a GUID) is used as the client ID. This client ID have to be sent from the client to the server when the client connects to the server.
What happens after the client has been identified and accepted, depends on the type of connection (protocol). If you use a stateful protocol, the same connection will be kept open as long as the client uses it so there is no need to re-identify the client. If you use a stateless connection (HTTP, for example), you will have to re-send the same ID from the client to the server every time the client requires data (that is: a document, a page, etc.) to the server.
A simpler and more efficent way to deal with a client/server architecture like this consists in using an existing, proven server of some kind. For example, you could use a RESTful web server like Wt (http://www.webtoolkit.eu/wt/blog), given that you are already using C++.
Even better, I would use a Ruby- or a Python-based RESTful web service framework like:
http://www.sinatrarb.com/
http://bottlepy.org/docs/dev/
http://flask.pocoo.org/
Or the new Ruby-on-Rails API:
http://blog.steveklabnik.com/posts/2012-11-22-introducing-the-rails-api-project
https://github.com/rails-api/rails-api
Developing the server in Ruby or Python is much faster and easier. The client can developed in any way (C++ with Qt, Javascript in a web browser and many other ways)
I have computers connected in a wifi netwrok. One of them serves as a root(lets call it server), and is directly or indirectly connected to all other computers(lets call them clients). I want to send some information from root to all nodes(information is different for each node).
Is there a way to do this without running any program on the client side(similar to PING) ?
Or the only possible way is by using sockets over client and server?
Is there a way to do this without running any program on the client side(similar to PING)?
Yes, provided that you don't care that the clients will never do anything with the information.
Seriously, without something on the client listening for and doing something with the data you send from the server, what do you expect?
Ping does not actually send any data to the client. It just roundtrips a packet.
To receive the information you need some kind of service running on the client. Sockets are needed. For minimal communication (not reliable) use UDP and for more reliable use TCP.
I have a web service that is running on IIS (6 or 7, doesn't matter) and I would like to know the port that the caller has sent their request/invocation from.
So if a client makes a call on my web service, how do I find out from the server side what the port number is they made the call from?
Is that something that even gets passed at even the lowest level? Just to be clear I'm not looking for the port for callback purposes. It's for logging only.
You should be able to find it as "REMOTE_PORT" in the server variables of the Server object.
However, this port should pretty much always be random, and is only active for the Request/Response pair the client is making. It should be can't be used for asynchronous call backs. Even your webservice when calling to someother service will use a random port number to initiate the request from. The only static port in the communication is the receiving port at the server end of the TCP connection.
If you service is on WCF, then:
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
var endpointProperty = messageProperties[RemoteEndpointMessageProperty.Name]
as RemoteEndpointMessageProperty;
if (endpointProperty != null)
{
string sRemoteAddress = endpointProperty.Address;
int nRemotePort = endpointProperty.Port;
}
TCP sockets do have the concept of sender port number but it doesn't have much use in application level protocols. That said, considering the last paragraph of the OP, I think you're looking for some way to call back the client. The ports I said previously cannot be used for that. Asynchronous requests are identical to synchronous ones running on a separate thread, nothing special about them.
Some Network Address Translation-type devices will hide the actual "sending" port number from you. You would then have access to a useless IP address and a useless port number.