java - my proxy server doesn't work as expected -
i developed simple, multi-threaded proxy server. note program proxy 1 specific server. here code :
public class proxy { int remoteport; inetaddress remotehost; public proxy(inetaddress remotehost, int remoteport) { this.remoteport = remoteport; this.remotehost = remotehost; } public void go() { executorservice pool = executors.newcachedthreadpool(); try (serversocket server = new serversocket(2015);) { system.out.println("proxy running....!"); while (true) { socket socketclient = server.accept(); system.out.println("client connected...!"); socket socketserver = new socket(remotehost, remoteport); system.out.println("connection established server...!"); worker p1 = new worker(socketclient, socketserver); worker p2 = new worker(socketserver, socketclient); pool.execute(p1); pool.execute(p2); } } catch (exception ex) { system.out.println(ex.getmessage()); } } class worker implements runnable { socket from, to; public worker(socket from, socket to) { this.from = from; this.to = to; } @override public void run() { system.out.println(thread.currentthread().getname()); try (bufferedinputstream bis = new bufferedinputstream(from.getinputstream()); bufferedoutputstream bos = new bufferedoutputstream(to.getoutputstream());) { while (true) { int octet = bis.read(); if (octet == -1) { break; } bos.write(octet); } bos.flush(); from.close(); to.close(); } catch (ioexception ex) { system.out.println(ex.getmessage()); } } } public static void main(string[] args) { inetaddress adr = null; try { adr = inetaddress.getlocalhost(); } catch (unknownhostexception ex) { system.out.println(ex.getmessage()); } new proxy(adr, 2020).go(); } }
i test proxy server client , server. client sends array of integers find maximum. server of turn, returns maximum of received array. problem following: proxy server not playing role. array not sent server , client not received maximum value of array. code of server follow:
public class servertcpmax { static void display(int[] tab) { (int u : tab) { system.out.print(u + " "); } system.out.println(); } static int searchmax(int[] t) { int max = t[0]; (int = 1; < t.length; i++) { if (t[i] > max) { max = t[i]; } } return max; } public static void main(string[] args) { try { serversocket server = new serversocket(2020); system.out.println("server running .......! "); while (true) { try (socket sclient = server.accept()) { system.out.println("proxy connected...!"); objectinputstream ois = new objectinputstream(sclient.getinputstream()); int[] tab = (int[]) ois.readobject(); display(tab); int max = searchmax(tab); try (dataoutputstream dos = new dataoutputstream(sclient.getoutputstream())) { dos.writeint(max); dos.flush(); } } } } catch (ioexception | classnotfoundexception ex) { system.out.println(ex.getmessage()); } } }
the following code represents client:
public class clienttcpmax { public static void main(string[] args) { int[] = {3, -7, 9, 22, 0, 7, 11, 2}; try { try (socket socket = new socket("127.0.0.1", 2015)) { system.out.println("connection established proxy ...!"); objectoutputstream oos = new objectoutputstream(socket.getoutputstream()); oos.writeobject(a); oos.flush(); datainputstream dis = new datainputstream(socket.getinputstream()); int max = dis.readint(); system.out.println(" max : " + max); } } catch (ioexception ex) { system.out.println(ex.getmessage()); } } }
Comments
Post a Comment