Jul 26, 2008

Socket Class

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Socket Class

kvarnerexpress
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

Reply

cse-icons
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.

 

 

 


Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : socket


    Looking for socket, class

Searching Video's for socket, class
advertisement



Socket Class



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE