Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

Socket Class

Recommended Posts

How do sockets work in java? I have only previously been exposed to the VB winsock control, which receives data automatically as an event. From a tutorial i've seen, it looks like all incoming data has to be manually read using a bufferedreader, perhaps using a continuous loop, is that how it goes?kvarnerexpress

Share this post


Link to post
Share on other sites

hi kvarnerexpress,In Java one application runs the ServerSocket on a particular port and waits for clients to connect to it.The clients establish a connection with the server socket using Socket object with ip address and port as parameter. After this connection is established, two streams(an input and an output) are available for the connection(some method like sc.getInputStream() /sc.getOutputStream()... where sc is the socket object) These streams are then used for connection communication by writing to the output stream and reading from the input stream. These can be wrapped over with a Reader/BufferedReader or Writer/BufferedWriter class.The reading part is not event triggered but process controlled i.e, you need to read explicitly when you are expecting something on the connection... A way out is to start a separate thread which would maintain the connection and continuously read from the stream. When it gets something on the stream it would set a static variable which can be used as a flag variable to poll once in a while and determine if the event has occurred.I have written this with an idea that you have more than basic idea of Java and a little knowledge of network communication. If I have not been clear in any part, do post your question...You can even find sample Client/Server program on the net. Execute both on your system and put in debug statements all over to get the idea of the flow of control.. Play around and you will learn a lot more...Cheers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.