Forums Register Login
Combination Sockets and Threads

A few weeks ago, I began writing a Java program who connects computers in a network with Sockets. Probably this topic has to be in the category sockets, but I think my problem are the threads I use in the program.
The problem: when I connect from the one application to the other, the connected client doesn't get the data. You can see the whole code in the zipfile. There are some 'System.out.println'-statements that are in Dutch, because that's my native language, but they aren't important for the program, thus I hope you won't get any problem with it.
I would appreciate it if someone could find the problem in my program. If you check the console, you can see that the newconnectionrequest is sent after that the other client says that he's ready, but the other doesn't get the request.

The zipfile contains the eclipse project structure, if you don't use eclipse, you can get the code by simply copying the 'src' folder to the map you want to use.
ZIP-file: floriskint.000space.com/artikels/FNetworkConnector.zip. The zip is on my own site because I wasn't allowed to upload zips to the coderanch servers.
only most important files: http://floriskint.000space.com/artikels/fnetworkconnector.zip

Thanks in advance

ps: excuse me for my bad English, as I said, my native language is Dutch, and my English is not good
I thought it would be a nice gesture to help a fellow Dutchman but I'm not going to dig through 72 files.
So please IsolateTheProblem.
I'm sorry, here are the files who are needed, the socketSet and socketset.* are most important:
You link doesn't work and it sounds like I'll still have to go through a lot of files.
Try to reduce it to lets say about 100 lines of code and post it here (within code tags) so that other people can help you to.

In my opinion, anything more than one screen page of code, drastically lowers your chance of getting an answer. Also, the less detail the problem description, and the less detail of the original poster of doing actual work to solve the problem, the less chance of getting an answer too.

I really hope the original poster is not just sitting around waiting for an answer -- because one is unlikely to be coming, and correctly so.


Excuse me if it seems that I havan't done anything to find the wrong code, and the reason why I post so much code is, that I'm not sure what's the wrong part. But I'll try to do as you said:
This is the code of the thread that reads the data from the socket:

This is the code for the class who controls the input of a socket:

The SocketSet class contains a socket and a few other methods (i.e.: dataReceived(Object)...)
I hope that this is better.
And again: my excuses, I haven't done this to harm you...

ps: I've noticed that if I post the link to the code between url-tags, the link doesn't work, but if you copy the link, it must work.
Thank you to take the time to check this topic, but I found the real misstake. It wasn't the threads and neither the sockets, but it was an imageicon that I wanted to send. Now, I've converted it to an Byte-array, and my program works. Excuse me to take parts of your time, but I'm really pleased to see how soon you've answered.


Shouldn't be marked these variables below as a volatile?

I think that these should be.

Adam is right. As an alternative you could synchronize the access to those variables.

Also your performing actions within the constructor of SocketInput. It is not recommended to do so
because then you're working with partially constructed objects. I gets dangerous if you extend the class.
Use constructors to initialize objects.
Thanks for your help.
Do you mean this:


You're getting there. However now you've made them volatile and synchronized the access to them. That is overkill. I would recommend you to read something about threading and synchronization because otherwise you'll eventually run into situations that are very hard to debug/explain. You could start here.

About the partially constructed objects:


Output: Asdf NullPointerException
If you want to find out what went wrong read something about object initialization.

Other remarks:
stream and objectStream use the same stream do you need both?
I would perform null checks and other checks in the constructor because then you've got a solid objects once created.
The else clause can be removed in createObjectInputStream
A finally block is appropriate in run

However, this does not eliminate all need to synchronize atomic actions, because memory consistency errors are still possible.

source: http://java.sun.com/docs/books/tutorial/essential/concurrency/atomic.html
How can I know when it is needed to do both, and why isn't it necessary in the code of my program?

the new code:



This thread has been viewed 1251 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 20, 2018 02:08:12.