johnnyBravo

Members
  • Content count

    3313
  • Joined

  • Last visited

Community Reputation

100 Neutral

About johnnyBravo

  • Rank
    Contributor
  1. I tried flush(), but it did seemingly nothing. edit: I got it working with this: PrintWriter out = new PrintWriter(socket.getOutputStream(), true); out.println("hello"); I'm not sure what the difference between the two output clases, but it works how i want. [Edited by - johnnyBravo on September 15, 2007 7:41:34 AM]
  2. When I'm sending messages it will not actually send the text unless I close the connection, but I need to send multiple messages back and fourth during a single connection. heres my source: Socket socket = new Socket("localhost", 3333); DataOutputStream o = new DataOutputStream(socket.getOutputStream()); o.writeBytes("hello"); //closing it is the only way to get it to send, using either o.close(); //or socket.close() The receiver source is ServerSocket listenSocket = new ServerSocket(3333); BufferedReader inFromConn = new BufferedReader(new InputStreamReader(socket.getInputStream())); while(!inFromConn.ready()); System.out.println(inFromConn.readLine()); //isn't called until the connection is closed thanks
  3. Java Thread problem, occasionaly hangs

    Quote:Original post by Antheus As long as you don't access "document" from anywhere else, this guarantees you thread-safety. Do you mean anywhere else as in another thread? I think the reason it was hanging was because I was using 'volatile' in conjunction with 'synchronized', which for some reason is ocasionally bad. Also is it ok to use synchronized on the file saving method, to make sure it only run once at a time?
  4. Java Thread problem, occasionaly hangs

    Quote:Original post by Antheus *** Source Snippet Removed *** This could be whatever. Synchronized isn't something you just toss around. What are the contested resources here? What do you need to make sure is thread-safe? the 'document' and the 'logPane' is what I'm trying to protect from being written to at the same time. And the 'saveDocument()' I want only to be called once at a time, so it doesn't try to save to the file while already doing that. Quote:Original post by Antheus If your threads access same document object, then it's better to synchronize on that explicitly: *** Source Snippet Removed *** Of course, the document instance must not change. So I just wrap my code inside each of the functions with that? Quote:Original post by Antheus As for why the app doesn't close... Could be anything. I realised its not that it won't close, but that it has hanged, the window controls still respond eg resize, close, maximise buttons etc, though the inner part of the window does not respond.
  5. Sometimes in my multithreaded program after starting 2 threads it hangs, although the close button is clickable, but the area inside the window is frozen, and also in Eclipse the little red stop running button is no longer clickable. I'm sure its because the way I'm using the threads. Heres how it goes: -load xml from file into document -start thread to listen for connections --on connection start another thread and receive input there ---add data to xml document ---save document to xml file I put 'volatile' on the variables that are used by multipe threads. eg private volatile Document document; private volatile LogPane logPane = new LogPane(); //just a JPanel with a textarea, and scrollpane And 'synchronized' on the functions that are also used by multiple threads. eg private synchronized void saveDocument(); private synchronized void addSellerToDocument(Node node); private synchronized void onConnection(final Socket socket); Am i handling the accessing of variables and functions from different threads wrongly? thanks edit: Also the series of events is: the first program waits for a connection on a separate thread another program connects, sends data and disconnects the first program creates a another thread to handle the connection, read the sent data and save it to file.
  6. Is it possible to load some xml from a string rather than from a file? eg "<test>aaab</test>" Heres what I've got so far: DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document d = db.parse(""); thx
  7. When I load an xml file using DOM, it causes exceptions if I put any spaces in the xml file. eg this doesn't work: <sellers> <test>aa</test> </sellers> where as this does: <sellers><test>aa</test></sellers> I've been looking through the javadocs, but I cannot find anything. Heres what I'm using: DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("AgentMSellers.xml"); Node n = doc.getDocumentElement(); System.out.println(n.getFirstChild().getFirstChild().getNodeValue()); I've tried these which seem to do nothing: factory.setIgnoringElementContentWhitespace(true); n.normalize(); doc.normalize(); doc.normalizeDocument(); Thanks.
  8. I need to read and write xml files, I'm after a tree-like hierchical approach. I've been looking at the java site which list lots of apis to choose from, I'm not sure which one to use. eg JAXP, JAXB, JDOM, DOM4J .... thx
  9. ODE, Bullet, or PhysX? Which one

    I had trouble with physx when I tried stacking boxes during initialisation they would kind of explode and fly off. But apart from that it was easy to use. I'm trying bullet btw, and theres a new version out. (I was unable to compile the last ver)
  10. How to tell if a vector is pointing towards a plane?

    Ah thanks, I had a feeling I was over doing it.
  11. How would I tell if a vector is pointing towards a plane? Hes what i've got, is this correct? A = directionNormalVector B = planeNormalVector*-1.0 acos(A.dot(B)) < pi/2 //is pointing toward plane? thx
  12. How to do this in std::list?

    Quote:Original post by ToohrVyk Actually, by decrementing k twice, you get the third last object. And it doesn't really get any shorter than a simple --k before the loop. Ezbez: --actors.end() might well be illegal. Quote: e.insert( --e.end(), TodaysDate() ); Error: The expression "--e.end()" is illegal. The reason is simple, if a little obscure: vector<Date>::iterator is simply a Date*, and you're not allowed to modify temporaries of builtin type. For example, the following plain-jane code is also illegal: Date* f(); // function that returns a Date* p = --f(); // error, but could be "f() - 1" I'm not sure I understand what the problem is, like what does that function have to do with .end() ?
  13. How to do this in std::list?

    thanks, for the i iterator, to make it just goto the second last object, I did: ActorsIterator k = actors.end(); k--; k--; for(ActorsIterator i=actors.begin();i!=k;i++) { But is there a better way to do it? edit: Quote:Original post by Ezbez I think that you missed the "// Need to iterate to second to last actor" part, SiCrane. Correct me if I'm wrong, but that could be done by doing "i != --actors.end()" Is it just --actors.end()? Because I thought actors.end() doesnt actually represent the last item, but after it, so I thought it may be ----actors.end() thx
  14. Previously I was using std::vector, and I was using it like this: for(int i=0;i<(int)actors.size()-1;i++) { for(int j=i+1;j<(int)actors.size();j++) { } } But now I'm using std::list instead, and I'm not sure how to convert that code above to work with it as a list. So far I've got: typedef std::list<Actor>::iterator ActorsIterator; for(ActorsIterator i=actors.begin();i!=actors.end();i++) { //need to iterate to second last actor for(ActorsIterator j=actors.begin();j!=actors.end();j++) { // need to start at i's current iteration+1 } } thanks