ich versuche mich jetzt seit ein paar tagen an einem multithreaded socketserver , der auch schon ganz gut lief bis ich mich dazu entschieden habe anstelle eines readLine (zeilenweise einlesen) dazu entschlossen hab nur zeichenweise einzulesen und die zeilen dann in einem anderen thread zusammen zu setzen und in einem driten diese abzuarbeiten.
soweit so gut nachdem ich es nun mehrfach mit future versucht hatte und... ach egal seht selbst den nicht laufenden code, ich denke es wird klar was ich will, kann mir bitte einer helfen, mir zmindest sagen warum mir eclipse nach dem ersten new runable eine ")" haben will :cry:
Gruss Sascha
soweit so gut nachdem ich es nun mehrfach mit future versucht hatte und... ach egal seht selbst den nicht laufenden code, ich denke es wird klar was ich will, kann mir bitte einer helfen, mir zmindest sagen warum mir eclipse nach dem ersten new runable eine ")" haben will :cry:
Code:
import java.awt.Color;
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.*;
class ClientWorker implements Runnable {
private Socket client;
private JTextArea textArea;
ClientWorker(Socket client, JTextArea textArea) {
/*
implementieren zweier stacks
*/
Stack readerStack = new Stack();
Stack ComandStack = new Stack();
this.client = client;
this.textArea = textArea;
};
ExecutorService executorService = Executors.newCachedThreadPool();//threadpool for the stackparser and comand parser
//executorService.execute(new Runnable() {
public void run(){//*************************reader task
System.out.println("thread started");
String line;
//String XMLline;
BufferedReader in = null;
PrintWriter out = null;//kill outputstream if exists as object b4
try{
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(client.getOutputStream(), true);//define "out" as outputstreamobject
System.out.println("read in out");
} catch (IOException e) {
System.out.println("in or out failed");
System.exit(-1);
}
int ch;
try {
while( (ch = in.read()) != -1){//&& ((ch>=0)|| myOldTimer<=myActualTimer-100)// && ((myStack[myStack.length-2] != 10) && (myStack[myStack.length-1] != 0))
readerStack.push(ch);
System.out.println(ch+" was pushed to readerStack");
}
System.out.println("closing door");
in.close();
} catch (IOException e) {
System.out.println("Read error or server lost internetconnection" + e + "\n--\n stoping reader thread");
Thread.currentThread().stop();
};
executorService.execute(new Runnable() {
public void run(){//*************************stackparser task
Object tempComandObject;
String tempComandString = "";
int apeXMLTest;
int apeComandStart;
int apeComandEnd;
String apeComandString;
try {
while (in){
if(!readerStack.empty()){
tempComandObject = readerStack.pop();
tempComandString.concat(String.valueOf(URLEncoder.decode("%" + tempComandObject.toString(), "UTF-8")).trim());//push to string and convert to usual signs.
apeXMLTest = tempComandString.indexOf("<?xml");
apeComandStart = tempComandString.indexOf("<apeCommandDocument apeCommandID=");
apeComandEnd = tempComandString.indexOf("</apeCommandDocument apeCommandID=");//16stelligeID
if (apeXMLTest != -1 && apeComandStart != -1 && apeComandEnd != -1){
ComandStack.push(tempComandString.substring(apeComandStart, apeComandEnd+31));
// here do some logging
tempComandString = tempComandString.substring(apeComandEnd+31, tempComandString.length);//deleting all carbage
} else {
//wait();//because there is no full command inside
}
} else {
//wait();//because readStack is empty
}
}//while
} catch (NullPointerException e) {
//blabla
} catch ( ThreadDeath td ) {
System.out.println( "ThreadDeath" );
throw td;
}
}};
executorService.execute(new Runnable() {
public void run(){//*************************parse the comands and do the job
Object tempComandExecutionObject;
String tempComandString = "";
int apeXMLTest;
int apeComandStart;
int apeComandEnd;
String apeComandString;
try {
while (in){
if(!comandStack.empty()){
apeComandObject = comandStack.pop();
apeComandString = tempComandObject.toString();//get the comandline as string
tempApeComandStart= apeComandString.indexOf("<apeComand>");
tempApeComandEnd = apeComandString.indexOf("</apeComand>");
apeComand = apeComandString.substring(tempApeComandStart+11,tempApeComandEnd).trim();
tempApeComandValueStart= apeComandString.indexOf("<apeComandValue>");
tempApeComandValueEnd = apeComandString.indexOf("</apeComandValue>)";
apeComandValue = apeComandString.substring(tempApeComandStart+16,tempApeComandEnd).trim();
// here do the action or even call a future to do it
} else {
//wait();//because comandStack is empty
}
}
} catch (NullPointerException e) {
//blabla
} catch ( ThreadDeath td )
{
System.out.println( "ThreadDeath" );
throw td;
}
}};
};
}
/*
den teil hier habe ich eigentlich nicht verändert bis auf ein paar namensänderungen
*/
class apeSocketThrdServer extends JFrame{
JLabel label = new JLabel("Text received over socket:");
JPanel panel;
JTextArea textArea = new JTextArea();
ServerSocket server = null;
apeSocketThrdServer(){ //Begin Constructor
panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.white);
getContentPane().add(panel);
panel.add("North", label);
panel.add("Center", textArea);
} //End Constructor
public void listenSocket(){
try{
server = new ServerSocket(4444);
} catch (IOException e) {
System.out.println("Could not listen on port 4444");
System.exit(-1);
}
while(true){
ClientWorker w;
try{
w = new ClientWorker(server.accept(), textArea);
Thread t = new Thread(w);
t.start();
} catch (IOException e) {
System.out.println("Accept failed: 4444");
System.exit(-1);
}
}
}
protected void finalize(){
//Objects created in run method are finalized when
//program terminates and thread exits
try{
server.close();
} catch (IOException e) {
System.out.println("Could not close socket");
System.exit(-1);
}
}
public static void main(String[] args){
apeSocketThrdServer frame = new apeSocketThrdServer();
frame.setTitle("Server Program");
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(l);
frame.pack();
frame.setVisible(true);
frame.listenSocket();
}
}
Gruss Sascha