Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:26:52+00:00 2026-06-12T02:26:52+00:00

I’m writing a basic network logging script where a client can connect to my

  • 0

I’m writing a basic network logging script where a client can connect to my server socket, and all data read from the socket will be written to file.

I have it so I can connect with telnet and the first line (before enter is pressed) is written to file, but subsequent lines are not being written. What do I need to change to make this work properly?

I also want to be able to have multiple listening sockets writing to the same file – is there a way to do this without multi-threading?

log_path ||= "./network-logs"
listen_port ||= 2509
minute_buffer = {now: Time.now, buffer: "" }
current_time = Time.now
current_date = Date.today.to_s
client = nil
Dir.mkdir(log_path) unless File.exists?(log_path)
f = File.new("#{log_path}/#{current_date}.txt", 'w')

def short_time
  "%02d" % Time.now.hour.to_s + ':' + 
  "%02d" % Time.now.min.to_s + ':' + 
  "%02d" % Time.now.sec.to_s
end

server = TCPServer.open(listen_port)
loop {                                  
  client = server.accept                
  f.puts "#{Time.now} Client Connected"
  puts "#{Time.now} Client Connected"
  current_time = Time.now               
  if minute_buffer[:now].min != Time.now.min 
    f.puts "##### Minute summary for #{minute_buffer[:now].to_s}: #{minute_buffer[:min]} :"
    puts "##### Minute summary for #{minute_buffer[:now].to_s}: #{minute_buffer[:min]} :"
    f.puts "##### #{minute_buffer[:buffer]}"
    minute_buffer[:now] = Time.now
    minute_buffer[:buffer] = ""
  end
  data = client.gets
  f.puts short_time + "  : " + data.to_s
  puts short_time + "  : " + data.to_s
  minute_buffer[:buffer] = minute_buffer[:buffer] + data.to_s
  if current_date != Date.today.to_s
    f.close
    current_date = Date.today.to_s
    f = File.new("#{log_path}/#{current_date}.txt", 'w')
  end
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:26:53+00:00Added an answer on June 12, 2026 at 2:26 am

    You have to move the client = server.accept line out of the loop:

    ...
    server = TCPServer.open(listen_port)
    client = server.accept # <- moved out of the loop
    loop {                                  
      f.puts "#{Time.now} Client Connected"
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.