• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to modify SimpleChatClient and VerySimpleChatServer on Head First Java p518 ~ p520  RSS feed

 
james choi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To run SimpleChatClient, I have to run VerySimpleChatServer first. As the book says,

To run the chat client, you need two
terminals. First, launch this server
from one terminal, then launch the
client from another terminal.

I see there are main methods for both classes.
I want to modify these classes so only one has the main method. I want to be able to run one of them and it does the work for both classes.

This is what I have done so far.
I have modified main method of VerySimpleChatServer to have an object of SimpleChatClient and directed to its go method. Then, removed main method in SimpleChatClient.


I can see this partially works. The problem is, I have to run it twice to make it work (only for the very first time). The first run does nothing, then second run pops the chat GUI up.
In order to accomplish this, is there any other methods that I have to modify?
Is my approach correct?

I am modifying these two so that I can create an executable .jar where I can double-click and have the chat client working.
Any advice would be helpful, thanks!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james choi wrote:Is my approach correct?

What exactly are you hoping to achieve ? The point of a chat server is to allow clients to chat with each other. You have one server and multiple clients. If every client is using a different server none of them will be able to communicate with the others.
 
james choi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
james choi wrote:Is my approach correct?

What exactly are you hoping to achieve ? The point of a chat server is to allow clients to chat with each other. You have one server and multiple clients. If every client is using a different server none of them will be able to communicate with the others.


I want to be able to run once, not server first then client. The executable Jar file format only allows you to specify one main class but both VerySimpleChatServer and SimpleChatClient both have main method. I want to create an executable Jar with both classes together. I hope you understand what I am trying to do...
The code I posted is the main class of the VerySimpleChatServer that I modified. I got rid of the main class of SimpleChatClient.
 
Paul Clapham
Sheriff
Posts: 22813
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's pretty clear what you want to do, yes. The question is, why do you want to do that? There is very little point (i.e. no point at all) in running a chat server and an instance of the client in the same JVM.
 
james choi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:It's pretty clear what you want to do, yes. The question is, why do you want to do that? There is very little point (i.e. no point at all) in running a chat server and an instance of the client in the same JVM.


Could you please provide some detail on why it has very little point? I don't think I understand your point.. I am still a beginner here.
Then, how would you make executable .jar of the chat client+server?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't. Both are executed independently of one another, so it makes little sense to keep them in the same jar file.
 
fady zohdy
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
james choi wrote:
To run the chat client, you need two
terminals. First, launch this server
from one terminal, then launch the
client from another terminal.

may i ask how you did that on windows?
i built the client and server classes but i don't know how to run them from different terminals?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!