• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with Sockets, Threads, and overall design from book exercise  RSS feed

 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! I'm brand new to the forums and extremely excited to be here. About 10 years ago I use to program with VB6 so I have some programming experience. Mostly developed bad habits though

I've made it towards the end of my book, Head First: Java. I can't seem to completely grasp sockets and multi-threading. We developed a simple client/server chat program, then added threading to allow multiple users to connect. I didn't understand how it worked 100% so I tried to add my own functionality to get a better understanding. Well, it turned out to be really messy.

I had the idea to allow users to have a username. Although the program allowed multiple users, it never incorporated any type of username functionality. So I tried to add my own. I tried to break procedural thinking and tried to think more OO. I wanted to make a class that was the blueprint for this. I thought, why not add a try catch that forces the program to wait for the first message the client sends after connecting, "USERNAME:Ovation", and split the string and add it to the ChatUsers object. It worked great, until I realized that the "tell everyone" message doesn't specify who the message was from. It tells each client that THEY sent the most recent message, regardless of who did! Now I'm really stuck and got myself in quite the mess. I'd really like to solve this. I want it to be like a normal chat where users can join and see all of the messages and eventually a list of all of the current users!

Also, if I'm doing anything incorrectly or there's a better way to do something please let me know. I really don't want to keep bad programming habits. Thank you so much guys! Sorry for such a long book, I'm just really excited to be here and learn from you all

 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't even try to understand sockets and threading. Try one or the other because there is too much to learn all at once.

Try the Java® Tutorials about networking which has a section about sockets in.
Or about concurrency, but don't try both simultaneously.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fair enough! Thank you so much Campbell, I'll take a look now!
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

Maybe you should try networking first; it is probably easier than concurrency.
 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You're welcome

Maybe you should try networking first; it is probably easier than concurrency.


That sounds like a great idea! I'll start with networking then! Also, is this normal:

I'll read through a topic and understand it completely while I read it. Once I move onto another topic, I'll start flushing the other one. For example, the book shifted from OO concepts to collections. I had to re-read OO because I started getting confused.

Also, is there any advice or anything to read on how to design a program using OO? I always have the urge to want to write everything out procedurally. I'd like to start making more classes but I feel like I'll over do it, or not enough. Any suggestions?

Thank you again!
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Bushnell wrote:Also, is there any advice or anything to read on how to design a program using OO? I always have the urge to want to write everything out procedurally. I'd like to start making more classes but I feel like I'll over do it, or not enough. Any suggestions?

I think a good OO place to start is by looking through your requirements and identifying nouns (objects) and verbs (methods). Then look at the relationships between nouns for "is-a" (inheritance) and "has-a" (aggregation).
 
Brandon Bushnell
Greenhorn
Posts: 23
Eclipse IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Brandon Bushnell wrote:Also, is there any advice or anything to read on how to design a program using OO? I always have the urge to want to write everything out procedurally. I'd like to start making more classes but I feel like I'll over do it, or not enough. Any suggestions?

I think a good OO place to start is by looking through your requirements and identifying nouns (objects) and verbs (methods). Then look at the relationships between nouns for "is-a" (inheritance) and "has-a" (aggregation).


Thank you! I definitely remember the IS-A concept, and they talked about HAS-A but I didn't quite get it. I just assumed HAS-A was going to be static/instance variables. I never realized that you could consider objects as nouns and methods as verbs. Boy that made it much easier, haha!

Thank you very much!
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brandon Bushnell wrote: . . . Also, is this normal: . . .
Afraid so. Yes. You cannot expect to learn everything in 21 days.

That link is not working at present.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!