["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…

On Sun, Sep 6, 2009 at 10:27 PM, Bigmac T.[email protected]
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].join.to_i

Michael F. wrote:

[“59023\n”, nil].join.to_i

You’re kidding, right?

Hi,

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

Michael F. 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

7stud – wrote:

Michael F. 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[x]

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?

Hi,

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

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

#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…

Bigmac T. wrote:

7stud – wrote:

Michael F. 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[x]

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.

7stud – wrote:

Bigmac T. wrote:

7stud – wrote:

Michael F. 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[x]

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

Bigmac T. 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?

7stud – wrote:

Bigmac T. 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’)

Ken B. wrote:

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac T. 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

On Mon, 07 Sep 2009 05:27:10 +0900, Bigmac T. 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}

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

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.)

Hi –

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

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.

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 B.[email protected] wrote:

“#{acc}#{elmt}”}.match(/\n/).pre_match


Paul S.
http://www.nomadicfun.co.uk

[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs