Toon posts:

van multithreaded naar singlethreaded..

Pagina: 1
Acties:
  • 34 views sinds 30-01-2008

Verwijderd

Topicstarter
Hi folks,

Ik probeer van een multithreaded webserver een singlethreaded webserver te maken maar t wil nog niet echt lukken. Ik denk dat de truc in dit stukje zit:


public void run() {
try {
processRequest();
} catch (Exception e) {
System.out.println(e);
}
}


Kan iemand mij vertellen hoe ik van de volgende code een singlethreaded webserver maak?

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package mypackage1;

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

class ClassStringTokenizer{
   public static void main(String args[]) throws IOException {

        //geef een poort waarnaar de ServerSocket luistert
        ServerSocket portlistener = new ServerSocket(12345);
        while (true) {

        try{
            HttpRequest request= new HttpRequest (portlistener.accept()); 
            Thread thread = new Thread (request);
            thread.start();
        }
        catch(Exception e){}

        
            

      }
   }
}

final class HttpRequest implements Runnable {

    final static String CRLF ="\r\n";
    Socket socket;

    public HttpRequest(Socket socket) throws Exception {
        this.socket = socket;
    }

   public void run() {
        try {
            processRequest();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

            private void processRequest() throws Exception {
            InputStreamReader socketstream =  new InputStreamReader(this.socket.getInputStream());
            BufferedReader socketbuffer = new BufferedReader(socketstream);
            String requestLine=socketbuffer.readLine();
            DataOutputStream socketoutput = new DataOutputStream(this.socket.getOutputStream());
            System.out.println(requestLine);

            
            StringTokenizer tokens = new StringTokenizer(requestLine);
            tokens.nextToken();
            String fileName= tokens.nextToken();

            if(fileName.startsWith("/"))
            fileName = fileName.substring(1,fileName.length());

                  //Open the requested file.  
                  FileInputStream fis = null;
                  boolean fileExists = true;

                  try {
                  fis = new FileInputStream(fileName);
                  } catch (FileNotFoundException e) {
                  fileExists = false;
                  }

                  // Construct the response message.
                  String statusLine = null;
                  String contentTypeLine = null;
                  String entityBody = null;

                  if (fileExists) {
                      statusLine = "HTTP/1.0 200 OK" + CRLF;
                      contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
                  } else {
                      statusLine = "HTTP/1.0 404 Not Found" + CRLF; 
                      contentTypeLine = "NONE";
                      entityBody = "\n\n Not Found";
                  }

        // Send the status line.
        socketoutput.writeBytes(statusLine);
        
        // Send the content type line.
        socketoutput.writeBytes(contentTypeLine);
        
        // Send a blank line to indicate the end of the header lines.
        socketoutput.writeBytes(CRLF);
        
        // Send the entity body.
        if (fileExists) {
            sendBytes(fis, socketoutput);
            fis.close();
        } else {
            socketoutput.writeBytes(entityBody);
        }
        
        //Close the streams
        socketoutput.close();
        socketbuffer.close();
        socket.close();
    }

    private String contentType(String fileName) {
        if(fileName.endsWith(".htm") || fileName.endsWith(".html"))
            return "text/html";
        else if(fileName.endsWith(".gif"))
            return "image/gif";
        else if(fileName.endsWith(".txt"))
            return "text/plain";
        else
            return "application/octet-stream";
    }
         
    private static void sendBytes(FileInputStream fis, OutputStream socketoutput) throws Exception {
        // Construct a 1K buffer to hold bytes on their way to the socket.
        byte[] buffer = new byte[1024];
        int bytes = 0;
        
        // Copy requested file into the socket's output stream.
        while((bytes = fis.read(buffer)) != -1 )
            socketoutput.write(buffer, 0, bytes);
    }
}


Thanks alvast!!!

  • GX
  • Registratie: Augustus 2000
  • Laatst online: 14-05-2025

GX

Nee.

*!*!* Over topictitels in P&W - Updated 25 feb 04 *!*!*
Welkom in P&W: FAQ en Beleid *updated: 28-04*
:*

(Dit gaat om Java?)

[ Voor 4% gewijzigd door GX op 07-05-2004 22:32 ]


Verwijderd

waarom zou je in godsnaam een single threaded webserver willen maken?


--edit

trouwens ik denk dat je thread stukje zich eerder in deze code bevindt....

Java:
1
2
3
4
5
        try{
            HttpRequest request= new HttpRequest (portlistener.accept()); 
            Thread thread = new Thread (request);
            thread.start();
        }

[ Voor 69% gewijzigd door Verwijderd op 07-05-2004 22:36 ]


  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 15:00

gorgi_19

Kruimeltjes zijn weer op :9

^^^^ Zie boven me.

We gaan hier niet je applicatie voor je aanpassen. Je hebt zelf een idee waar het aan kan liggen, waarom probeer je het niet uit? :) "Debug mijn topic" of "wie kan dit topic voor mij aanpassen" zijn we hiet niet zo dol op.

Lees P&W FAQ - De "quickstart" eens door en creer een topic met alleen de relevante code met je eigen ideeen en foutmeldingen, wat er wel werkte en wat er niet werkte.

Neem volgende keer ook een duidelijke topictitle.

Deze gaat in ieder geval dicht.

[ Voor 21% gewijzigd door gorgi_19 op 07-05-2004 22:37 ]

Digitaal onderwijsmateriaal, leermateriaal voor hbo


  • curry684
  • Registratie: Juni 2000
  • Laatst online: 12-05 22:23

curry684

left part of the evil twins

gorgi_19 is aan het prutsen ;)

Professionele website nodig?


Dit topic is gesloten.