mardi 10 mars 2015

Writing a multi-threaded proxy in Java



i'm attempting to write a simple multi-threaded proxy in java that takes in a properly formatted HTTP request, parses it, sends it into a server, and prints out what the server returns using System.out. I have four classes that i can't connect together to properly communicate with the server. Here is my code:



import java.util.Scanner;
import java.util.StringTokenizer;

/*
* Class parses HTTP request headers and organizes them into their proper format
*/
public class EstHTTP {

int portNumber = 0;

public void doFormattedMethodRequest(String unformattedRequest) throws Exception{

StringTokenizer tokenizer = new StringTokenizer(unformattedRequest," ");
Scanner scan = new Scanner(System.in);


String method = tokenizer.nextToken();
while (!method.equals("GET")){
System.out.println("There needs to be a GET header.");
scan.nextLine();
}

String endUrl = tokenizer.nextToken();
if (!endUrl.contains(".html")){
portNumber = Integer.parseInt(endUrl);
}

String version = tokenizer.nextToken();
if (!version.equals("HTTP/1.0")){
System.out.println("The HTTP needs to be version 1.0");
}
String hostHeader = tokenizer.nextToken();
String host = tokenizer.nextToken();


String connectionHeader = tokenizer.nextToken();
String connection = tokenizer.nextToken();


System.out.print(method); System.out.print(" "+endUrl);
System.out.println(" "+version);
System.out.print(hostHeader); System.out.println(host+endUrl);
System.out.print(connectionHeader); System.out.print(connection);

TcpServer.makeServerCall(method, endUrl, version, host, connection, portNumber);

}//end method
}//end class

********************************************
import java.util.*;


public class EstHTTPClient {

public static void main (String[] args) throws Exception{

EstebanHTTP estHTTP = new EstebanHTTP();
System.out.println("Please enter your info: ");
Scanner scan = new Scanner(System.in);

String unformattedRequest = scan.nextLine();

estHTTP.doFormattedMethodRequest(unformattedRequest); //here i'm getting the request but i'm not sure what class/method to call in order to properly communicate with the server.


}//end main
}//end class

********************************************
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;


class TcpServer {

public static void serverMain() throws Exception{

//ServerSocket welcomeSocket = new ServerSocket(portNumber);

while(true){
// new TCPServerThread(welcomeSocket.accept()).start();
}


}//end method

public static void makeServerCall(String method, String endUrl, String version, String host, String connection, int portNumber) throws IOException{

Socket thissocket = new Socket(host,80);
thissocket.getInputStream();

//i'm not sure if i have to use InputStream here or in TcpServerThread
InputStream fromServer;
OutputStream toServer;
fromServer = thissocket.getInputStream();
toServer = thissocket.getOutputStream();

}
}

********************************************
import java.io.*;
import java.net.*;


class TCPServerThread extends Thread {

private Socket connectionSocket = null;

public TCPServerThread(Socket theSocket){
super("TCPServerThread");
this.connectionSocket = theSocket;
}

public void run(){
String clientSentence;
String capitalizedSentence;
byte[] b = new byte[8196];

try{

ServerSocket welcomesocket = new ServerSocket(6789);
while (true){
Socket connectionsocket = welcomesocket.accept();
BufferedReader infromclient = new BufferedReader(new InputStreamReader(connectionsocket.getInputStream()));
DataOutputStream outtoclient = new DataOutputStream(connectionsocket.getOutputStream());
clientSentence = infromclient.readLine();
PrintWriter pr = new PrintWriter(System.out,true);
pr.println(clientSentence);
capitalizedSentence = clientSentence.toUpperCase()+'\n';
pr.println(capitalizedSentence);
outtoclient.writeBytes(capitalizedSentence);


}
/*
InputStream incomingIS = connectionSocket.getInputStream();
int len = incomingIS.read(b);

BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));

OutputStream outgoingOS = connectionSocket.getOutputStream();
outgoingOS.write(b, 0, len);




DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
clientSentence = inFromClient.readLine();
System.out.println("Recieved: "+clientSentence);
capitalizedSentence = clientSentence.toUpperCase()+'\n';
outToClient.writeBytes(capitalizedSentence);
*/
}
catch(Exception e){
e.getMessage();
}
}
}


In my TcpServerThread class i'm doing most of the server communication, and i'm not sure whether or not how and where to call this class, and with what info, in order for the server to make a correct response.




Aucun commentaire:

Enregistrer un commentaire