Hi

I'm developing a game that includes a chat room for four people. All the programs must be the same, I don't want one server program and three clients.

Here's what it should do

  • Someone creates a chat and invites three more there
  • The chat starter will become the server, everyone else join it
  • Everyone can send messages together

So, SocketServer and Socket classes? Can you guys help me building this? I need help to understand how to do this. I know how to do one server application and one client, but how I can mix them?

Recommended Answers

All 17 Replies

I suggest that you start with a simple server and client. Have the server create a new Thread to process incoming connections.
Then adding the logic for more than one client should be the next step.
But do it one step at a time.

Someone creates a chat and invites three more there

How does the starter communicate with the other three initially? That would require that all the others be listening with server sockets waiting for the invite.

The chat starter will become the server, everyone else join it

To be a server, requires using the ServerSocket class.

I have the rest of the project almost ready, so I don't make another programs for chatting.

An yes, there is a ServerSocket listening on every application. Or then I just make something else.

Did you finish the project? I can't see a question in your last post.

Did you finish the project? I can't see a question in your last post.

No I didn't. I didn't post a new question, because I didn't get the answer for the first.

I'm still asking the same question. What's the best way to make a chat for four people and how do I make it?

What's the best way to make a chat for four people and how do I make it?

That is too general a question.
Can you narrow it down to some specific questions?

My first post was an answer to your first question.

That is too general a question.
Can you narrow it down to some specific questions?

My first post was an answer to your first question.

No, I don't think that is too general. There's a million ways to do a chat, I wan't to know what's good and easy way. If I put thousands of if-else-clauses there, the code will became a monster. I want to avoid that, so I ask if there's some kind of trick to do this.

Maybe your first post a reply to my question, but I don't accept it as an correct answer.

I don't accept it as an correct answer.

Fine.
Bottom line before this goes around in too many circles:
What specific questions do you have?

if there's some kind of trick to do this.

Yes. The trick is to have a good design BEFORE writing any code.

Do you have a design that you'd like comments on? Post it.
Otherwise: the code will became a monster

What specific questions do you have?

None. The first thing is to have the idea, then we go step-by-step to the more specific questions. Why should I make something which is needed last (Yeah, I can send messages but nobody receives them!!)?

I have designed this in my head, nothing on the paper. It's waste of time and it doesn't produce results before I know what is possible and how I can do it. Why should I plan how to make a car if I don't know what to put in there?

I have designed this in my head, nothing on the paper.

Time to put some thoughts on paper.

Start with the features you want. Make a list.
Then drill done for each to specify what it will take to get those features.

Time to put some thoughts on paper.

But what? I already explained in the first post what I want it to do.

But that is at too high a level for an answer.

I guess I'll leave you with your thoughts.

Perhaps someone else will give you what you want, I'm having trouble getting any specifics on where I can help.

Good luck.

I planned something and I did a little bit research on the internet... I found this Oracle's document. It is what I want (Assuming it works on multiple clients. I haven't tried it yet, but I think it will work).

Can I ask something more? Okay, so let's say I have class named X (which not true) and there's method Y. Why I can't call X.Y() without getting an error "non-static method Y cannot be referenced from a static context"?

I can't say X something = new X() before that, because X is my main program.

Can you answer this question I asked on my first post?
How does the starter client communicate with the other three initially? That would require that all the others be listening with server sockets waiting for the invite.

If the method y is in class X, then you can call non static method y from any method (except main()) in class X without using a reference object: y();
If you are in the static main() method of class X then you need to a reference to class X to call any non static method in class X.
X x = new X();
x.y();

can't say X something = new X() before that, because X is my main program.

Explain what you mean by "main program". Is main a method or class?

Can you answer this question I asked on my first post?
How does the starter client communicate with the other three initially? That would require that all the others be listening with server sockets waiting for the invite.

If the method y is in class X, then you can call non static method y from any method (except main()) in class X without using a reference object: y();
If you are in the static main() method of class X then you need to a reference to class X to call any non static method in class X.
X x = new X();
x.y();


Explain what you mean by "main program". Is main a method or class?

I changed the program, now it's required to start the server thread on any program and then others can join it. No inviting from server.

I'm developing this with Netbeans, and "main program" is that ...View.java file in the project. There's the design of the program and the main functionality.

start the server thread on any program and then others can join it

Are the IP addresses of all the possible servers known to all the clients?

I'm developing this with Netbeans, and "main program" is that

The IDE you use does not change the way that java works.
Java does not have programs. It has classes and methods and variables. You put these together to make a "program".

Are the IP addresses of all the possible servers known to all the clients?


The IDE you use does not change the way that java works.
Java does not have programs. It has classes and methods and variables. You put these together to make a "program".

Yes, all the clients know the servers addresses.

No, the IDE doesn't change the way that Java works. I meant main class, not program.

I meant main class
vs
I meant the class named Main (names for classes begin with capital letters.

Yes, all the clients know the servers addresses.

Will they constantly poll each other so that there isn't confusions if one is not online for a time and then comes back and two others have meanwhile connected. I mean is it possible for there to be two stations that think they are THE server.
How is it determined which station is to be The Server?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.