Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

read() and write() method of socket channel did not work fine for huge data  RSS feed

 
sunil sahoo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have implemented socket channel for client server application.
That program works fine when i am sending data of 10000 characters but when I am sending data of more than 50000 characters, then that program did not work fine.
For sending data I am using
String message = "my own data"+"\n"
ByteBuffer buf = ByteBuffer.wrap(message.getBytes());
SocketChannel.write(buf);

And for recieving tha data I am using
BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
br.readLine();

My server is in socket channel and client is simple socket.


Thanks
Sunil
sunil.sahoo@artificialmachines.com
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "did not work fine"? You need to Tell The Details
 
sunil sahoo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I mean that I can not read the whole data only I can read a part of data while C, C++ people can get the whole data.
So how to read the data using TCP socket in JAVA if the data is so long
Thanks
Sunil
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no limitation to the amount of data that can be written to a stream/socket/channel.
I'm sorry, but I don't have much experience with NIO. Have you tried with streams? That I can help with.
 
sunil sahoo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi As per our discussion I have changed my whole code into simple socket. There is no NIO now.
Means now it is on stream.
Both server and client work on socket only.

Here also I faced the same problem I can read and write a string of maximum 20,000 characters but i want to read and write more data without using filereader writer.

Thanks
Sunil

 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're going to have to show us some code. As I said before, there is no limitation to the amount of data you can read/write with a stream.
Is your code failing without error or are you getting an exception (if you are, what is it)?
 
Ankur Goel Goel
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried sending huge data and i am successful in receiving it. There are problems when the buffer is full.

You can try using flush command.

and along with it you read byte by byte it might help like shown in

http://techtadka.net/articles/programming/203-how-to-read-and-write-data-from-socket-in-java.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!