Need help with asynchronous proxy connection

B

Bruce Lee

I need to build a proxy server that will keep alive an asynchronous
connection with a client and a remote server until the client closes. The
type of data is ASCII.

I've tried this code it doesn't work?

//needs to keep client socket & server socket open and wait for messages
from client and returns from server

class CommSession extends Thread{
Socket clie;
Socket serv;
CommSession(Socket clie,Socket serv){
this.clie=clie;
this.serv=serv;
}

public void run(){

try{
System.out.println("set up session on "+serv);
while(!clie.isClosed()){
DataInputStream in = new
DataInputStream(clie.getInputStream());
DataOutputStream out = new
DataOutputStream(serv.getOutputStream());
String cmd="";
while((cmd=in.readLine())!=null){
out.writeBytes(cmd+"\n");
System.out.println("TO-SERVER:"+cmd);
}

DataInputStream in2 = new
DataInputStream(serv.getInputStream());
DataOutputStream out2 = new
DataOutputStream(clie.getOutputStream());
String cmd2="";
while((cmd2=in2.readLine())!=null){
out2.writeBytes(cmd2+"\n");
System.out.println("TO-CLIENT:"+cmd2);
}

}

}catch(Exception ss){ss.printStackTrace();}
}


}
 
L

Lee Fesperman

Bruce said:
I need to build a proxy server that will keep alive an asynchronous
connection with a client and a remote server until the client closes. The
type of data is ASCII.

I've tried this code it doesn't work?

//needs to keep client socket & server socket open and wait for messages
from client and returns from server

class CommSession extends Thread{
Socket clie;
Socket serv;
CommSession(Socket clie,Socket serv){
this.clie=clie;
this.serv=serv;
}

public void run(){

try{
System.out.println("set up session on "+serv);
while(!clie.isClosed()){
DataInputStream in = new
DataInputStream(clie.getInputStream());
DataOutputStream out = new
DataOutputStream(serv.getOutputStream());
String cmd="";
while((cmd=in.readLine())!=null){
out.writeBytes(cmd+"\n");
System.out.println("TO-SERVER:"+cmd);
}

DataInputStream in2 = new
DataInputStream(serv.getInputStream());
DataOutputStream out2 = new
DataOutputStream(clie.getOutputStream());
String cmd2="";
while((cmd2=in2.readLine())!=null){
out2.writeBytes(cmd2+"\n");
System.out.println("TO-CLIENT:"+cmd2);
}

}

}catch(Exception ss){ss.printStackTrace();}
}

}

The isClosed() method for java.net.Socket only tests if you explicitly closed the
socket. It does not test for the other side closing. You need to use the fact that
readLine() returned null to determine of the client closed the socket.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top