Multithreaded Proxy Server implemented in java -
i implementing multithreaded proxy server in java accept messages clients , forward them server acknowledge reception of message. however, i'm having trouble doing so. point out doing wrong? thanks.
proxyapp:
public class proxyapp { public static serversocket server = null; public static socket client = null; public static void main(string[] args) { try { server = new serversocket(6789); socket clientsocket = null; while(true) { client = server.accept(); if(client.isconnected()) { system.out.println("proxy listening client on port 6789"); } thread t1 = new proxyhandler(client); t1.start(); clientsocket = new socket(inetaddress.getlocalhost(), 6780); if(clientsocket.isbound()) { system.out.println("clientsocket connected on port 6780"); } proxyhandler t2 = new proxyhandler(clientsocket); t2.start(); } } catch(ioexception io) { system.err.println("ioexception: " + io.getmessage()); } } }
proxyhandler:
public class proxyhandler extends thread { private socket socket; private string message; public proxyhandler(socket socket) { this.socket = socket; } @override public void run() { message = ""; try { datainputstream in = new datainputstream(socket.getinputstream()); dataoutputstream out = new dataoutputstream(socket.getoutputstream()); while(true) { message = in.readutf(); out.writeutf(message); system.out.println(message); } } catch(ioexception io) { system.err.println("ioexception: " + io.getmessage()); system.exit(2); } } }
clientclass:
public class clientclass { public static void main(string[] args) { try { socket client = new socket(inetaddress.getlocalhost(), 6789); if(client.isbound()) { system.out.println("successfully connected on port 6789"); } scanner scanner = new scanner(system.in); datainputstream infromproxy = new datainputstream(client.getinputstream()); dataoutputstream outtoproxy = new dataoutputstream(client.getoutputstream()); while(true) { string message; system.out.print("enter message: "); message = scanner.next(); outtoproxy.writeutf(message); system.out.println(infromproxy.readutf()); } } catch(ioexception io) { system.err.println("ioexception: " + io.getmessage()); system.exit(2); } } }
serverclass:
public class serverclass { public static void main(string[] args) { try { serversocket server = new serversocket(6780); if(server.isbound()) { system.out.println("server connected on port 6780"); } socket client = null; while(true) { client = server.accept(); if(client.isconnected()) { system.out.println("proxy connected"); } datainputstream infromproxy = new datainputstream(client.getinputstream()); dataoutputstream outtoproxy = new dataoutputstream(client.getoutputstream()); system.out.println(infromproxy.readutf()); outtoproxy.writeutf("message has been acknowledged!"); } } catch(ioexception io) { system.err.println("ioexception: " + io.getmessage()); system.exit(2); } } }
the in , out in following code represent message , same machine:
datainputstream in = new datainputstream(socket.getinputstream()); dataoutputstream out = new dataoutputstream(socket.getoutputstream());
so when read in , write out, doing creating echo service.
message = in.readutf(); out.writeutf(message);
for proxy, want read client , write server, , vice versa.
it seems want closer this:
client = server.accept(); clientsocket = new socket(inetaddress.getlocalhost(), 6780); thread t1 = new proxyhandler(client, clientsocket ); t1.start(); thread t2 = new proxyhandler(clientsocket, client); t2.start();
where job of first thread send data client server , second's job send server client.
Comments
Post a Comment