Solved

Read from socket

Posted on 2002-06-02
9
680 Views
Last Modified: 2010-08-05
Hi all
I am using non blocking IO for connecting clients to IRC server. In order to do so, I read from the socket using ByteBuffer. Is it possible to read line after line instead of buffer? I need to parse each line seperatlly and It's a problem reading the information in blocks.
   
    Thanks
0
Comment
Question by:dannysh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 7049501
 Yes you can. Create a BufferedReadre instead:

BufferedReader reader = new BufferedReader(new InputStreamReader(<your socket variiable>.getInputStream()));

  Then use it like:

  String line = reader.readLine();

  to read the data line by line.

  Hope it helps.
0
 

Author Comment

by:dannysh
ID: 7049503
Hi girionis
Can I use BufferedReader in socketChannel in order to read from the socket? I think you can't do it am I right? I am using socketChannel to read from the socket.
0
 
LVL 35

Expert Comment

by:girionis
ID: 7049627
 Hmmm. I really do not know. I am not very familiar with the nio package. Maybe if you take a look at the documentation will help you.

  Hope it helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Accepted Solution

by:
girionis earned 50 total points
ID: 7050162
 Something else you could do is to read the characters one by one until you find the newline character ('\n') and then append it to a string. Since a new line characters indicates the end of the line then I guess it would be equal as reading a whole line using BufferedReader's readLine() method.

  Hope it helps.
0
 

Author Comment

by:dannysh
ID: 7050418
Hi girionis
reading char by char can be very slow specially when there are huge number of sockets.
   
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7050493
I would say - this is the only way to search for '\n' character. The BufferedReader is doing the same. To improve the performance of this reading - wrap the socket stream with a BufferedInputStream:

java.io.BufferedInputStream in = new java.io.BufferedInputStream(<your socket variiable>.getInputStream());

 
0
 

Author Comment

by:dannysh
ID: 7050499
Hi Venci75
As far as I know the architucture of socketChannel and Selector does not allow you using the BufferedInputStream for reading the information.
I think I will read the information into ByteBuffer and parse this buffer looking the /n char which will indicate new line.
It is not possible using BufferedInputStream.
 
0
 

Author Comment

by:dannysh
ID: 7065149
I used this idea.
  Thanks
0
 
LVL 35

Expert Comment

by:girionis
ID: 7065493
 Thank you for the points dannysh. I am glad I helped :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question