• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Reading a stream from a socket

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, i have some data i want to read from a socket. I wrote the following code:




The problem is that i have the following output:

fÆ$[] something like this.

well i saw a code in C language and the guy that wrote it read it to a char buffer[]. That why i think i could read to a string. The output was supost to show me some numbers.

Please i apreciate any help you all can give me, i'm new in Java programming and there are some aspects i dont know yet so maybe i'm going on the wrong direction.


my kind regards

(Edited: code tags are much better then unindented and monochrome code. -- Paul C)

 
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First tell us exactly what data is supposed to be sent over that socket. And exactly what its format is.
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Assuming that the char buffer (in C) maps to a string (in Java) is not a good idea. In C, the char buffer is commonly known for holding binary data. If you see a char buffer, it doesn't necessary mean that you have a string. And considering your output, it is very likely that you have binary data.

Henry


Pedro Neves wrote:Hello, i have some data i want to read from a socket. I wrote the following code:




The problem is that i have the following output:

fÆ$[] something like this.

well i saw a code in C language and the guy that wrote it read it to a char buffer[]. That why i think i could read to a string. The output was supost to show me some numbers.

Please i apreciate any help you all can give me, i'm new in Java programming and there are some aspects i dont know yet so maybe i'm going on the wrong direction.


my kind regards

(Edited: code tags are much better then unindented and monochrome code. -- Paul C)

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well i talk to the guy that wrote the C code and mhe told me i have to read bytes and. the output wil be sometinhg like this:


006 000 002 000 001 000 069 005 109 053 083 052 000 000 178 052 070 000 240 110 119 031 206 074 012 000
006 000 111 000 001 000 173 000 099 008 125 006 000


how can i do this? can i read it entirely to a byte array?
 
Paul Clapham
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. So far all we know is that your input is "bytes". That isn't nearly enough information to write code for.

You could certainly read in those bytes, and put them into a byte array. (Write them to a ByteArrayOutputStream if that's what you want to do.) But then what? You still need information about what the bytes mean and how they are organized. Just knowing that they are "bytes" is useless, everything is made of bytes.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And wouldn't it be easier to take the C code and port it over to Java? ... definitely easier that speculating on how what is coming over the socket maps to the output.

Henry
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Okay. So far all we know is that your input is "bytes". That isn't nearly enough information to write code for.

You could certainly read in those bytes, and put them into a byte array. (Write them to a ByteArrayOutputStream if that's what you want to do.) But then what? You still need information about what the bytes mean and how they are organized. Just knowing that they are "bytes" is useless, everything is made of bytes.




Well what i want is to read byte after byte and print its value on the screen. The data comes over the socket on a row format. :-/
 
Paul Clapham
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pedro Neves wrote:Well what i want is to read byte after byte and print its value on the screen. The data comes over the socket on a row format. :-/



That doesn't make sense, if the example you posted is a real example. Most of the bytes in that example are control characters and half of them are nulls. Or perhaps I don't understand what you mean by the "value" of a byte. What would the value of the byte you describe as "006" be? What about "206"?

And "row format" doesn't mean anything that I can see based on that example either.
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Pedro Neves wrote:Well what i want is to read byte after byte and print its value on the screen. The data comes over the socket on a row format. :-/



That doesn't make sense, if the example you posted is a real example. Most of the bytes in that example are control characters and half of them are nulls. Or perhaps I don't understand what you mean by the "value" of a byte. What would the value of the byte you describe as "006" be? What about "206"?

And "row format" doesn't mean anything that I can see based on that example either.




Well as far as i know the the output is a bytestream i mean, i have to catch byte after byte and then represent ir. I have to read the information that a TelosB send to my app. I read bytes and the i represent them (which i supose i have to represent it as integers )

Flag (bit0=ADC0, bit1=ADC1, ..., bit11=CPUtemp)
102 003 000 019 000 003 000 032 000 002 000 002 000 255 015
102 003 000 019 000 003 000 032 000 002 000 001 000 064 000
102 003 000 019 000 003 000 032 000 002 000 002 000 048 000


How can i catch all the bytes?

I'm sorry if im not expressing well. In fact i dont know how to be more explicit (i think)

my regards and thanks to all who are tryint to help me
 
Paul Clapham
Marshal
Posts: 28298
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can catch all of the bytes simply by reading them from the input stream. And you can write them to somewhere else if you like. However if you plan to interpret them in some way then you have to understand what they mean.

It's just like reading a document. Anybody can read through it one letter at a time and say "There's a capital A, there's an s, there's a space" and so on. But normally those letters combine together to mean something other than just a list of letters, and there are rules about how they do that. If you don't know those rules then you're illiterate and the letters will mean nothing to you.
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pedro Neves wrote:
Well as far as i know the the output is a bytestream i mean, i have to catch byte after byte and then represent ir. I have to read the information that a TelosB send to my app. I read bytes and the i represent them (which i supose i have to represent it as integers )

Flag (bit0=ADC0, bit1=ADC1, ..., bit11=CPUtemp)
102 003 000 019 000 003 000 032 000 002 000 002 000 255 015
102 003 000 019 000 003 000 032 000 002 000 001 000 064 000
102 003 000 019 000 003 000 032 000 002 000 002 000 048 000


How can i catch all the bytes?



Yes. If you just want get and print a raw byte stream, then reading into a byte buffer would work. One issue though -- it looks like from the printouts that the byte stream are of unsigned bytes. Java supports signed bytes. So, don't forget to convert it (like cast to an int followed by ANDing out the high order bits) prior to printing.

Henry

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Pedro Neves wrote:
Well as far as i know the the output is a bytestream i mean, i have to catch byte after byte and then represent ir. I have to read the information that a TelosB send to my app. I read bytes and the i represent them (which i supose i have to represent it as integers )

Flag (bit0=ADC0, bit1=ADC1, ..., bit11=CPUtemp)
102 003 000 019 000 003 000 032 000 002 000 002 000 255 015
102 003 000 019 000 003 000 032 000 002 000 001 000 064 000
102 003 000 019 000 003 000 032 000 002 000 002 000 048 000


How can i catch all the bytes?



Yes. If you just want get and print a raw byte stream, then reading into a byte buffer would work. One issue though -- it looks like from the printouts that the byte stream are of unsigned bytes. Java supports signed bytes. So, don't forget to convert it (like cast to an int followed by ANDing out the high order bits) prior to printing.

Henry





well im finally starting to understand :)

But i still dont understand how can i convert the bytes to have some like this:

START MOBILE AGENT 1
Sent Message type
Destination Logical Node ID
Controller Message
FDS
Control Message
Agent ID
Sigma Factor (:10)
Sampling Time (sec)
ADC's
107 102 001 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
102 102 005 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
108 102 009 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
110 102 013 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
109 102 002 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
112 102 006 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
105 102 010 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
113 102 014 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
111 102 003 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
106 102 007 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
103 102 011 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
104 102 015 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000

as far as i can i only got this:

102 -89 36 83 40 5 0 58 32 1 0 58 0 3 0 8 0 111 0 2 0 -1 15 -121 9 77
102 -88 36 -73 40 5 0 58 32 1 0 59 0 3 0 8 0 111 0 2 0 -1 15 -121 9 75
102 -87 36 27 41 5 0 58 32 1 0 60 0 3 0 8 0 111 0 2 0 -1 15 -111 9 97
102 -86 36 127 41 5 0 58 32 1 0 61 0 3 0 8 0 111 0 2 0 -1 15 -97 9 127
100 -85 62 -31 41 5 0 24 1 1 0 54 0 3 0 7 0 111 0 22 0 0 0 22 0 0
0 0 0 0 0 0 0 0 0 0 12 64 78 79 0 0 -112 25 0 0 0 0 104 0 15 0
95 9 -71 9 -77 9 -31 0 37 1 -39 5 119 8 -3 24 124 5 -97 9 -57 6 -66 43 5 0
73 9 -119 9 91 9 -31 0 37 1 -35 5 127 8 -4 24 123 5 -97 9 -57 6 34 44 5 0

how can i convert it well with my code? :



package Sockets;

import java.lang.*;
import java.io.*;
import java.net.*;

class Client {
public static void main(String args[]) {

String s="192.168.241.132";
boolean flag=true;


try {
Socket skt = new Socket(s, 12345);

DataInputStream in = new DataInputStream(skt.getInputStream());
byte[] dyn_data = new byte[51];


do
{


System.out.print("\n");

in.readFully(dyn_data,0, 51);

DataInputStream speedData = new DataInputStream(new ByteArrayInputStream(dyn_data));


for ( int i = 0; i < speedData.available(); i++ )
{


byte ch = speedData.readByte();
System.out.print((int) ch + " ");
}


}while(flag);

in.close();
}
catch(Exception e) {
System.out.print("erro de connecção\n");
}
}
}




Thanks for all
 
Henry Wong
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To convert an unsigned byte, held in a signed byte variable, back to the original value you can do this...



Of course, this doesn't take care of the formatting which you still need to do.

Henry

Pedro Neves wrote:
well im finally starting to understand :)

But i still dont understand how can i convert the bytes to have some like this:

START MOBILE AGENT 1
Sent Message type
Destination Logical Node ID
Controller Message
FDS
Control Message
Agent ID
Sigma Factor (:10)
Sampling Time (sec)
ADC's
107 102 001 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
102 102 005 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
108 102 009 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
110 102 013 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
109 102 002 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
112 102 006 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
105 102 010 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
113 102 014 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
111 102 003 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
106 102 007 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
103 102 011 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
104 102 015 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000

as far as i can i only got this:

102 -89 36 83 40 5 0 58 32 1 0 58 0 3 0 8 0 111 0 2 0 -1 15 -121 9 77
102 -88 36 -73 40 5 0 58 32 1 0 59 0 3 0 8 0 111 0 2 0 -1 15 -121 9 75
102 -87 36 27 41 5 0 58 32 1 0 60 0 3 0 8 0 111 0 2 0 -1 15 -111 9 97
102 -86 36 127 41 5 0 58 32 1 0 61 0 3 0 8 0 111 0 2 0 -1 15 -97 9 127
100 -85 62 -31 41 5 0 24 1 1 0 54 0 3 0 7 0 111 0 22 0 0 0 22 0 0
0 0 0 0 0 0 0 0 0 0 12 64 78 79 0 0 -112 25 0 0 0 0 104 0 15 0
95 9 -71 9 -77 9 -31 0 37 1 -39 5 119 8 -3 24 124 5 -97 9 -57 6 -66 43 5 0
73 9 -119 9 91 9 -31 0 37 1 -35 5 127 8 -4 24 123 5 -97 9 -57 6 34 44 5 0

how can i convert it well with my code? :






Thanks for all

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:To convert an unsigned byte, held in a signed byte variable, back to the original value you can do this...



Of course, this doesn't take care of the formatting which you still need to do.

Henry

Pedro Neves wrote:
well im finally starting to understand :)

But i still dont understand how can i convert the bytes to have some like this:

START MOBILE AGENT 1
Sent Message type
Destination Logical Node ID
Controller Message
FDS
Control Message
Agent ID
Sigma Factor (:10)
Sampling Time (sec)
ADC's
107 102 001 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
102 102 005 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
108 102 009 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
110 102 013 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
109 102 002 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
112 102 006 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
105 102 010 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
113 102 014 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
111 102 003 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
106 102 007 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
103 102 011 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000
104 102 015 000 019 000 003 000 032 000 001 000 015 000 001 000 000 000

as far as i can i only got this:

102 -89 36 83 40 5 0 58 32 1 0 58 0 3 0 8 0 111 0 2 0 -1 15 -121 9 77
102 -88 36 -73 40 5 0 58 32 1 0 59 0 3 0 8 0 111 0 2 0 -1 15 -121 9 75
102 -87 36 27 41 5 0 58 32 1 0 60 0 3 0 8 0 111 0 2 0 -1 15 -111 9 97
102 -86 36 127 41 5 0 58 32 1 0 61 0 3 0 8 0 111 0 2 0 -1 15 -97 9 127
100 -85 62 -31 41 5 0 24 1 1 0 54 0 3 0 7 0 111 0 22 0 0 0 22 0 0
0 0 0 0 0 0 0 0 0 0 12 64 78 79 0 0 -112 25 0 0 0 0 104 0 15 0
95 9 -71 9 -77 9 -31 0 37 1 -39 5 119 8 -3 24 124 5 -97 9 -57 6 -66 43 5 0
73 9 -119 9 91 9 -31 0 37 1 -35 5 127 8 -4 24 123 5 -97 9 -57 6 34 44 5 0

how can i convert it well with my code? :






Thanks for all



Man thanks to all. Believe me i could not to do that work without u. I'm learning Java for almost 2 weeks i really did not know what to do.

My kind regards to all

Thanks
 
This is awkward. I've grown a second evil head. I'm going to need a machete and a tiny ad ...
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic