0

I have one server listening on port and ip and a client which will connect to this server.

DataInputStream meterin=new DataInputStream(socket.getInputStream);
DataOutputStream modemds=new DataOutputStream(modems.getOutputStream);

now server is sending some data here:(CA F0 00 00 00 00 00 00 00 3A).But when I read this as

int c;
byte bt[]=new byte[11]
c=meterin.read(bt,0,11)`
System.out.println("bytes"+c)  // it is returning 10 bytes

modemds.write(bt,0,c)

but at client i am getting. (CA F0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00). which is more than 10 bytes even it is reading 10 bytes upwards.

4
  • Can you add the code you used to send the data? Commented Aug 11, 2012 at 9:38
  • 1
    I think that is the code used to send the data - but it's not clear why you're not looping round until you've read as much data as you expected, or what the code on the receiving side is. Please clarify your question, as it's hard to understand at the moment. Commented Aug 11, 2012 at 9:42
  • Oh. Then perhaps the issue lies at the client. Commented Aug 11, 2012 at 9:44
  • Actually here i do not know about the no of bytes to be read .Here server send commands which are not fixed and are of varying bytes and do not have and termination characters. Commented Aug 11, 2012 at 13:39

2 Answers 2

1

You cannot assume the read will fill the buffer. See readFully() for an alternative, or else loop until you get all the data you need.

Sign up to request clarification or add additional context in comments.

Comments

0

With some assumptions and fFrom the code it looks like you are reading the data from server and then relaying it to another listener or what you call as client. So on the client side code please check if the receiving buffer is initialized to more than the data being sent .. that would then lead to adding additional bytes in output. If that is the case, use System.arrayCopy to copy out the actual bytes from the receiving buffer (assuming the client code is java).

2 Comments

Data copying is unnecessary. write(buffer, 0, count) is all that is required.
Here client is basically a modem(socket) which is connected to some other server at different ip and port. so how should i check about receiving buffer initialization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.