mirror of
https://github.com/balkian/SWCM.git
synced 2024-12-27 20:48:11 +00:00
Java version with simple thread management added
This commit is contained in:
parent
ad3297b84b
commit
ad903e0124
55
ServidorConcurrente/HebraServ.java
Normal file
55
ServidorConcurrente/HebraServ.java
Normal file
@ -0,0 +1,55 @@
|
||||
package ServidorConcurrente;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
class HebraServ extends Thread {
|
||||
|
||||
private Socket c;
|
||||
|
||||
public HebraServ (Socket conn) {
|
||||
c = conn;
|
||||
}
|
||||
|
||||
public void run () {
|
||||
try {
|
||||
byte[] body, header, lc = {13,10};
|
||||
String l, code, x, path="", CR_LF = new String(lc,"US-ASCII");
|
||||
int j, n = 0;
|
||||
|
||||
LineNumberReader netIn = new LineNumberReader(new InputStreamReader(c.getInputStream(), "UTF-8"));
|
||||
OutputStream netOut = c.getOutputStream();
|
||||
|
||||
System.out.println(l = URLDecoder.decode(netIn.readLine(), "UTF-16"));
|
||||
if (l.matches("GET .*")) {
|
||||
path=l.replaceFirst("GET[ ]+/([^ \\&#]*)[ \\&#].*", "$1" );
|
||||
if ((path.matches(""))||(path.matches(".*/"))) { path += "index.html" ; }
|
||||
|
||||
while (!(x = netIn.readLine()).equals("")) { System.out.println(x); }
|
||||
|
||||
try {
|
||||
RandomAccessFile i = new RandomAccessFile(path, "r");
|
||||
i.read(body = new byte[(int)i.length()]);
|
||||
code = "200 OK";
|
||||
|
||||
} catch (FileNotFoundException f) {
|
||||
body = ("<html><body><h1> Recurso no encontrado </h1>" + path + "</body></html>").getBytes("US-ASCII");
|
||||
code ="404 Not Found";
|
||||
}
|
||||
|
||||
} else {
|
||||
body = ("<html><body><h1>Comando desconocido</h1></body></html>").getBytes("US-ASCII");
|
||||
code = "400 Bad Request";
|
||||
}
|
||||
|
||||
header = ("HTTP/1.0 " + code.toString() + CR_LF + "Content-type: text/html" + CR_LF + "Content-length: " + String.valueOf(body.length) + CR_LF + CR_LF).getBytes("US-ASCII");
|
||||
netOut.write(header);
|
||||
netOut.write(body);
|
||||
netOut.flush();
|
||||
c.close();
|
||||
} catch (IOException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
33
ServidorConcurrente/ServWebPatternConc.java
Normal file
33
ServidorConcurrente/ServWebPatternConc.java
Normal file
@ -0,0 +1,33 @@
|
||||
package ServidorConcurrente;
|
||||
|
||||
import java.util.*;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
|
||||
public class ServWebPatternConc {
|
||||
public static int maxCon = 10;
|
||||
public static void main (String args[]) {
|
||||
try {
|
||||
ServerSocket serv = new ServerSocket(8080);
|
||||
System.out.println("showserver created at port 8080.");
|
||||
ThreadGroup workers = new ThreadGroup("WebWorkers");
|
||||
|
||||
while (true) {
|
||||
if(workers.activeCount()<maxCon){
|
||||
System.out.println("Hay "+workers.activeCount()+" hebras corriendo.");
|
||||
Socket conn = serv.accept();
|
||||
Thread hs = new Thread(workers, new HebraServ(conn));
|
||||
hs.start();
|
||||
}
|
||||
else{
|
||||
try{
|
||||
Thread.sleep(500);
|
||||
}
|
||||
catch(Exception ex){
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e) { System.err.println(e); }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user