Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Object nesting is changing how an object behaves somehow?  RSS feed

 
Alex Lieb
Ranch Hand
Posts: 61
3
Java Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I'm trying to do a basic program with sockets where a client and a server send each other messages and print them out. I got the client to send messages to the server, and I got the server to receive and print them, but now that I'm trying to abstract the message-sending process a little bit I'm running into some very strange problems I've never encountered before, and some of them I have a vague idea what's wrong, but this one I am just totally lost. I'm actually not totally sure if it's a problem with my socket or my threads. I can't imagine how there would be a thread issue with this, but it's *acting* like it's having a thread issue. Also I know in principle what "synchronized" does, but I've never really had to use it in practice and I'm not sure if I'm using it in all of the right places, which might be relevant, but more on that later!

So the way I'm doing this; I'm telling the client to open two sockets connected to the server; one will be exclusively for sending messages to the server. That one is part of the main thread. The other one will be exclusively for receiving messages from the server, and that one is wrapped in a new thread. The server has the sockets organized the same way, except obviously it's just listening for connections, and when it finds a new one it just treats it as an input socket until it hears otherwise.

The output socket is stored in an object I called OutputSocket; which looks like this:



I want there to be a single controller object that creates and manages both the OutputSocket and the InputStreamThread (which contains the read-only socket), so I made a ClientController that instantiates the two socket objects and calls "run" on the one that extends Thread. This is my ClientController object:



So here's the weird part... When my main method does this, the client sends each message and the server receives and prints that message immediately:



But when the main method does this, the client says it's sending each message immediately, but the server doesn't actually receive anything until the socket is closed. When the socket is closed, it prints everything the client has been trying to send it all at once:



As far as I'm aware, there should be no difference between these, but there is...
 
Alex Lieb
Ranch Hand
Posts: 61
3
Java Netbeans IDE Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I.... I found it... It is a thread problem. A really stupid thread problem...

This was the constructor for the ClientController object:



ClientInputThread extends Thread, has 1 Constructor, and overrides 1 method, "run".

Apparently "run" is the method I need to override, but I thought "run" actually ran it as a new thread... It does not. Apparently you want to call start() for that. If you call "run" from outside the thread, it runs it *within the current thread.* The thing that tipped me off was I realized if I took the ClientInputThread object out of the constructor, it would work perfectly... Which indicated that somehow, calling "run" on the input thread was putting other things on hold.

I'm still confused about one thing though; if that were the case, even on the client side it shouldn't have been *telling* me it was sending them immediately because the whole thread should've been occupied by running that InputListener... I'm still not sure why it was even making it to the "send" method at all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!