Name: boT120536 Date: 07/22/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
The following program traces the communication between a TCP-client (e.g.
Browser) and a server. After accepting a connection it starts two threads.
One copies bytes from the client to the server, the other vice versa. If the
server closes the connection, the program should close the connection with
the client. Therefore it calls "clnt.close()" in line 57. However, the
connection is not closed, it is still "ESTABLISHED". As far as I can assume,
the cause is the still running thread that is supposed to read from the
client and copy to the server. But in my understanding, Socket.close()
should force the network connection to be closed, even if an associated
InputStream is still open. (BTW, I tried closing the associated input stream
before closing the socket - makes no difference.)
I tried this with RedHat7.1 both with the standard kernel and an updated
kernel (2.4.5).
/**
*
*/
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
class FilterProxy {
// incomming port number
private static String portIn;
// outgoing port number
private static String portOut;
// target server
private static String targetServer;
public static class ProxyServer extends Thread {
Socket clnt;
Socket srv;
public ProxyServer (Socket s) {
clnt = s;
}
public void run () {
InputStream in = null;
OutputStream out = null;
try {
srv = new Socket
(InetAddress.getByName(targetServer),
Integer.parseInt(portOut));
final InputStream clntIn = clnt.getInputStream ();
final Thread p1 = new Pipe
(clntIn, srv.getOutputStream (), "--> ",
new PipeEOFHandler () {
public void gotEOF () {
System.out.println ("Request received.");
try {
clnt.shutdownInput();
srv.shutdownOutput();
} catch (IOException e) {
e.printStackTrace();
}
}
});
final Thread p2 = new Pipe
(srv.getInputStream (), clnt.getOutputStream (), "<-- ",
new PipeEOFHandler () {
public void gotEOF () {
System.out.println ("Answer sent.");
try {
srv.shutdownInput();
clnt.shutdownOutput();
clnt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
p1.start ();
p2.start ();
p1.join ();
p2.join ();
srv.close ();
} catch (InterruptedException ie) {
System.out.print ("Exception: " + ie + "\n");
System.out.flush();
} catch (IOException e) {
System.out.print ("Exception: " + e + "\n");
System.out.flush();
} finally {
if (srv != null) {
try {
srv.close ();
} catch (IOException ie) {
}
}
try {
clnt.close ();
} catch (IOException ie) {
}
}
}
}
public static interface PipeEOFHandler {
void gotEOF();
}
public static class Pipe extends Thread {
InputStream in;
OutputStream out;
String prefix;
PipeEOFHandler eofHandler;
public Pipe (InputStream from, OutputStream to,
String prfx, PipeEOFHandler peh) {
in = from;
out = to;
prefix = prfx;
eofHandler = peh;
}
public void run () {
int c;
try {
StringBuffer sb = new StringBuffer(100);
boolean wasCR = false;
while ((c = in.read ()) != -1) {
out.write ((char)c);
if ((c != '\n') && (c != '\r')) {
sb.append ((char)c);
}
if (c == '\r' || (c == '\n' && !wasCR)) {
System.out.println (prefix + sb.toString());
System.out.flush();
sb = new StringBuffer (100);
wasCR = (c == '\r');
}
else {
wasCR = false;
}
}
in.close ();
out.flush();
out.close ();
} catch (IOException e) {
System.out.print ("While closing Streams in Pipe: "
+ e.getMessage());
System.out.flush ();
} finally {
if (eofHandler != null) {
eofHandler.gotEOF();
}
}
}
}
/**
* Hauptschleife f?r den Thread.
*/
public static void run() {
try {
int port = Integer.parseInt(portIn);
int calls = 0;
ServerSocket httpd = null;
try {
httpd = new ServerSocket(port, 50, null);
} catch (IOException e) {
System.err.println(e.toString());
System.exit(1);
}
while (true) {
Socket socket = httpd.accept();
System.out.println
("Connection from: "
+ socket.getInetAddress().getHostName());
ProxyServer tt = new ProxyServer(socket);
tt.start();
}
} catch (IOException e) {
System.err.println(e.toString());
System.exit(1);
}
}
public static void main (String args[]) throws FileNotFoundException {
try {
portIn = args[0];
portOut = args[1];
targetServer = args[2];
System.out.println( "\nportIn - " + portIn
+ "\nportOut - " + portOut
+ "\ntargetServer - " + targetServer);
run ();
} catch (ArrayIndexOutOfBoundsException ae) {
System.out.println ("parameter not found!");
System.out.println ("sample: java -classpath . FilterProxy portIn portOut
targetServer");
System.out.println (" java -classpath . FilterProxy 80 8680
134.101.26.35");
}
}
}
(Review ID: 127550)
======================================================================
- duplicates
-
JDK-4344135 Linux: close is not preemptive
- Closed