• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Combination Sockets and Threads

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

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

Floris
ps: excuse me for my bad English, as I said, my native language is Dutch, and my English is not good
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Floris Kint
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, here are the files who are needed, the socketSet and socketset.* are most important:
http://floriskint.000space.com/artikels/fnetworkconnector.zip
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Henry

 
Floris Kint
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Floris Kint
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey.

BTW:

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



I think that these should be.

Adam
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Floris Kint
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help.
Do you mean this:
SocketInputThreadRead

SocketInput


Floris
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:


Example:

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
 
Floris Kint
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

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:
socketinput


socketinputthreadread


Floris
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic