简体   繁体   中英

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.xx) 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.

I would definitely use method 2 by adding a static route in the router (port forward). You should - however - ensure that the server behind the router is protected from the rest of your network (DMZ).

UPDATE:

Perhaps I have missed something here (method 1 or 2) :-) - but just to make it absolutely clear: It is always the client that should initiate the connection to the server. And yes, you could allow the client to request the server for updates on a regular basis.

Using option 2, will you have to queue messages for your C# client until it connects? That could make your Java application run into out of memory problems if the C# application doesn't connect.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM