Συζητήσεις για γλώσσες προγραμματισμού και θέματα σχετικά με προγραμματισμό.
-
PaP
- Venus Project Founder
- Posts: 1077
- Joined: Wed Apr 21, 2004 12:06 am
- Academic status: Alumnus/a
- Location: San Francisco
-
Contact:
Post
by PaP » Sat May 15, 2004 5:07 pm
Επανέρχομαι και πάλι με νέο πρόβλημα το οποίο έχει να κάνει με τα threads
Code: Select all
public class petition implements Runnable {
Thread T;
public void run() {
try {
//Analyse petition
Petition_Analysis.Analyse_Petition(in);
//Then Attend it
if (Petition_Analysis.pt.Version.equals("v10"))
Http_10.Attend_Petition(Petition_Analysis.pt, out, extra.detectConnectionAlive());
else if (Petition_Analysis.pt.Version.equals("v11"))
Http_11.Attend_Petition(Petition_Analysis.pt, out, extra.detectClose());
else {
out.print("HTTP/1.0 400 Bad Request\r\n\r\n");
out.close();
in.close();
client.close();
newSock = true;
}
//The following function immediately prints the contents of the PrinterBuffer
out.flush();
System.gc();
}
catch (MalformedURLException murle) {
out.print("HTTP/1.0 400 Bad Request\r\n\r\n");
try { client.close(); }
catch(IOException ioe) { System.out.println(ioe); }
newSock = true;
}
catch (NullPointerException npe) {}
//try - analyse petition & attend it
catch (IOException ioe){
System.out.println(ioe);
}
} // -- run()
} // --class: thread
Και το καλώ από εδώ αλλά ο compiler που λεει κάτι χαζά ότι δεν μπορεί να καλέσει non-static. Αλλά αν κάνω την τάξη static και πάλι δεν κάνει κάτι αλλά είναι και χαζό να δηλώσω ένα thread static.
Code: Select all
try {
//Creating if needed a new socket where the clients listens
if (newSock) {
client = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
System.out.println("LOG: Client is listening on port: "+client.getPort());
}
petition th = new petition();
th.run();
} // try- create input and output buffers
catch (UnknownHostException e) {
System.err.println("JHTTP Server could establish a connection. Exiting...");
}
catch (IOException e) {
System.err.println("Could not get I/O for the connection to client.");
} //try - create client sockets, wait and attend connection
Any ideas ?
-
Einherjar
- Venus Project Founder
- Posts: 3751
- Joined: Tue Jan 27, 2004 4:42 pm
- Academic status: Alumnus/a
- Gender: ♂
- Location: Washington DC, USA
-
Contact:
Post
by Einherjar » Sat May 15, 2004 7:59 pm
Νομίζω ότι το πρόβλημα έγκειται στο ότι κάνεις
implement το Runnable. Αφού το κάνεις αυτό θα έπρεπε να κάνεις implement και την μέθοδο
public void start().
Η τακτική που χρησιμοποιείται στις περιπτώσεις που έχουμε threads είναι
- Αν θες να κάνεις extend μια άλλη κλάση τότε κάνεις implement Runnable όπως έχεις κάνει σε συνδιασμό με αυτό που σου είπα
- Αν δεν υπάρχει τέτοια ανάγκη κάνεις απλά extens Thread φτιιάχνεις και την run που γουστάρεις.
Σε όλες τις περιπτώσεις πάντως, για να ξεκινήσει το thread κάνεις
Code: Select all
classname myThread = new classname(params);
myThread.start(); //όχι myThread.run();
[Better to understand a little than to misunderstand a lot]
-
PaP
- Venus Project Founder
- Posts: 1077
- Joined: Wed Apr 21, 2004 12:06 am
- Academic status: Alumnus/a
- Location: San Francisco
-
Contact:
Post
by PaP » Sat May 15, 2004 10:02 pm
Αλλαξα τον κώδικα και τώρα τρέχει ΟΚ. Αλλά αφού εξυπηρετήσει ένα petition ο server κλείνει ενώ δεν πρέπει. Επίσης πρέπει να φτιάξω ειδικές συναρτήσεις monitors για τα PrintWriter & BufferedReader ?
Code: Select all
package jhttp_server;
import java.io.*;
import java.net.*;
import java.lang.*;
/**
* <p>Title: JHTTP_server</p>
* <p>Description: A simple HTTP server implemented with Java</p>
* <p>Copyright: Copyright (c) 2004 - Lisenced under GPL</p>
* <p>Company: Universidad Rey Juan Carlos</p>
* @author Panayiotis Papadopoulos -> papado@freemail.gr
* @version 0.1
*/
public class jserver {
static ServerSocket serverSocket = null;
static PrintWriter out;
static BufferedReader in;
static Socket client = null;
static short i=0;
static int port;
static boolean newSock=true;
public static void main(String[] args) throws IOException {
port = Integer.valueOf(args[0]).intValue();
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e) {
System.out.println("Must Enter a port number");
}
System.out.println("|------------------------------------------------------------------------|");
System.out.println("|JHTTP Server initialized and accepting connections on port:" +
serverSocket.getLocalPort()+"\t |");
System.out.println("|------------------------------------------------------------------------|");
while(true){
while (i < 5) {
i++;
try {
//Creating if needed a new socket where the clients listens
if (newSock) {
client = serverSocket.accept();
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
System.out.println("LOG: Client is listening on port: "+client.getPort());
}
//Here the server creates a new thread for the petition
petition newPet = new petition();
newPet.T.start();
} // try- create input and output buffers
catch (UnknownHostException e) {
System.err.println("JHTTP Server could establish a connection. Exiting...");
}
catch (IOException e) {
System.err.println("Could not get I/O for the connection to client.");
} //try - create client sockets, wait and attend connection
} //while 5 petitions
System.out.println("Session Closed. 5 continious petitions served.");
out.close();
in.close();
client.close();
newSock = true;
System.gc();
i=0;
} // while - forever
}// -------------------main-------------------
}// jserver - main class of JHTTP server project
class petition implements Runnable {
Thread T;
petition () {
T = new Thread(this);
T.start();
}
public void run() {
try {
//Analyse petition
Petition_Analysis.Analyse_Petition(jserver.in);
//Then Attend it
if (Petition_Analysis.pt.Version.equals("v10"))
Http_10.Attend_Petition(Petition_Analysis.pt, jserver.out, extra.detectConnectionAlive());
else if (Petition_Analysis.pt.Version.equals("v11"))
Http_11.Attend_Petition(Petition_Analysis.pt, jserver.out, extra.detectClose());
else {
jserver.out.print("HTTP/1.0 400 Bad Request\r\n\r\n");
jserver.out.close();
jserver.in.close();
jserver.client.close();
jserver.newSock = true;
}
//The following function immediately prints the contents of the PrinterBuffer
jserver.out.flush();
System.gc();
}
catch (MalformedURLException murle) {
jserver.out.print("HTTP/1.0 400 Bad Request\r\n\r\n");
try { jserver.client.close(); }
catch(IOException ioe) { System.out.println(ioe); }
jserver.newSock = true;
}
catch (NullPointerException npe) {}
//try - analyse petition & attend it
catch (IOException ioe){
System.out.println(ioe);
}
} // -- run()
} // --class: thread
-
Einherjar
- Venus Project Founder
- Posts: 3751
- Joined: Tue Jan 27, 2004 4:42 pm
- Academic status: Alumnus/a
- Gender: ♂
- Location: Washington DC, USA
-
Contact:
Post
by Einherjar » Sun May 16, 2004 1:32 am
Σου άλλαξα κάποια πράγματα και θα κάνει αυτό που θέλεις (αν κατάλαβα καλά τι θέλεις)
jhttppackage jhttp_server;
import java.io.*;
import java.net.*;
public class jserver {
static ServerSocket serverSocket = null;
static short i=0;
static int port;
static boolean newSock=true;
public static void main(String[] args) throws IOException {
port = Integer.valueOf(args[0]).intValue();
try {
serverSocket = new ServerSocket(port);
}
catch (IOException e) {
System.out.println("Must Enter a port number");
}
System.out.println("|------------------------------------------------------------------------|");
System.out.println("|JHTTP Server initialized and accepting connections on port:" +
serverSocket.getLocalPort()+"\t |");
System.out.println("|------------------------------------------------------------------------|");
while(true){
while (i < 5) {
i++;
try {
//Creating if needed a new socket where the clients listens
if (newSock) {
new petition(serverSocket.accept());
}
} // try- create input and output buffers
catch (UnknownHostException e) {
System.err.println("JHTTP Server could establish a connection. Exiting...");
}
catch (IOException e) {
System.err.println("Could not get I/O for the connection to client.");
} //try - create client sockets, wait and attend connection
} //while 5 petitions
System.out.println("Session Closed. 5 continious petitions served.");
newSock = true;
System.gc();
i=0;
} // while - forever
}// -------------------main-------------------
}// jserver - main class of JHTTP server project
petition
Code: Select all
package jhttp_server;
import java.io.*;
import java.net.*;
class petition extends Thread{
Socket client;
PrintWriter out;
BufferedReader in;
petition (Socket s) {
client=s;
}
public void run() {
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream());
System.out.println("LOG: Client is listening on port: "+client.getPort());
}catch (IOException e) {
System.err.println("Could not get I/O for the connection to client.");
} //try - create client sockets, wait and attend connection
try {
//Analyse petition
Petition_Analysis.Analyse_Petition(jserver.in);
//Then Attend it
if (Petition_Analysis.pt.Version.equals("v10"))
Http_10.Attend_Petition(Petition_Analysis.pt, jserver.out, extra.detectConnectionAlive());
else if (Petition_Analysis.pt.Version.equals("v11"))
Http_11.Attend_Petition(Petition_Analysis.pt, jserver.out, extra.detectClose());
else {
out.print("HTTP/1.0 400 Bad Request\r\n\r\n");
out.close();
in.close();
client.close();
newSock = true;
}
//The following function immediately prints the contents of the PrinterBuffer
jserver.out.flush();
System.gc();
}
catch (MalformedURLException murle) {
jserver.out.print("HTTP/1.0 400 Bad Request\r\n\r\n");
try { jserver.client.close(); }
catch(IOException ioe) { System.out.println(ioe); }
jserver.newSock = true;
}
catch (NullPointerException npe) {}
//try - analyse petition & attend it
catch (IOException ioe){
System.out.println(ioe);
}
out.close();
in.close();
client.close();
} // -- run()
} // --class: thread
[Better to understand a little than to misunderstand a lot]