• Post Reply Bookmark Topic Watch Topic
  • New Topic

When someone sends a packet

 
hurricane computers
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
I am writting a java applet that allows me to chat with another user. The problem is, I have no idea when a packet from another user will come in. I don't want to use up cpu time in a while loop looking for incomeing packets. Is there a way I can use the addActionListener to a socket so whenever something happens on the socket, it will tell me. More importantly, I just want to know when a packet comes in, I don't care about outgoing packets.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, welcome to the ranch! The sheriffs will be around to point out that your user name should be a believable first and last name. Change it quick before they spot you!

Could you post a bit of your listening code? If you just try to read from a socket I'd expect the read to block (wait) until something comes in.

I'd be tempted to give the applet a ServerSocket. When somebody wants to send a message they connect, send and disconnect. The ServerSocket quietly waits for someone to connect without eating up any CPU.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The input stream will block until a packet comes anyway. A while loop is the proper way. You will probably need a seperate thread for that inputting of data, and I personally like to add another one for output as well. Keeps the GUI responsive.

Also check Naming
[ August 19, 2005: Message edited by: Mr. C Lamont Gilbert ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!