Forum: Ruby CR/LF in data read from a socket

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4708a793f80176885038679776e43ac7?d=identicon&s=25 mac (Guest)
on 2006-01-24 15:57
(Received via mailing list)
Hello,
I am trying to write a client/server program in an effort to learn Ruby.
I
have been playing around with Ruby for about 2 months and I really love
it.
As I developed my program (DICOM tool kit) I am faced with an issue on
which
I spend almost a day to figure out. I know what is happening but don't
know
why and how to correct it. Any pointers will help. Thanks. Here it is:


++++++++++++++++++++++++++++++++++++++++
Snippet of the code:

.
# open the socket
require 'socket'
port =4010
server = TCPServer.new('localhost', port)
while (session = server.accept)

}
.
.
.
# read the stream
 ind=0
 while msgCtrlHdr!= 2
  p "Inside handleC_STORE_RQ Entering Loop....."
  # read next PDU
  pduType= session.getc
  printf("PDU Type = %d\n",pduType)
  # read the pdu length and the pdu itself.
  pduLength=session.read(5).unpack('xN')[0]
  print "pduLength =",pduLength,"\n"
  pdu=session.read(pduLength)
  itemLength, presCtxt=pdu.slice!(0,5).unpack('Nc')
  print "itemLength =",itemLength,"   presCtxt =",presCtxt,"\n"
  msgCtrlHdr=pdu.slice!(0,1).unpack('c')[0]
  outputFile = File.new( "test_image"+ind.to_s,"w")
  outputFile.syswrite(pdu)
  ind=ind+1
  image << pdu
  #printf("Image Length = %d\n",image.length)
 end

++++++++++++++++++++++++++++++++++++++++++++++++++

As you can see mine is the server part and the client is a standard Java
program that sends out images, I did not write it, it is a commercial
program.
When I load the output file in to a hex editor (Ultraedit) I see "0D"
and
"0A" chars (CR and LF) in the file, several of them where they should
not
be.
Following is a snippet of the data from ultra edit.

00000000h: 08 00 00 00 04 00 00 00 A0 01 00 00 08 00 08 00 ;
........ .......
00000010h: 1A 00 00 00 4F 52 49 47 49 4E 41 4C 5C 50 52 49 ;
....ORIGINAL\PRI
00000020h: 4D 41 52 59 5C 55 4E 44 45 46 49 4E 45 44 08 00 ;
MARY\UNDEFINED..
00000030h: 16 00 1A 00 00 00 31 2E 32 2E 38 34 30 2E 31 30 ;
......1.2.840.10
00000040h: 30 30 38 2E 35 2E 31 2E 34 2E 31 2E 31 2E 34 00 ;
008.5.1.4.1.1.4
.
00000050h: 08 00 18 00 34 00 00 00 31 2E 33 2E 31 32 2E 32 ;
....4...1.3.12.2
00000060h: 2E 31 31 30 37 2E 35 2E 38 2E 31 2E 31 32 33 34 ;
.1107.5.8.1.1234
00000070h: 35 36 37 38 39 2E 31 39 39 35 30 37 32 37 31 37 ;
56789.1995072717
00000080h: 35 38 30 35 30 37 30 37 37 36 35 00 08 00 20 00 ;
58050707765...
.
00000090h: 08 00 00 00 31 39 39 34 30 36 31 33 08 00 22 00 ;
....19940613..".
000000a0h: 08 00 00 00 31 39 39 34 30 36 31 33 08 00 23 00 ;
....19940613..#.
000000b0h: 08 00 00 00 31 39 39 34 30 36 31 33 08 00 30 00 ;
....19940613..0.
000000c0h: *0D 0A* 00 00 00 31 35 33 32 32 36 2E 33 39 32 08 ;
.....153226.392.
000000d0h: 00 32 00 *0D 0A* 00 00 00 31 35 33 32 32 37 2E 37 ;
.2......153227.7
000000e0h: 33 30 08 00 33 00 *0D 0A* 00 00 00 31 35 33 35 32 ;
30..3......15352
000000f0h: 35 2E 30 30 30 08 00 40 00 02 00 00 00 00 00 08 ; 5.000.
.@........
00000100h: 00 41 00 08 00 00 00 49 4D 41 20 4E 4F 4E 45 08 ; .A.....IMA
NONE.
00000110h: 00 50 00 *0D 0A* 00 00 00 53 4D 53 30 30 30 30 31 ;
.P......SMS00001
00000120h: 33 20 08 00 60 00 02 00 00 00 4D 52 08 00 70 00 ; 3
..`.....MR..p.
00000130h: 08 00 00 00 53 49 45 4D 45 4E 53 20 08 00 80 00 ; ....SIEMENS
..â?¬.
00000140h: 1A 00 00 00 44 69 61 67 20 52 61 64 69 6F 6C 6F ; ....Diag
Radiolo
00000150h: 67 79 20 47 72 6F 73 73 68 61 64 65 72 6E 08 00 ; gy
Grosshadern..
00000160h: 90 00 00 00 00 00 08 00 10 10 1A 00 00 00 44 69 ;
.............Di
00000170h: 61 67 20 52 61 64 69 6F 6C 6F 67 79 20 47 72 6F ; ag
Radiology
Gro
00000180h: 73 73 68 61 64 65 72 6E 08 00 30 10 00 00 00 00 ;
sshadern..0.....
00000190h: 08 00 80 10 00 00 00 00 08 00 90 10 10 00 00 00 ;
..�............
000001a0h: 4D 41 47 4E 45 54 4F 4D 20 56 49 53 49 4F 4E 20 ; MAGNETOM
VISION

000001b0h: 09 00 00 00 04 00 00 00 A0 01 00 00 09 00 10 00 ;
........ .......
000001c0h: 0E 00 00 00 53 50 49 20 52 45 4C 45 41 53 45 20 ; ....SPI
RELEASE

000001d0h: 31 20 09 00 12 00 14 00 00 00 53 49 45 4D 45 4E ; 1
........SIEMEN
000001e0h: 53 20 43 4D 20 56 41 30 20 20 43 4D 53 20 09 00 ; S CM VA0
CMS
..
000001f0h: 13 00 14 00 00 00 53 49 45 4D 45 4E 53 20 43 4D ;
......SIEMENS
CM
00000200h: 20 56 41 30 20 20 4C 41 42 20 09 00 10 10 12 00 ;  VA0  LAB
......
93d566cc26b230c553c197c4cd8ac6e4?d=identicon&s=25 Pit Capitain (Guest)
on 2006-01-25 08:55
(Received via mailing list)
mac schrieb:
>   outputFile = File.new( "test_image"+ind.to_s,"w")
>   outputFile.syswrite(pdu)
> ...
> When I load the output file in to a hex editor (Ultraedit) I see "0D" and
> "0A" chars (CR and LF) in the file, several of them where they should not
> be.

Hi Mac,

if you're on Windows you have to open the output file in binary mode:

   outputFile = File.new( "test_image"+ind.to_s, "wb" )

Also look at the block form of File.open to automatically close the
output file.

Regards,
Pit
4708a793f80176885038679776e43ac7?d=identicon&s=25 mac (Guest)
on 2006-01-26 05:44
(Received via mailing list)
Pit,
Thanks for the correction, that fixed it! Really appreciate it.
There is obiviously lack of dacumentation in Ruby world.
9dfe8c734b0f9b37a4e218425c0a2138?d=identicon&s=25 Gene Tani (Guest)
on 2006-01-26 06:44
(Received via mailing list)
mac wrote:
> Pit,
> Thanks for the correction, that fixed it! Really appreciate it.
> There is obiviously lack of dacumentation in Ruby world.
>

I had to respond to this.  I'm not sure if you're comparing to
java/.NET, or maybe perl/python or lisp/smalltalk/ML/some of those
kinds of environments.  There actually is heaps of info on the net.
I've seen people produce lots of top-quality code in response to
queries on this mail list, really detailed analysis, etc.  It's really
extraordinary, sometimesyou have to ask.
This topic is locked and can not be replied to.