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.)
Related
sorry if my english isn't perfect.
I'm trying to make an app and I need to exchange information between more devices.
I thought that could be a solution connect the devices on a server but I really don't have the idea where start.
What language I need to study to make this? There is a better solution?
This highly depends on what you are trying to achieve in the first place. It would be helpful if you could tell what you are trying to do, but I will still outline some general aspects:
You need to decide, what information is going to be exchanged and how this should happen
What information: Figure out, what exactly needs to be sent and received. Generic text messages? Images? Byte Streams?
How should this be done: Generally spoken, there are two approaches of getting information as a client: Polling and subscribing.
Polling: This approach means to periodically check an endpoint for new data. For example, HTTP uses this way: A web browser or any other client (REST-Client for example) periodically requests information from a HTTP-Server, using a connection just for this single request.
Subscribing / Sync / Notification: In some way or another, the client tells the server that it is interested in the information and wants to get notified when there is something new. The connection is initiated at the beginning and held open for further usage. The benefit of this approach is that changes are received immediately, but on the other hand a permanent connection needs to be maintained.
Things to study
At the beginning, get a good understanding of the TCP/IP Protocol, how Sockets work, how common Protocols do their job (e.g. HTTP, WebSockets)
Take a look at specific Protocols working on top of the basic ones
Tip: REST: Most common WebServices Protocol, providing a common way to exchange stateless data. Uses Polling.
WebSockets: Socket connection using Web Browsers. Commonly used to update information without needing to poll.
There is no specific language to learn for connections. It's more about understanding what the difficulties are and what ways have been invented to address this. Once you get to this point and know what you want to do, it's possible in every language.
Recommendation: As you seem to use Java/Android, I would try to use REST. A really great client-side library for REST on Android is Retrofit. For the server side use what fits for you .. common Java way would be to use Jersey, but you are free to choose from a lot of choices. If using Jersey is too hard for the beginning, maybe take a look at the JS/NodeJS world, those guys invented Express, which allows you to create a REST service out of just a database, wihtout having to code a lot.
First you need to decide if you want to go for an Android or an iOS application. There are other various mobile operating systems as well, but these are widely used . If you want to go for android which is most widely used in my opinion, then you need to learn Java. If you want to go for iOS application, then you need to learn swift or objectiveC. These languages provide the API to connect with various types of services such as Facebook, Firebase and Amazon etc. If you want to connect to some other local server who’s IP is known to you, then you can use socket programming to send messages.
There could be many ways you can implement this. One way will be using Web services. Of course REST might be a better option, if you follow this approach. You can implement Your service(server side code) with any language. I will recommend you use java since you are already using android.
Aside from this You might need to go through the basics of REST, its specifications and
some reference implementations for language of your preference.
I want to develop a p2p app using java and netbeans IDE. In my app i want to make two things
file sharing between users
Chat between users
I have read from different resources
Introduction to the Peer-to-Peer Sockets Project
Peer-to-Peer File Sharing
Problem is how i am able to connect two or more pcs connected to different networks. Showing all connected peers, file sharing of any type under a size limit. Any help will be appreciated.
These questions are dealing with the most well known problem of NAT traversal, dealing with the NAT traversal is not related with the language you are using rather its about the basic problem domain. First of all you need to advertise the address(es) of your both end using some signalling method (i.e. SIP, Jingle ..). Now if you are trying to connect two end point which resides behind same network or NAT then simply knowing or advertising the local host candidates will do the job, but if any of your end point resides out side of the network or having NAT between two endpoints then you need to traverse the NAT using protocols like STUN/TURN/ICE. Based on your use cases, you may interested to read the RFCs like RFC-5389, RFC-5245 and RFC-5766. Once you know what you need you might get any open source solution by google, or If you are curious enough you may start implementing your solution based on the RFCs. So the keywords like NAT traversal or STUN/TURN/ICE might help you to know your problem first, once you know your problem domain then you can find solution.
Now if you are trying to connect two end point which resides behind same network or NAT then simply knowing or advertising the local host candidates will do the job, but if any of your end point resides out side of the network or having NAT between two endpoints then you need to traverse the NAT using protocols like STUN/TURN/ICE.
There is a Java server and many Arduino devices, and they are connected via TCP connection. Board that we can use: Arduino Uno or Arduino Mega 2560
Each Arduino device is actually reporter (say, it takes data from water meter and transmits data to the server) or some kind of remote control for some appliances.
Currently, this connection isn't secure. I need to make it secure, i.e. implement VPN.
I'm completely new in this field of knowledge, and I'm trying to figure out the way should I use.
I really hope that I can find existing implementation of some protocol, and build it into this system.
But which protocol?
I already found out that I can't use IPSec, since TCP/IP stack is hardware-implemented in these Arduino devices, so, we can't modify it.
Therefore, I need to use some protocol higher than TCP, and I need implementation in C (for client devices) and in Java (for server).
I'm trying to find implementations of SSL, or PPTP, or L2TP, or something else which I still don't know about.
If anyone have experience in this field, I would be glad to see your suggestions.
Response to your follow up question got too long...
VPN usually creates a "tunnel" into an infrastructure- say, the University computer system. That is, the VPN concentrator "sits st the perimeter" of the university network, and when you connect to it, you create a "pass through" into the system behind the firewall - any protocols, any IP addresses inside the firewall become accessible. Key is that anyone observing the flow of traffic from your computer to the concentrator (also called "VPN gateway") only sees you talking to the concentrator - they don't know what IP addresses inside the firewall you are talking to, with what protocol, or what data. By contrast if you don't need to hide all that stuff you just have to encrypt your data itself; a simple encryption algorithm is easy to implement, especially if you don't need to worry about people stealing your devices and getting the code. How complex the encryption needs to be will depend on your application- there is a trade off between speed, memory use, and security.
Goog. le "Arduino encryption library" for some examples; pick a key, then encrypt your data, and just POST it...
Let us know how you make out!
I very much agree with #Floris. To add some ideas to that:
What about HTTPs? If your communication is one-way (Arduino -> PC) then it should be sufficient for your needs. This issue is discussed in this post, with references to this discussion. HHTPs on Arduino is not easy, but it may have been done.
Apparently, XXTEA is another alternative, and there are several stackoverflow questions on this topic as well.
If you go the -build-it-yourself route, check out this post on electronics.SE: apparently there is a cryptography library for AVR, and also useful list of attacks to consider in the electronics.SE post: are you worried only about MitM attack? What if someone rips your device open the reads the keys, is that a problem?
Here is described an implementation of the [CHAP] (en.wikipedia.org/wiki/Challenge-Handshake_Authentication_Protocol) for Arduino/AVR devices.
And here is described a similar implementation of [HMAC] (en.wikipedia.org/wiki/Hash-based_message_authentication_code) and [SHA256] (en.wikipedia.org/wiki/SHA-2) for Arduino/AVR.
[Cryptosuite] (github.com/wgoulet/Cryptosuite) is an implementation of HMAC-SHA-256 for Arduino.
Finally, [here] (github.com/arpitchauhan/cryptographic-protocols-arduino-and-PC) are demonstrated some cryptographic procolos for Arduino, including key exchange using RSA.
So it is definitely possible to secure communication between Arduino and a server.
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.
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.