Hi,

Working with HTTP servers written in Java, I've hit upon
a stumbling block, where all Linux JVMs I've tested work
quite differently from the Win32-ones:

I've written a small Java webserver (attached).
This accepts both GET and POST events,
and under Win32 this works fine.

However, if I run under Linux (tested: Blackdown, IBM),
when I POST to the webserver, the browser almost always receives a
"connection reset by peer" message-

It seems that whenever the server receives a POST message,
it will prematurely close the socket connection to the browser
- even though flush() has been called.

A workaround is to add a long delay before closing the socket,
but this is clearly not acceptable.

Can anyone explain why the Linux VMs work in this way?
Does anyone know of a workaround?


Thanks,

-Jesper Juul
Soup.dk


Source code:
******************

import java.net.*;
import java.io.*;
import java.util.*;

/**
MicroWebServer2 testing POST events under Linux.
@author Jesper Juul - [EMAIL PROTECTED]
*/

public class MicroWebServer2
{
int port=81;

public static void main(String args[])
{
MicroWebServer2 mws=new MicroWebServer2();
mws.doIt();
}

public void doIt()
{
try
{
ServerSocket sso=new ServerSocket(port);
System.out.println("MicroWebServer2");
System.out.println("Waiting for sockets connections on port "+port);
while(true)
{
  Socket so=sso.accept();
  new HTTPHandler(so).start();
}

}
catch(Exception e)
{
  System.out.println("ServerSocket error: "+e);
}
}

}


class HTTPHandler extends Thread
{
Socket so;

HTTPHandler(Socket so)
{
  this.so=so;
}

int linecount=0;

public void run()
{
  try
{
DataInputStream dis=new DataInputStream(so.getInputStream());

boolean post=false;

String firstline=null;
int readlen=0;

while(true)
{
  String s=dis.readLine();
//System.out.println("Line: "+s);
if (firstline==null)
{
firstline=s;
if (s.startsWith("POST ")) post=true;
}
if (s.startsWith("Content-Length:"))
{
StringTokenizer st=new StringTokenizer(s," ");
st.nextToken();
readlen=Integer.parseInt(st.nextToken().trim());
}
if ((s==null)||(s.length()==0)) break;
linecount++;
}

if (linecount==0)
{
System.out.println("Got empty request!");
  }
else
{
DataOutputStream dos=new DataOutputStream(so.getOutputStream());


if (post)
{
byte data[]=new byte[readlen];
dis.readFully(data);
String text=new String(data);
System.out.println("POST: "+text);
}
else
{
System.out.println("GET: "+firstline);
}

String out="<html><body>MicroWebServer2!</body></html>";

String header = "HTTP/1.0 200 OK\n" +
                "Allow: GET, POST\n"+
                "MIME-Version: 1.0\n"+
                "Content-Length: " + out.length() +
                "\n\n";

dos.writeBytes(header);
dos.writeBytes(out);
dos.flush();
dos.close();
System.out.println("Done!");
}
}
catch(Exception e)
{
  System.out.println("HTTPHandler err: "+e);
}
}

}

********

Sample form to post:

**********

<html>
<head>
<title>Feedback form</title>
</head>
<body bgcolor="#ffffff">
<form action="http://localhost:81/test" method="POST">
     <input type="text" name="textfield" value="some text">
     <input type="submit" name="send" value="send">
</form>
</body>
</html>

***********************************
soup.dk
Online games and chat
Oehlenschlaegersgade 66, 3.th.
DK-1663 Copenhagen V
Denmark
+45 33 24 81 62


----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to