• Post Reply Bookmark Topic Watch Topic
  • New Topic

Simple Server/Client Chat  RSS feed

 
Jeremiah Smith
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I've been trying to learn Java on my own, and have a very basic understanding of most things (and no advanced understanding of anything!). I've been trying to create a server/client chat program, but I keep running into areas where I'm not really sure how to work it. I've run into a wall where my client just sits and spins instead of working - could someone please look at my code and critique the hell out of it? I'm willing to learn, I just need someone to learn me!

http://pastebin.com/ix4EnsiH

I know some areas look really clunky - basically I can work out the dynamic parts later, I just want to understand the meat and potatoes of reading from sockets and writing to sockets (and how to do both at the same time).

Thank you!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch !!

If you want people to look at your code, it is generally a good idea to post it (even if it is snippets) onto the ranch itself. The ranch even provide tags to help display code. Asking someone to click on a link, even if it is easy, may be enough to cause people to move onto helping someone that is more better prepared.

It is also a good idea to do some research -- to ask specific questions, instead of asking for vague general help.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremiah Smith wrote:I've been trying to create a server/client chat program, but I keep running into areas where I'm not really sure how to work it. I've run into a wall where my client just sits and spins instead of working - could someone please look at my code and critique the hell out of it?


Your server just connects the first client, and then loops waiting for data from the client, in order to print. The server also doesn't handle more than the first client. Your client just connects to the server, and then loops waiting for data from the server, in order to add it to some string (for no purpose).

This is only a critique of the main loops -- as I ignored all the GUI stuff, and just assumed that those parts works.

Henry
 
Jeremiah Smith
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply! I'll make sure to drop code snippets directly in my posts from now on instead of links. In regards to specific questions - I was mainly probing towards why my code just sits and spins, and how to enable it to handle sending and receiving data on both the server and client side.

Henry Wong wrote:
Jeremiah Smith wrote:I've been trying to create a server/client chat program, but I keep running into areas where I'm not really sure how to work it. I've run into a wall where my client just sits and spins instead of working - could someone please look at my code and critique the hell out of it?


Your server just connects the first client, and then loops waiting for data from the client, in order to print. The server also doesn't handle more than the first client. Your client just connects to the server, and then loops waiting for data from the server, in order to add it to some string (for no purpose).

This is only a critique of the main loops -- as I ignored all the GUI stuff, and just assumed that those parts works.

Henry


I'll get into a server that can support multiple clients as soon as I figure out handling just one LOL. Ok so it sounds like server side I need something that listens for output from the client, and vice versa for the client, which doesn't make either just sit and wait. This leads to my next question: how can you run multiple processes that handle both receiving input and sending output without sitting and spinning? Threads? I've read about them but I'm really not sure how to apply them effectively, plus I haven't dug into IO streams too much yet.

Again, thanks for checking the code out - I'll drop the stuff (minus the GUI) into my original post ASAP.

Edit: Ok, I can't seem to figure out how to edit my original post... here is the code:

SERVER:



CLIENT:



CLIENT WINDOW: (applicable parts, minus GUI)

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremiah Smith wrote:
Again, thanks for checking the code out - I'll drop the stuff (minus the GUI) into my original post ASAP.

Edit: Ok, I can't seem to figure out how to edit my original post...


By default, once a post has been responded to, it can't be edited. If you want to relax this restriction, you will need to earn some cows. Anyway, thanks for bringing the code into the topic -- for that, I will grant you a cow ...

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremiah Smith wrote:This leads to my next question: how can you run multiple processes that handle both receiving input and sending output without sitting and spinning? Threads? I've read about them but I'm really not sure how to apply them effectively, plus I haven't dug into IO streams too much yet.


In theory, you can make the messages run in both directions independently, and hence, use threads without any concern for state. In practice though, I would probably recommend that you work out a protocol, which defines on who sends and receives what and when, and then code it accordingly.

Henry
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly what you are trying to achieve is greatly explained in Java tutorial from Oracle - http://docs.oracle.com/javase/tutorial/networking/sockets/index.html
It contains runnable examples and also covers multiple clients support. The GUI stuff is not covered there, but that's another question.
 
Jeremiah Smith
Greenhorn
Posts: 3
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:Exactly what you are trying to achieve is greatly explained in Java tutorial from Oracle - http://docs.oracle.com/javase/tutorial/networking/sockets/index.html
It contains runnable examples and also covers multiple clients support. The GUI stuff is not covered there, but that's another question.


I read up on the trail for sockets and it seems they don't really address having a program deal with both input from the socket and output from the user at the same time. I also looked up a little more on threads, and it seems that's what I'm going to have to use to separate waiting for input from the server and dealing with user input at the same time. I'll post relevant code when I fully figure out how to manage threads
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!