["test string\n", nil] i need to strip \n and nil

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the file,
"59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with the
string...

···

--
Posted via http://www.ruby-forum.com/.

["59023\n", nil].join.to_i

···

On Sun, Sep 6, 2009 at 10:27 PM, Bigmac Turdsplash<i8igmac@aim.com> wrote:

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the file,
"59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with the
string...

--
^ manveru

["59023\n", nil].compact.collect{|x| x.chomp}

···

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac Turdsplash wrote:

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the file,
"59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with the
string...

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Michael Fellinger wrote:

["59023\n", nil].join.to_i

You're kidding, right?

···

--
Posted via http://www.ruby-forum.com/\.

Ken Bloom wrote:

···

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac Turdsplash wrote:

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the file,
"59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with the
string...

["59023\n", nil].compact.collect{|x| x.chomp}

Didn't you really mean:

["59023\n", nil].inject("") {|acc, elmt|
"#{acc}#{elmt}"}.match(/\n/).pre_match

--
Posted via http://www.ruby-forum.com/\.

Hi,

Michael Fellinger wrote:
>
> ["59023\n", nil].join.to_i

You're kidding, right?

I don't beleive it either but it's time to say:

  ary.compact.map { |l| l.chomp }.join

This is expert level (and maybe even not portable):

  ary.compact!
  ary.each { |l| l.chomp! }
  result = ary.join

I'm quite sure that the result is always in the first array
element.

  res = ary.first
  res.chomp!

Everything untested.

Bertram

···

Am Montag, 07. Sep 2009, 08:41:45 +0900 schrieb 7stud --:

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

7stud -- wrote:

Michael Fellinger wrote:

["59023\n", nil].join.to_i

You're kidding, right?

I had it figured out a few minutes after this post... i didnt know the
differnce between a string and array... i convert the array to a string
then used compact

i thought i was working the string class so i was reading threw the
wrong page...

any ways... because of this premature post, i spent a few hours try to
solve a new problem before i ask for help...

strings="a9digh0t5"
# every 3 bytes i need broke into separate strings
puts(string[1]) #==> a9d
puts(string[2]) #==> ihg
puts(string[3]) #==> 0t5

how can i brake a long string into blocks equal to 3 bytes?

···

--
Posted via http://www.ruby-forum.com/\.

Hi --

···

On Tue, 8 Sep 2009, 7stud -- wrote:

Ken Bloom wrote:

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac Turdsplash wrote:

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the file,
"59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with the
string...

["59023\n", nil].compact.collect{|x| x.chomp}

Didn't you really mean:

["59023\n", nil].inject("") {|acc, elmt|
"#{acc}#{elmt}"}.match(/\n/).pre_match

I don't think so. That seems a bit roundabout.

David

--
David A. Black / Ruby Power and Light, LLC / http://www.rubypal.com
Ruby/Rails training, mentoring, consulting, code-review
Latest book: The Well-Grounded Rubyist (http://www.manning.com/black2\)

September Ruby training in NJ has been POSTPONED. Details to follow.

No, that's not quite the same at all. (I did exactly what he asked for in
the subject line.)

···

On Tue, 08 Sep 2009 03:50:24 +0900, 7stud -- wrote:

Ken Bloom wrote:

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac Turdsplash wrote:

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the
file, "59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with
the string...

["59023\n", nil].compact.collect{|x| x.chomp}

Didn't you really mean:

["59023\n", nil].inject("") {|acc, elmt|
"#{acc}#{elmt}"}.match(/\n/).pre_match

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Hi,

···

Am Montag, 07. Sep 2009, 11:33:13 +0900 schrieb Bertram Scharpf:

I'm quite sure that the result is always in the first array
element.

  res = ary.first
  res.chomp!

I just looked it up and found thhat you probably use TCPSocket#recvfrom.

  data, status = socket.recvfrom ...
  data.chomp!

Bertram

--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de

#server.rb
require 'socket' # Get sockets from stdlib
server = TCPServer.open(2000) # Socket to listen on port 2000
loop { # Servers run forever
  Thread.start(server.accept) do |client|
  file = File.new('/pentest/windows-binaries/tools/test.exe')
  fileContent = file.read # reads the file
  client.puts(fileContent)
  client.close
end

}

#client
require 'socket'

host = '192.168.1.4'
port = 2000
sock = TCPSocket.open(host, port)

  data = sock.recvfrom(999999)
  destFile = File.open('/tmp/poop.exe', 'wb')
  destFile.print data
  destFile.close

Ok, just to show you what im working with... it works, but there is a
file size limit when working with recvfrom()

im trying to work out a function to check file size then brake the data
into blocks...

···

--
Posted via http://www.ruby-forum.com/.

Bigmac Turdsplash wrote:

7stud -- wrote:

Michael Fellinger wrote:

["59023\n", nil].join.to_i

You're kidding, right?

I had it figured out a few minutes after this post... i didnt know the
differnce between a string and array... i convert the array to a string
then used compact

Ridiculous. You would never compact an array just to get the first
element. If you want the first element, then grab it. See Bertram's
posts.

i thought i was working the string class so i was reading threw the
wrong page...

any ways... because of this premature post, i spent a few hours try to
solve a new problem before i ask for help...

strings="a9digh0t5"
# every 3 bytes i need broke into separate strings
puts(string[1]) #==> a9d
puts(string[2]) #==> ihg
puts(string[3]) #==> 0t5

how can i brake a long string into blocks equal to 3 bytes?

If the length of the string will always be a multiple of 3, you can do
this:

str = "a9digh0t5"

str.scan(/.{3}/) do |substr|
  puts substr
end

--output:--
a9d
igh
0t5

Otherwise, you'll have to deal with ruby's infernal each_byte() method.

···

--
Posted via http://www.ruby-forum.com/\.

I think 7stud is being sarcastic... He thinks the compact is
unnecessary, and is explaining his point by introducing other
unnecessary elements like inject.

···

On Tue, Sep 8, 2009 at 1:08 AM, Ken Bloom<kbloom@gmail.com> wrote:

On Tue, 08 Sep 2009 03:50:24 +0900, 7stud -- wrote:

Ken Bloom wrote:

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac Turdsplash wrote:

im sending files back and forth form a client and a server using
sockets,

the first string the server sends to the client is the size of the
file, "59023"

the client recives this string in this format "59023\n", nil

i need to trim \n and nil from the string so i can do some math with
the string...

["59023\n", nil].compact.collect{|x| x.chomp}

Didn't you really mean:

["59023\n", nil].inject("") {|acc, elmt|
"#{acc}#{elmt}"}.match(/\n/).pre_match

No, that's not quite the same at all. (I did exactly what he asked for in
the subject line.)

--
Chanoch (Ken) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

--
Paul Smith
http://www.nomadicfun.co.uk

paul@pollyandpaul.co.uk

Bigmac Turdsplash wrote:

require 'socket'

host = '192.168.1.4'
port = 2000
sock = TCPSocket.open(host, port)

  data = sock.recvfrom(999999)
  destFile = File.open('/tmp/poop.exe', 'wb')
  destFile.print data
  destFile.close

Ok, just to show you what im working with... it works, but there is a
file size limit when working with recvfrom()

Skipping the basics of a language never works, does it?

···

--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote:

Bigmac Turdsplash wrote:

7stud -- wrote:

Michael Fellinger wrote:

["59023\n", nil].join.to_i

You're kidding, right?

I had it figured out a few minutes after this post... i didnt know the
differnce between a string and array... i convert the array to a string
then used compact

Ridiculous. You would never compact an array just to get the first
element. If you want the first element, then grab it. See Bertram's
posts.

i thought i was working the string class so i was reading threw the
wrong page...

any ways... because of this premature post, i spent a few hours try to
solve a new problem before i ask for help...

strings="a9digh0t5"
# every 3 bytes i need broke into separate strings
puts(string[1]) #==> a9d
puts(string[2]) #==> ihg
puts(string[3]) #==> 0t5

how can i brake a long string into blocks equal to 3 bytes?

If the length of the string will always be a multiple of 3, you can do
this:

str = "a9digh0t5"

str.scan(/.{3}/) do |substr|
  puts substr
end

--output:--
a9d
igh
0t5

Otherwise, you'll have to deal with ruby's infernal each_byte() method.

Actually, you can take advantage of a regex's greedy nature, and do this
instead:

str = "a9digh0t5xx"

str.scan(/.{1,3}/) do |substr|
  puts substr
end

--output:--
a9d
igh
0t5
xx

···

--
Posted via http://www.ruby-forum.com/\.

7stud -- wrote:

Bigmac Turdsplash wrote:

require 'socket'

host = '192.168.1.4'
port = 2000
sock = TCPSocket.open(host, port)

  data = sock.recvfrom(999999)
  destFile = File.open('/tmp/poop.exe', 'wb')
  destFile.print data
  destFile.close

Ok, just to show you what im working with... it works, but there is a
file size limit when working with recvfrom()

Skipping the basics of a language never works, does it?

lol, oops... data = sock.read

how can i send a hole directory?
#server
directory = File.new('/pentest/windows-binaries/tools/')
#client
folder.open('/tmp/directory/', 'wb')

···

--
Posted via http://www.ruby-forum.com/\.