在Ruby中写入套接字时出现套接字错误

时间:2014-12-13 21:57:32

标签: ruby sockets tcp network-programming udp

我有以下代码:

  # Connect to neighbors and send hello message
  current_node.neighbors_addresses.each do |address| 
    for i in 0..10
      begin
        sock = TCPSocket.new(address, 3000)
        break
      rescue Errno::EHOSTUNREACH, Errno::ECONNREFUSED
        puts "before sleep"
        sleep(2)
        puts "after sleep"
      end
    end
    sock.write("Hi from node #{current_node.name}\n")
  end

current_node是一个节点对象,其neighbor_addresses是一个实例变量,用于存储当前节点邻居的IP地址。

问题是,行sock.write("Hi from node #{current_node.name}\n")给出错误:

server1.rb:211:in `block in main': undefined method `write' for nil:NilClass (NoMethodError)
    from server1.rb:200:in `each'
    from server1.rb:200:in `main'
    from server1.rb:276:in `<main>'

如果write不是套接字类中的方法,那么如何写入该套接字?

1 个答案:

答案 0 :(得分:0)

public DashboardVm GetAllTraineeDataForDashbaord(int teamId) { var data = _context.Users.Where(x => x.IsTrainee == true && (teamId == 0 || x.TeamId == teamId) && x.IsActive == true) .GroupJoin(_context.Feedbacks.Include(x => x.User), u => u.UserId, f => f.AddedFor, (u, f) => new {u, f}) .OrderBy(x=>x.u.UserName).AsNoTracking() .ToList(); //if (data.Any(r => r.f != null && r.f.Any(t => t.User == null))) //{ // LogUtility.ErrorRoutine(new Exception("lastWeeklyFeedback is null:")); //} UserConverter userConverter = new UserConverter(); FeedbackConverter feedbackConverter = new FeedbackConverter(); DateTime mondayTwoWeeksAgo = Common.Utility.UtilityFunctions.GetLastDateByDay(DayOfWeek.Monday, DateTime.Now.AddDays(-14)); DateTime lastFriday = Common.Utility.UtilityFunctions.GetLastDateByDay(DayOfWeek.Friday, DateTime.Now); DateTime lastMonday = lastFriday.AddDays(-5); var concurrentTrainee = new ConcurrentQueue<UserData>(); // data.AsParallel().ForAll(traineeLocal=> Parallel.ForEach(data, traineeLocal => // foreach(var trainee in data) { var trainee = traineeLocal; bool lastWeekFeedbackAdded = trainee.u.DateAddedToSystem >= lastFriday || (trainee.f.Any(x => x.FeedbackType == (int)Common.Enumeration.FeedbackType.Weekly && ( x.StartDate >= lastMonday || (x.EndDate <= lastFriday && x.EndDate >= lastMonday)))); Feedback lastWeeklyFeedback = lastWeekFeedbackAdded ? (trainee.f.OrderByDescending(feedback => feedback.FeedbackId) .FirstOrDefault(x => x.FeedbackType == (int)Common.Enumeration.FeedbackType.Weekly)) : null; concurrentTrainee.Enqueue(new UserData { User = userConverter.ConvertFromCore(trainee.u), IsCodeReviewAdded = trainee.u.DateAddedToSystem >= lastFriday || trainee.f.Any(x => x.FeedbackType == (int)Common.Enumeration.FeedbackType.CodeReview && x.AddedOn >= mondayTwoWeeksAgo), LastWeekFeedbackAdded = lastWeekFeedbackAdded, WeeklyFeedback = lastWeeklyFeedback == null ? new List<Common.Entity.Feedback>() : new List<Common.Entity.Feedback> { feedbackConverter.ConvertFromCore(lastWeeklyFeedback)}, RemainingFeedbacks = feedbackConverter.ConvertListFromCore(trainee.f.Where(x => x.FeedbackType == (int)Common.Enumeration.FeedbackType.CodeReview || x.FeedbackType == (int)Common.Enumeration.FeedbackType.Weekly || x.FeedbackType == (int)Common.Enumeration.FeedbackType.Assignment || x.FeedbackType == (int)Common.Enumeration.FeedbackType.Skill || x.FeedbackType == (int)Common.Enumeration.FeedbackType.RandomReview) .OrderByDescending(x=>x.FeedbackId) .Take(5) .ToList()), WeekForFeedbackNotPresent = new List<string>(), AllAssignedCourses = new List<CourseTrackerDetails>() }); } ); return new DashboardVm { Trainees = concurrentTrainee.ToList() }; } 放在sock.write之后?                      - SeanNieuwoudt

如果sock = TCPSocket...的代码执行了10次,则for循环结束后rescue将为sock。在调用任何方法或访问对象本身的任何属性之前,您需要确保nilsock对象。 SeanNieuwoudt是对的。                      - yeyo