Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Sending message from server to multiple users  RSS feed

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain me how to step by step make server chat send message from any user to all users?
I tried to add them to ArrayList<Socket> clients but when i tried to send it by

It throws errors.
I think I need some handler but with what inside.
 
Saloon Keeper
Posts: 5288
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It throws errors


Full stack trace, please. A more complete code excerpt would be useful as well - those 3 lines don't really show us what you're trying to do.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:

It throws errors


Full stack trace, please. A more complete code excerpt would be useful as well - those 3 lines don't really show us what you're trying to do.


No no, i just ask what should i use to make something like this to work i mean sending to multiple clients from server.
 
Tim Moores
Saloon Keeper
Posts: 5288
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I thought you wanted help with an error. For that we would have needed details of what's happening.

If you're looking for general recommendations, I would look into technologies like websockets and server-sent events for communication between a web server and browser. Assuming that the client is a browser, of course - you haven't said.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:Well, I thought you wanted help with an error. For that we would have needed details of what's happening.

If you're looking for general recommendations, I would look into technologies like websockets and server-sent events for communication between a web server and browser. Assuming that the client is a browser, of course - you haven't said.


It's chat server.
 
Tim Moores
Saloon Keeper
Posts: 5288
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was asking about the client, not the server - which protocol you can use between them depends on what kind of client we're talking about.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:I was asking about the client, not the server - which protocol you can use between them depends on what kind of client we're talking about.


Client gui made with Swing connects using Socket, send message to the server and recieve this message and from other clients.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arek Wróbel wrote:

Tim Moores wrote:I was asking about the client, not the server - which protocol you can use between them depends on what kind of client we're talking about.


Client gui made with Swing connects using Socket, send message to the server and recieve this message and from other clients.


It's using DataInputStream and DataOutputStream.
 
Tim Moores
Saloon Keeper
Posts: 5288
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, it's a Java client as well. Then we're back to this:

Tim Moores wrote:

It throws errors


Full stack trace, please. A more complete code excerpt would be useful as well - those 3 lines don't really show us what you're trying to do.



The code excerpt you posted could legitimately be part of the server side, so from that alone we can't tell anything.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:OK, it's a Java client as well. Then we're back to this:

Tim Moores wrote:

It throws errors


Full stack trace, please. A more complete code excerpt would be useful as well - those 3 lines don't really show us what you're trying to do.



The code excerpt you posted could legitimately be part of the server side, so from that alone we can't tell anything.



Main class code:



ServerFrame code:



ServerHandler code:




So let's start from beginning:
When you run server it's starting to accept connection.
When someone connect and send message, this message is sending back to the client. It work's for everyone but every client get message only from themselves. . So here is my question how can i make it to send messages from client to all clients already connected.I tried
add clientSockets to ArrayList<Socket> clients but it threw errors.Look down here:

Server start.
Server prints: Server waiting for connection
Client join.
Server prints:
Server waiting for connection
Exception in thread "main" java.lang.NullPointerException
at server.ServerFrame.makeServerFrame(ServerFrame.java:160)
at server.Server.main(Server.java:11)
 
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "main" java.lang.NullPointerException
at server.ServerFrame.makeServerFrame(ServerFrame.java:160)


Look at line 160 and find where the null value is.  Add some print statements to print the values of variables if you can't tell.  
Once you find the null value backtrack in the code to see why it is null and not a valid value.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Exception in thread "main" java.lang.NullPointerException
at server.ServerFrame.makeServerFrame(ServerFrame.java:160)


Once you find the null value backtrack in the code to see why it is null and not a valid value.


How to do that?
And the null value is here:
run:
Server waiting for connection
Exception in thread "main" java.lang.NullPointerException
at server.ServerFrame.makeServerFrame(ServerFrame.java:161)
at server.Server.main(Server.java:11)
Line 160: client value before adding a new clientSocket to ArrayList isnull After this print server doesn't recieve or send messages
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, got it:

run:
Server waiting for connection
Line 160: client value before adding a new clientSocket to ArrayList isnull After this print server doesn't recieve or send messages
Server waiting for connection
java.lang.NullPointerException
at server.ServerFrame.makeServerFrame(ServerFrame.java:162)
at server.Server.main(Server.java:11)
Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at server.ServerHandler.handleInputMessages(ServerHandler.java:46)
at server.ServerHandler.run(ServerHandler.java:38)
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at server.ServerHandler.handleInputMessages(ServerHandler.java:46)


The code at line 46 called the get method on an empty ArrayList.
Test the size of the list before calling its get method.  You can not call the get method on an empty list.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

Exception in thread "Thread-2" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at server.ServerHandler.handleInputMessages(ServerHandler.java:46)


The code at line 46 called the get method on an empty ArrayList.
Test the size of the list before calling its get method.  You can not call the get method on an empty list.


Yes, it's 0.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But why its 0? When someone join it should add him to ArrayList.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In ServerFrame class at 160: is clients.add(clientSocket);  
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing some debugging by adding print statements every where the clients ArrayList variable is touched.  Print the size of the list.  The print out will show you if you added anything before you tried to do a get from the list.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:Try doing some debugging by adding print statements every where the clients ArrayList variable is touched.  Print the size of the list.  The print out will show you if you added anything before you tried to do a get from the list.


Thank you for tips again Mr Norm. If something will bother me i will write here. Nice to have people like You in our community.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know why I like You?
You don't just code instead of me or make it work instead of me. You just let me think, how to fix that, learn that.
I like it. I know that i should try to debug that but i just tought i can't add Socket to ArrayList so i just writed here again. Thank you for everything again. To next time
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see there are too many uses of static in the code.  Try getting rid of most of them.  
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:I see there are too many uses of static in the code.  Try getting rid of most of them.  


But when i tried to do it, i couldn't use them i main method without it :/ How can i do this?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am propably so much back with java ... I think that I forgot about static variables. Time to look into book.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to get rid of static variables I need to make some method , create instace of class at main method and just use method ex.    ServerInstace.work();?
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

create instance of class at main method and just use method


Yes, something like that.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I think i made something that i want.
At 48 and 53 lanes instead of number 2 i need something that will check all elements in ArrayList. Because already it's checking element at place 2 in ArrayList<Socket> clients.
So only a third person that connected to server can send message to server and recieve them from server.
I tried (for int i = 0; i < clients.size; i ++)
{
}
but it didn't work. Any ideas?


 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

but it didn't work.


Please explain.
What happened?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After i start server i connect with one client. If i send message from client, server recieve it and send back but when another one connects and try to send message from any clients, server stops recieve messages.
I think there is somwhere problem with loops.

 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When i connect with second client and write something it doesn't  appear in prints and in clients and server gui. After that it just says Preparing to make for() loop.


run:
Server waiting for connection
[Socket[addr=/127.0.0.1,port=49573,localport=5000]]
Server waiting for connection
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49573,localport=5000]]
Preparing to make for() loop:
Actual value of variable msg_To_Read is message from client 1
End of for() loop
Message delivered from client
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49573,localport=5000]]
Preparing to make for() loop:
Actual value of variable msg_To_Read is again from client 1
End of for() loop
Message delivered from client
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49573,localport=5000]]
Preparing to make for() loop:
[Socket[addr=/127.0.0.1,port=49573,localport=5000], Socket[addr=/127.0.0.1,port=49574,localport=5000]]
Server waiting for connection
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49573,localport=5000], Socket[addr=/127.0.0.1,port=49574,localport=5000]]
Preparing to make for() loop:


Code:

 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

server stops recieve messages.


What is the last statement that the server executes?
Is the server waiting to read something?  Like on line 51 where there is a readUTF method called?
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

server stops recieve messages.


What is the last statement that the server executes?
Is the server waiting to read something?  Like on line 51 where there is a readUTF method called?





Here is output:

run:
Server waiting for connection
[Socket[addr=/127.0.0.1,port=49579,localport=5000]]
Server waiting for connection
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49579,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
Adding message tu server gui
Actual value of variable msg_To_Read is Hi! I am client 1.
Trying to get output
Sending output
End of for() loop
Message delivered from client
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49579,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
Adding message tu server gui
Actual value of variable msg_To_Read is Hey im client 1, lets swap.
Trying to get output
Sending output
End of for() loop
Message delivered from client
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49579,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
[Socket[addr=/127.0.0.1,port=49579,localport=5000], Socket[addr=/127.0.0.1,port=49580,localport=5000]]
Server waiting for connection
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49579,localport=5000], Socket[addr=/127.0.0.1,port=49580,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read

 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is causing stop in loop:

msg_To_Read = dataInputStream.readUTF();
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I change it to this dataOutputStream.writeUTF(dataInputStream.readUTF());
Still stop while trying to make this command.
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the readUTF method will wait/hang/block until it has read something.
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run:
Server waiting for connection
[Socket[addr=/127.0.0.1,port=49651,localport=5000]]
Server waiting for connection
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49651,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
Message to read null
Adding message tu server gui
Actual value of variable msg_To_Read is client 1 message
Trying to get output
Sending output
End of for() loop
Message delivered from client
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49651,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
Message to read client 1 message
Adding message tu server gui
Actual value of variable msg_To_Read is Let's client 2 connect
Trying to get output
Sending output
End of for() loop
Message delivered from client
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49651,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
Message to read Let's client 2 connect
[Socket[addr=/127.0.0.1,port=49651,localport=5000], Socket[addr=/127.0.0.1,port=49652,localport=5000]]
Server waiting for connection
There is list of actuals connected clients [Socket[addr=/127.0.0.1,port=49651,localport=5000], Socket[addr=/127.0.0.1,port=49652,localport=5000]]
Preparing to make for() loop:
Trying to read input
Saving input to msg_To_Read
Message to read Let's client 2 connect




 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It read only from client 1, when client 2 joins msg  value is still from client 1 : /
 
Arek Wróbel
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea what to do.
 
Norm Radder
Rancher
Posts: 3353
33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There needs to be a separation of tasks.  The code to send messages to all clients must be separate from the code to read input from each client.
You will need to use Threads to allow all that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!