So i want to make one application send data to the other via wi-fi. Since IPs are private, for the devices that are used to run the apps on, i figured i should go with something like this in order to make the two apps communicate:
App1<------>RemoteS开发者_开发问答erver<------->App2
I need help to setup the server that would just receive data from App1 and forward them to App2. I suppose i should use sockets for both apps, do i have to use 2 sockets? How do i pass the data that i receive from app1 to app2?
I will be using Java.
Ty in advance.
Yes, two sockets would be the right thing (if you don't want to tunnel via HTTP or similar).
public void proxy() {
ServerSocket s = ...;
Socket app1 = s.accept();
Socket app2 = s.accept();
InputStream app1Input = app1.getInputStream();
InputStream app2Input = app2.getInputStream();
OutputStream app1Output = app1.getOutputStream();
OutputStream app2Output = app2.getOutputStream();
pipeInToOut(app1Input, app2Output);
pipeInToOut(app2Input, app1Output);
}
public void pipeInToOut(final InputStream in, final OutputStream out) {
new Thread() { public void run() {
byte[] buffer = new byte[2000];
int r;
while((r = in.read(buffer) > 0) {
out.write(buf, 0, r);
}
}}.start();
}
Add some exception handling, stream closing and logic :-)
精彩评论