• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When someone sends a packet

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic