Java Chatrooms

Java Chatrooms



⚑ πŸ‘‰πŸ»πŸ‘‰πŸ»πŸ‘‰πŸ» INFORMATION AVAILABLE CLICK HERE πŸ‘ˆπŸ»πŸ‘ˆπŸ»πŸ‘ˆπŸ»

































Java Chatrooms


Sign up with email
Sign up




Sign up with Google



Sign up with GitHub



Sign up with Facebook




Asked
7 years, 1 month ago


Active
2 years, 4 months ago


46.4k 25 25 gold badges 187 187 silver badges 173 173 bronze badges


285 3 3 gold badges 8 8 silver badges 19 19 bronze badges



please post the errors you got. Also, from what i see, you call runOutput() once, and there is no loop, meaning s = ss.accept() is only gonna be called once

–  Dioxin
Dec 27 '13 at 21:23






Look at my edited answer. Hope that helps you a bit

–  Dioxin
Dec 28 '13 at 3:34


12.7k 5 5 gold badges 33 33 silver badges 69 69 bronze badges



that really hepled in accepting many clients,but still things clients write don't reach the server :\

–  user3100088
Dec 27 '13 at 21:34



thanks alot bro , but still that didn't work out(no errors though)

–  user3100088
Dec 27 '13 at 21:47



@VinceEmigh When using your server class, I get the error 'running cannot be resolved to a variable'. Do you know why this happens?

–  Sam
Oct 8 '18 at 4:21



@Jacob You need to declare a running variable. I have edited my answer , although this is an old answer which expresses horrid design, so I wouldn't recommend emulating it.

–  Dioxin
Oct 8 '18 at 4:28





729 7 7 silver badges 22 22 bronze badges


Stack Overflow

Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness


Products

Teams
Talent
Advertising
Enterprise



Company

About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us



Stack Exchange Network

Technology
Life / Arts
Culture / Recreation
Science
Other


Join Stack Overflow to learn, share knowledge, and build your career.
I was making a chat room project where the server accepts many clients, and whatever the client writes reaches the other clients and so on.
Unfortunately the server accepts maximum 2 clients and after one client writes an input it gives errors.
Your 'n' thread has no loop, meaning its gonna run once (accept a connection), then die off.
Dont worry, your thread will be paused at 'ss.accept()', since it will wait until a socket is accepted to continue.
Sorry, wrote that really quick, dont have much time. Ill come back and relate it to your code in a bit if it doesnt help.
You need some way of continuously retrieving input from the Server's input stream, in which you can then handle the input.
That loop loops each time you retrieve something from inputstream.readUTF() (and isnt null, of course). Hopefully that example will help you
After actually reading your code and testing it, i have noticed that your structure seems a bit off. First, your Thread n in your server is responsible for accepting connections ( addClient() ), but you also call it first thing in your Thread input ?
Thread n is handling accepting connections, so with the loop, Thread n is fine.
Thread input is handling input retrieved from clients. This is where you are getting confused.
Each client should have their own InputStream and OutputStream server-sided. Similar to how you have a list for Sockets (since you are gonna be creating multiple Sockets server-sided), you need multiple Streams aswell. For this, I suggest making a User class, then if anything, have your ArrayList for User, not Socket.
in.readLine() is depreciated, which means it's out-dated and shouldn't be used (to avoid bugs).
Now that you have your User class set up, which handles the data, you can now create a new User each time a connection is received.
Hopefully, that'll give you a better idea for how your server should run.
As for your client.. Well, there are a ton of things you should realize:
First, big problem, you're re-initializing your DataOutputStream each loop, which is gonna cause the stream server-side to close. Gotta take it out of the loop, possibly put it before (or look at my server example for a better idea of handling stream initialization).
Second, the names should be kept on the Server, rather than the Client. This way, its easy to iterate through the names, check if its taken, ect..
Third, nothing with networking, but seeing how you never refer to Thread n or Thread input , other than to start it, you should remove the reference variable. (even though im pretty sure local vars are picked up by the Garbage Collector, still looks nicer). Example:
Hope this helps you understand how a multi-threaded server works! :)
PS: This is a very basic server, and really shouldn't be used for large scale applications.
Creating new threads (new User()) can cause a lot of overhead, so its best to handle connections using an ExecutorService, or some other type of threadpool service. Good luck!
I think you should rethink the logic of a lot of the server code. You should have one thread that handles connecting clients, and assigns them their own input threads. You can then have a method on that input reading thread that will iterate through the clients sending them the message.
When I first started writing my Client-Server chat program I found this little Oracle doc very useful. Hopefully it will help you too.
You should note this part of the document in particular:
Client connection requests are queued at the port, so the server must accept the connections sequentially. However, the server can service them simultaneously through the use of threadsβ€”one thread per each client connection.
The basic flow of logic in such a server is this:

while (true) {
accept a connection;
create a thread to deal with the client;
}


By clicking β€œPost Your Answer”, you agree to our terms of service , privacy policy and cookie policy

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo Β© 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . revΒ 2021.2.12.38571


Π§Π°Ρ‚ Π½Π° Java (БСрвСрная Ρ‡Π°ΡΡ‚ΡŒ)
Server client chat room in Java - Stack Overflow
Multi Chat Rooms using Java - YouTube
How to Create a Chat Console Application in Java using Socket
Creating a Chat Server Using Java : 8 Steps (with Pictures) - Instructables

Vintage Sexy Pics
Son Forces Stepmom
Chubby Live Cam
Karups Hot
Emma Frost Nude

Report Page