• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Chat Client using Sockets

 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I'm developing a basic chat application on Java using Sockets. In my simple program, a user will be able to communicate with a certain user through a server. Unfortunately, I could not figure out how to communicate between threads as users do not possess the thread data. Before giving the details, I would first like to present my program logic. For the sake of simplicity, I will not include all the code that is present in my work.

Server Side :
Server.java



This piece of code do communicate between threads (at least it is supposed to do). However, I could not think of a way to retrieve messages from clients.

On the client side, I have this piece of code:



Therefore, my question is, how can I read messages queued in the ClientThread class from ServerListener? Is there anyway to accomplish this? If not, can you propose a different idea which will not interfere with the logic of the program too much? I should also note that this piece of code is based upon someone else's idea. I'm trying to modify it so that it reads objects instead of Strings, and instead of writing to a room of people, sending a message to certain ID.

I think I need to invoke a method inside Server class, but because I'm not instantiating any Server classes inside ClientUI class, this is not possible.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can I read messages queued in the ClientThread class from ServerListener

If the server has the ClientThread objects with the messages and the Client wants to read them from the ServerListener class, there will need to be a way for the client to ask the server for the messages.
 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Norman Radder,

Thanks for the reply. I'm a bit lost. I successfully connected sockets, and the program reads and writes flawlessly. However, I noticed that reading and writing at the mean time causes some inconveniences. For example, the server side always waits for an object to be read and until that it freezes the running thread in which I also listen to the pending messages. In particular, the while loop waits for the client to write a message. If client does not write any messages, client will not receive.

I can revise this code so that each client runs 3 threads. But that doesn't seem right. Waste of resources.

Here is the code I currently have : This piece belongs to the ClientThread -an inner class of the Server class.


How should I proceed?
 
Burak Güneş
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed it. Sorry for bothering everyone reading this.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
server side always waits for an object to be read and until that it freezes the running thread in which I also listen to the pending messages.

After the accept method receives a connection, the code should create a new thread to handle the connection and loop back to the accept().
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!