Home » Java » Transferring http via sockets. How to connect thread inputStream, to another one outputStream with sockets?

Transferring http via sockets. How to connect thread inputStream, to another one outputStream with sockets?

Posted by: admin October 26, 2017 Leave a comment

Questions:

I’m trying to build the client – server app, that will use a Android phone like proxy server via 3g/4g connection. There is a problem, that over 3g/4g operators denied incoming connections. So, i’m trying to do this with socket connection and one a server, that will be used like bridge.
So the scenario will be like:

  1. client A wants to open a google from Android phone B.
  2. client A connects to reverse-proxy server C;
  3. server C already connected with B.
  4. A->C->B request;
  5. B->C->A response;

So, i’m using sockets for this.
I have a socket Server

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

public class MainServer {
static final int PORT = 1978;
static List<ThreadProxy> clientList= new ArrayList<>();
ServerSocket serverSocket = null;
Socket socket = null;

public MainServer() {
    try {
        serverSocket = new ServerSocket(PORT);
    } catch (IOException e) {
        e.printStackTrace();
    }
    while (true) {
        try {
            socket = serverSocket.accept();
        } catch (IOException e) {
            System.out.println("I/O error: " + e);
        }

        ThreadProxy client = new ThreadProxy(socket);
        clientList.add(client);
        client.start();
        System.out.println("New client connected.");
    }
}

public static void main(String args[]) {
    new MainServer();
}

public List<ThreadProxy> getClientList(){
    return clientList;
}

class ThreadProxy extends Thread {
    protected Socket socket;
    int port;
    InetAddress ip;

    public ThreadProxy(Socket clientSocket ) {
        this.socket = clientSocket;
        port = this.socket.getPort();
        ip = this.socket.getInetAddress();
        System.out.println("INFO: Incoming connection from: "+ip+":"+port);
    }

    public void run() {
        InputStream inp = null;
        BufferedReader brinp = null;
        DataOutputStream out = null;
        try {
            inp = socket.getInputStream();
            brinp = new BufferedReader(new InputStreamReader(inp));
            out = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            return;
        }
        String line;
        while (true) {
            try {
                line = brinp.readLine();
                if ((line == null) || line.equalsIgnoreCase("QUIT")) {
                    socket.close();
                    return;
                } else {
                    switch (line){
                        case "getList":
                            for(ThreadProxy client: clientList) out.writeBytes(client.toString() + " ,");
                            out.writeBytes("\n");
                            out.flush();
                            break;
                        default:
                            System.out.println("from client " + socket.getInetAddress() + ":" + socket.getPort() + ":" + line);
                            out.writeBytes(line + "\n");
                            out.flush();
                            break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
        }
    }

    @Override
    public String toString() {
        return "ThreadClients{" +
                "ip=" + ip +
                ", port=" + port +
                '}';
    }
}

}

And a simple clients().

How can i connect one thread inputStream, to another outputStream?

Can I use this connection to transmit http/https data(requests, responses, video streams, like youtube)?

Answers: