Android Question AsyncStream Internet connection.

walmo

Active Member
Licensed User
Longtime User
Hi
Will it be possible to send internet data through AsyncStream connection?
Need to make a proxy server.
Thanks
 
Last edited:

walmo

Active Member
Licensed User
Longtime User
Ok, so its possible to make a server with OkHttpUtils2 that will share a internet connection to multiple clients.
Where can i find some info on this please.
Thank you
 
Upvote 0

walmo

Active Member
Licensed User
Longtime User
Its looking like im in over my head here , dont know where to start searching.
I've setup the connection over wifi direct , used the FTPServer library and it connects with the password in the ftp Adduser.
I do see the request on the server from the client in the log windows when i browse.
But now What?

What i think i must do is ..
1.> Get the request from client .
2.> Get that request from the internet.
3.> Send That info back to client.

But im stuck....
Here is a piece of java code
Java:
import java.io.*;
import java.net.*;
/**
*
* @author jcgonzalez.com
*
*/
public class ProxyMultiThread {
    public static void main(String[] args) {
        try {
            if (args.length != 3)
                throw new IllegalArgumentException("insuficient arguments");
            // and the local port that we listen for connections on
            String host = args[0];
            int remoteport = Integer.parseInt(args[1]);
            int localport = Integer.parseInt(args[2]);
            // Print a start-up message
            System.out.println("Starting proxy for " + host + ":" + remoteport
                    + " on port " + localport);
            ServerSocket server = new ServerSocket(localport);
            while (true) {
                new ThreadProxy(server.accept(), host, remoteport);
            }
        } catch (Exception e) {
            System.err.println(e);
            System.err.println("Usage: java ProxyMultiThread "
                    + "<host> <remoteport> <localport>");
        }
    }
}
/**
* Handles a socket connection to the proxy server from the client and uses 2
* threads to proxy between server and client
*
* @author jcgonzalez.com
*
*/
class ThreadProxy extends Thread {
    private Socket sClient;
    private final String SERVER_URL;
    private final int SERVER_PORT;
    ThreadProxy(Socket sClient, String ServerUrl, int ServerPort) {
        this.SERVER_URL = ServerUrl;
        this.SERVER_PORT = ServerPort;
        this.sClient = sClient;
        this.start();
    }
    @Override
    public void run() {
        try {
            final byte[] request = new byte[1024];
            byte[] reply = new byte[4096];
            final InputStream inFromClient = sClient.getInputStream();
            final OutputStream outToClient = sClient.getOutputStream();
            Socket client = null, server = null;
            // connects a socket to the server
            try {
                server = new Socket(SERVER_URL, SERVER_PORT);
            } catch (IOException e) {
                PrintWriter out = new PrintWriter(new OutputStreamWriter(
                        outToClient));
                out.flush();
                throw new RuntimeException(e);
            }
            // a new thread to manage streams from server to client (DOWNLOAD)
            final InputStream inFromServer = server.getInputStream();
            final OutputStream outToServer = server.getOutputStream();
            // a new thread for uploading to the server
            new Thread() {
                public void run() {
                    int bytes_read;
                    try {
                        while ((bytes_read = inFromClient.read(request)) != -1) {
                            outToServer.write(request, 0, bytes_read);
                            outToServer.flush();
                            //TODO CREATE YOUR LOGIC HERE
                        }
                    } catch (IOException e) {
                    }
                    try {
                        outToServer.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
            // current thread manages streams from server to client (DOWNLOAD)
            int bytes_read;
            try {
                while ((bytes_read = inFromServer.read(reply)) != -1) {
                    outToClient.write(reply, 0, bytes_read);
                    outToClient.flush();
                    //TODO CREATE YOUR LOGIC HERE
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (server != null)
                        server.close();
                    if (client != null)
                        client.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            outToClient.close();
            sClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

THANKS
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
step back and look at it conceptually.
your proxy server sets up 2 connections: one between it and its client,
and one between it and the site the client wants to connect to.

imagine you have 2 phones, one at each ear. you listen to the phone
on the right and you repeat what you hear to the phone on the left. when
the person on the phone on the left answers, you repeat what is said to the
phone on the right. that's what your sample code is doing.

in the old days (maybe even still today), you could put the phones "together"
(by cloning the sockets) and let the parties speak to each other. i haven't done
this for years, but it may still be how it's handled. in any case, you can
certainly open streams between the proxy and the client and between the
proxy and url the client wishes to connect to.

by being in the middle, the proxy can block connections or even filter what
is said in the left ear before passing to the phone on the right... cloning
sockets, of course, gives up that control in favor of speed.

if you are able to communicate with a your server app from a client, you
should be able to open a stream from your server app to some server in the
wild and repeat what that stream receives back to your client.
 
Upvote 0
Top