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

  • Thread starter Bigmac Turdsplash
  • Start date
B

Bigmac Turdsplash

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

Michael Fellinger

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
 
B

Bertram Scharpf

Hi,

Am Montag, 07. Sep 2009, 08:41:45 +0900 schrieb 7stud --:
Michael said:
["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
 
B

Bertram Scharpf

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
 
B

Bigmac Turdsplash

7stud said:
Michael said:
["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?
 
B

Bigmac Turdsplash

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

7stud --

Bigmac said:
7stud said:
Michael said:
["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.
 
7

7stud --

Bigmac said:
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?
 
7

7stud --

7stud said:
Bigmac said:
7stud said:
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[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
 
B

Bigmac Turdsplash

7stud said:
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')
 
K

Ken Bloom

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}
 
7

7stud --

Ken said:
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
 
D

David A. Black

Hi --

Ken said:
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.
 
K

Ken Bloom

Ken said:
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.)
 
P

Paul Smith

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

Ken said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top