poor java socket read performance
Hello, I'm just learning network programming and I've made a test program that checks the latest versions of various software. I'm using the HTTPSession (reinvented the wheel on purpose) to retreive webpages, but the time it takes is excessive. I find that it would read a few hundred bytes, hang, read a some more until it's done. I ran this program under both Windows and Linux to see if was due to ip implementation differences, but in both cases, using BufferedReader to read a socket is much faster than GetPage. I also suspected that this problem could be because I'm using the latest Java Mustang releases. Any ideas what the problem is?
public class HTTPSession
{
HTTPSession()
{
}
void Open(String host_name) throws IOException
{
m_socket = new Socket(host_name, 80);
m_out = new PrintStream(m_socket.getOutputStream());
m_in = m_socket.getInputStream();
m_host_name = host_name;
}
String GetPage(String path) throws IOException
{
String string = "", s;
m_out.print("GET " + path + " HTTP/1.1\n" +
"Host: " + m_host_name + "\n\n");
m_out.flush();
byte buffer[] = new byte[8192];
int bytes_read;
while ((bytes_read = m_in.read(buffer)) != -1)
{
// fixme: this will not work for non ascii encoded web pages
// and very bad in terms of memory fragmentation
for (int i = 0; i < bytes_read; ++i)
string += (char)buffer[i];
}
return string;
}
String m_host_name;
private Socket m_socket;
private PrintStream m_out;
private InputStream m_in;
}
|