Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading from socket is slow

843790Aug 20 2007 — edited Aug 20 2007
I have a ServerSocket.accept() loop running in an applet that is accepting connections from a desktop java application. Upon receipt of a connection, a new thread is spawned to handle the socket. In the thread, all I am doing is wrapping the socket's input stream in a BufferedReader and trying to read a single line of data from the socket.

When a client desktop application sends data to the server applet, opening the socket happens in less than 1ms, as does retrieving and wrapping the socket's input stream. The problem is that reading that single line of data (about 14 characters) from the socket takes anywhere from 25 to 65 seconds.

The client and server are both running on the same machine, the server bound to 127.0.0.1 on a high port, and as I said, everything works properly except for the extreme sluggishness of reading from the socket. Does anyone know why this is?

Example problem code, runs in an applet with no security restrictions:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

public class Handler extends Thread {
	
	// Our socket object
	private Socket conn;
	
	public Handler( Socket conn ) {
		// conn is the socket connection
		this.conn = conn;
	}
	
	public void run() {

		try {

			// Open reader for the socket
			BufferedReader in = new BufferedReader( 
					new InputStreamReader(this.conn.getInputStream())
					);
		
			// This is the problematic line, takes anywhere from
			// 25-65 seconds.
			String ID = in.readLine();
			
			System.out.println(ID);
			
			in.close();
			this.conn.close();
			
		} catch( IOException ioe ) {
			System.out.println( ioe.getMessage() );
		}
	} 

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 17 2007
Added on Aug 20 2007
2 comments
127 views