0

I have a web server (started from a service):

public class WebServer extends Thread {

    private boolean isRunning = false;
    protected Context context = null;
    private int serverPort;

    private BasicHttpProcessor httpproc = null;
    private BasicHttpContext httpContext = null;
    private HttpService httpService = null;
    protected HttpRequestHandlerRegistry registry = null;
    private NotificationManager notifyManager = null;

    public WebServer(Context context, NotificationManager notifyManager, String serverName, int serverPort) {
        super(serverName);
        this.setContext(context);
        this.setNotifyManager(notifyManager);
        this.serverPort = serverPort;

//      SharedPreferences pref =
//              PreferenceManager.getDefaultSharedPreferences(context);

        this.httpproc = new BasicHttpProcessor();
        this.httpContext = new BasicHttpContext();

        this.httpproc.addInterceptor(new ResponseDate());
        this.httpproc.addInterceptor(new ResponseServer());
        this.httpproc.addInterceptor(new ResponseContent());
        this.httpproc.addInterceptor(new ResponseConnControl());

        this.httpService =
                new HttpService(this.httpproc, new DefaultConnectionReuseStrategy(),
                        new DefaultHttpResponseFactory());

        this.registry = new HttpRequestHandlerRegistry();

        setHandlerRegistry();
        this.httpService.setHandlerResolver(this.registry);
    }

    protected void setHandlerRegistry() {}

    @Override
    public void run() {
        super.run();

        try {
            ServerSocket serverSocket = new ServerSocket(this.serverPort);

            serverSocket.setReuseAddress(true);

            while (this.isRunning) {
                try {
                    final Socket socket = serverSocket.accept();

                    DefaultHttpServerConnection serverConnection =
                            new DefaultHttpServerConnection();

                    serverConnection.bind(socket, new BasicHttpParams());

                    this.httpService.handleRequest(serverConnection, this.httpContext);

                    serverConnection.shutdown();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                catch (HttpException e) {
                    e.printStackTrace();
                }
            }

            serverSocket.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

And sometimes, there is a error message (trace) logged for this code line when I receive some data from my client; this error is random:

  this.httpService.handleRequest(serverConnection, this.httpContext);

The error message is: W/System.err(1052) at ....

Thank you guys !

anthony
  • 7,653
  • 8
  • 49
  • 101
  • 1
    buddy paste your full stack trace... – Bhavin Nattar Mar 13 '14 at 13:34
  • I added IP address like that: ServerSocket serverSocket = new ServerSocket(this.serverPort, 1, getLocalIpAddress()); The running behavior is the same; when there are a lot of requests from my client, sometimes the message error appears: **W/System.err (1686)** – anthony Mar 13 '14 at 14:20
  • The detail (via a debug breakpoint in Exception) is: sendto failed: ECONNRESET (Connection reset by peer) – anthony Mar 13 '14 at 14:31
  • Learn to use logcat (rather than the debugger) to get the full stack trace. Also add log messages in your server code. – Chris Stratton Mar 13 '14 at 15:34

0 Answers0