I am coding a program that necessitates having two machines talk to each other via TCP.
I have a (very) basic familiarity with the java.net Socket classes and related things. I am worried about routers/firewalls blocking the connection. All I need to do is get a port open in order to use the Java STL Socket classes.
I was told to look into using upnp by a friend, and another stackoverflow question regarding upnp suggested using Cling. As a network programming novice, Cling seems like overkill for this (and I am not advanced enough to fully understand the manual).
I am sorry if I am asking the wrong question, looking in the wrong place, etc. All I want to do is connect two machines to send some floats back and forth without having to manually go into my router and forward ports.
Is there anything in the STL that does this automatically, or any simple libs?
First thing first: there's no such thing as "STL" in Java. You're probably referring to the basic Java SDK.
Second, you are asking for "simple means" to perform an operation that is, from a networking point of view, not trivial at all. Look at the most basic task that one of your programs (program A) will have to do: denoting the network location of its "target" (program B). If program B is behind a router, what do you know about program B's network location? nothing. All you know is the router's address.
Same with firewalls. You're concerned about firewalls blocking your connection? well, if your firewalls are any good, then you want them to block all connections except for pre-approved ones.
For programs separated by a NAT, the only sensible method to go about doing what you're looking for is to use UPnP. If Cling is an overkill, try something like weupnp.
There's no free lunch, though.
If there is no network "masking" device (such as a router) between program A and program B, then very simple TCP/IP programming using the Java SDK should just work. If there are networking devices along the ways, you'll have to cope with them by means of configuring your devices, or using UPnP.
I would take a long look at the following tutorial: http://www.giantflyingsaucer.com/blog/?p=224
The idea here is that a library called xsocket creates events that trigger when new information arrives.
Related
I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.
How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.
Could you please recommend me some web or book to read about this, or send me some example code to learn?
Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?
EDIT:
I will add some extra information.
What I would love to achieve is the following:
My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).
Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.
From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.
That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.
It would be nice also to be easily re-connectable to the server.
ICE is a modern and good library for distributed applications:
many languages as C++ and Java
many platforms
GNU GPL
good performance
easy to use
First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.
More info at http://zeroc.com/ice.html
Have fun ;-)
EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-(
Therefore ACE is not as easy to use as STL or BOOST. Moreover, ACE is not really efficient... I prefer ICE ;-)
I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.
Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.
Anyway the problem has been solved multiple times already.
http://activemq.apache.org/
http://www.rabbitmq.com/devtools.html
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.
I'm currently working on trying to create a very basic bare bones instant messenger with Java.
I'm at a bit of a loss where to begin, was wondering if anyone here could reccommend a starting point? What I should read? What would be the easiest way for a beginner/intermediate programmer to try and achieve this?
Currently, I'll I'd want it to be able to do is to send text to one machine to another. I am so far unfamiliar with anything network related in java. Would I use a server or just direct connect between two machines?
Any pointers/direction or advice anyone has in this matter would be very helpful!
Firstly check out the official resource for Socket programming, the article on writing a Client/Server Pair should provide you with what you need to know to create a instant messenger.
Well, I am focusing the second question of yours.
If you're building this program for your local area network and if you're intended to use socket programming, you need to find the exact ip address of the machine. If you don't know the ip address ( I mean if you don't want the program to know ip address before you run it ) ,then you can ping local ip addresses until you find some peers.
Some of the p2p applications use servers as the ip matchers. This means that the servers have ip address tables which make all of the users send/receive data by using these servers. But if you just started on networking with this project, I think don't use server.
Lastly, you can take a look at a project of my own. It's a simple parallel document preparation program with a chat module inside it. It's developed in C#. It needs wireless ad hoc network to run. Link : http://code.google.com/p/parallel-docs/
I don't think this type of project is ever going to be a beginner project. I'm absolutely not discouraging you to try it, I think you should. I'm always in over my head with stuff like this but I look it up and ask questions and usually end up learning something. My advice would be no matter how simple of a program it is, start with some Class Diagrams, if nothing else just to get organized before jumping into the code. For example you know you you'll need a user account object right off the bat and you know you'll need certain attributes for that object (username, password, etc.), I encapsulate everything so then you would need methods to get and set these fields and so on. I've found that by doing that even if I don't create complete proper UML diagrams getting the easy stuff out of the way and if somewhat organized first if nothing else frees me to focus on the more complex logic and things I may not yet know how to implement.
I want to communicate between two computers that aren't on the same network, i tried to use sockets, but I only found tutorials and examples for communicating in the same network.
I tried to search on stackoverflow, but I had no clear idea of what to search for.
I want to send text two-way (chat).
-edit- The thing is, it is to me vague what I have to use or do, so I can't be much clearer then to say I want to send two way text (perhaps date) for a chatapp, just to learn from.
-edit-
To communicate between two machines, they have to be on the same extended network. The only other option is to save data to external drives, e.g. USB drives, and you can attach to the second machine and up load.
You can use sockets to communicate with any other machine with an Internet connection, provided your firewall(s) and configured to allow the connection. The whole point of a firewall is to prevent unauthorised access. I suspect this is your real problem. If your firewalls prevent access, you should not be able to get around this using Java (google network hacking for alternatives ;)
Another option is to use a service such as email if the two computers cannot talk to each other directly (they still have to be connected to the same network) This is much more complex than using a direct connection but it can be done.
I suggest instead you configure your firewall(s) correctly and you will be able to use plain Sockets.
You have to get the external IPs (there are sites which while visiting give you this IP), and hope that the firewall allows communication. Typically one should use a port above 1000.
There should be peer-to-peer libraries.
Another way is via SSH on one computer (JSch is one java library for SSH).
The easiest way is email exchange: the javax.mail API is compact but not the easiest. (Especially you could easily delete all mails, as I did once.)
First off, before I ask, i would like to point out that this question is for education. I want to know to expand my understanding of Java and network security (what little there is).
How could you use Java for network security and counter attacks? I have been using server/sockets for a while now (for non system security stuffs), but I don't quite understand what I'm doing. Naturally, I should learn up on networking, but where to start? There is a protocol for everything, heck there are protocols to have protocols. To further expand, how could you use Java to say, port sniff, catch packets or kill/open a port remotely?
I guess to phrase the question more adequately; does anyone know of any good sources that I could look at to get a more in depth look/study of how Java handles network security and counter hacking and malware containment?
I think the best thing to do would be to learn concepts, then worry about using Java to implement the concepts later on. There are some gaps in your understanding (for example, I don't even know what "open a port remotely" might mean) and the best thing to do would be to solidify your understanding of how networks work first.
I don't really have a list of network security texts I can recommend -- probably someone else will! -- but IMHO it might not hurt to start with a classic like Steven's "UNIX Network Programming" to shore up the fundamentals, if you can find a copy.
how could you use java to say, port
sniff, catch packets or kill/open a
port remotely?
You can't use Java to sniff ports.
You can't use Java to catch packets.
You can't use Java to kill/open a port remotely.
how Java handles network security and
counter hacking and malware
containment?
Java doesn't handle network security other than internally for its own applications via the security sandbox.
Java doesn't handle counter hacking.
Java doesn't handle malware containment other than internally for its own applications via the security sandbox and bytecode verifier.
One of those things above can be done via an add-on to Java, but basically Java isn't the correct tool for this job.
Can any one suggest me a packet blocking software ??
My requirement is to block packets within the LAN.(Internet does not come into picture).
Supposing CLIENT_A communicating to CLIENT_B in the same network(LAN).
I want a CLIENT_C (who is in the same network) to capture(I can use jpcap library's sniffer for this purpose) and block packets sent by CLIENT_A to CLIENT_B.
How do i block packets over LAN..?
Thank you in advance.
Regards,
Veenit Shah
This kind of thing is normally done using a separate firewall (e.g. in a router or gateway box) or firewall software running on one or both of the client machines.
This is not the sort of thing that it is sensible to implement in Java.
EDIT - in response to this followup
I am aware that such a thing is not advisable in Java..but still ill have to implement it..so is there any means to do so..?
Let us assume that you are talking about implementing a client-side firewall on a Linux machine. I can think of two approaches:
You could use Process.execute() and friends to run the Linux iptables(8) admin utility which manipulates the OS kernel's network packet filters. This is the simplest Java-based approach. But it requires that your Java app runs as root.
You could reverse engineer what iptables(8) is doing to manipulate the packet filters and code the same functionality in Java. That would be more coding work, including implementing parts of the functionality in C via JNI or JNA. And your app needs to run as root.
But a far, far simpler approach is to simply run iptables(8) from the command line, or make your changes using the fancy GUI-based admin tools.
Note that in the scenarios above, the firewall itself is not implemented in Java. All you are doing is administering the firewall from a Java application. I cannot think of ANY way to actually do the filtering / blocking in Java that is even remotely practical.
You may need some arp attack things to redirect packets from CLIENT-A to Client-C instead of CLIent-B.
I don't think this is a good idea.
If you are reading the packet on the network on Client C, Client B has already received it so it is too late to block it. If you are using switched ethernet, then Client C wouldn't even see the packet that was sent to B anyway.
The only way to do this is to have A communicate to B through C then C can decide if packets should be sent. This is called a firewall. Instead of writing one, you could use iptables on Linux to do this.
But to make use of this, you need to understand how the network works and I'm not sure you do at this point based on your question. So you will need to learn a lot about ethernet (assuming you are using ethernet) and the different networking layers. I'm not sure of a resource for this.