ganesh ganesh -4 years ago 111
C# Question

Communication between C# clients which are behind router and Java server which is on public internet

I have a C# applications which acts like a client and it can be installed on any system which is directly connected to public internet (through data cards or port forwarding) or they can be behind router also (without port forwarding).

The other application which is developed using java acts like a server application which is on the public internet. Now, my java application wants to push a message to C# application which is behind router. Java application has the clients public and private (192.168.x.x) IP address. Java application is supposed to run 24x7.

So, now there are two options for me:

  1. Whenever c# application starts it will establish a socket connection with java application and this socket connection will remain open till C# application gets closed.

  2. Whenever Java application has something for C# application it will create a socket connection with C# application then it will push the message and then close the connection.

Now, with 1st option there is a problem that there will be lots of unnecessary connection since there can be thousands of client application and it may happen that on some day there will be nothing to push for some clients. and I don't know how to go for 2nd option.

What will be the right way to accomplish this task (option 1 or 2)?

Is UPnP protocol right for 2nd option? What are the open source UPnP libraries which has both the API's (C# and Java). I found one such called ohnet. Will it be a right thing for me? I didn't found a single small example for OhNet to test.

Answer Source

2) is not feasible if you don't have control over network configuration at the client end. It won't in general be possible for the server to make connections to the client if the client is behind any moderately secure firewall / router.

So you will in general have have to go for some variant of 1) where the client creates a connection to the server.

You don't necessarily have to keep the connection open though - it's always possible to get the client to poll the server periodically to check if there are any new updates.

If you want realtime updates to the client from the server then you will still need to keep a connection open. This isn't necessarily a problem if you use Java NIO you should be able to handle tens of thousands of simultaneous incoming connections relatively easily.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download