小编Hug*_*ugo的帖子

套接字 sendto() 返回 EINVAL

我正在尝试在 C 中发送一个 UDP 数据包。我有以下内容sendto()

char* msg = "Hello";

//ret is the return value of getaddrinfo, the address is AF_INET (IPv4)
//and the sock_type is SOCK_DGRAM (UDP)
struct sockaddr_in *ip = (struct sockaddr_in *)ret->ai_addr;

if ((sendto(sock, msg, strlen(msg), 0, (struct sockaddr *)ip, 
                sizeof(struct sockaddr *))) != -1) {
    printf("msg sent successfully");
} else {
    printf("Error sending msg: %s\n", strerror(errno));
}
Run Code Online (Sandbox Code Playgroud)

但是,它返回一个错误,指出存在无效参数。查看联机帮助页,我无法确定哪个是无效参数。有任何想法吗?

编辑:这是我所有的代码

#include <stdio.h>  
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h> …
Run Code Online (Sandbox Code Playgroud)

c sockets udp ipv4 sendto

2
推荐指数
1
解决办法
7197
查看次数

rspec清理数据库在运行之前

我在Sinatra项目中使用RSpec.我正在测试一个运行after_find钩子的模型来初始化一些值.我需要在每次运行之前擦除数据库,经过一些搜索后,我的规范帮助文件如下所示:

require "pry"
require "factory_girl_rails"
require "rspec-rails"

FactoryGirl.find_definitions

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  config.use_transactional_fixtures = true
end
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

undefined method `use_transactional_fixtures=' for #<RSpec::Core::Configuration:0x007fc1c89397e8> (NoMethodError)`
Run Code Online (Sandbox Code Playgroud)

我查阅了RSpec :: Core :: Configuration的文档,唯一提到的use_transactional_fixtures=是它特别指出它是特定于rails的.我安装了'rspec-rails'宝石,所以我不知道它为什么不使用它.我甚至在spec帮助文件中要求它.

我最初的想法是创建一个运行db:drop,db:create和db:migrate然后测试的Rakefile,但我希望有一个稍微简单的方法 use_transactional_fixtures=

ruby activerecord rspec sinatra

2
推荐指数
1
解决办法
5745
查看次数

C套接字发送UDP并处理来自路由器的ICMP答复

我正在尝试将UDP数据包发送到生存时间为1的路由器,然后收到ICMP时间超限答复。到目前为止,我已经能够发送数据包,但是当我的程序进入执行的recv部分时,它只是挂起。我对recvfrom进行了错误检查,但甚至没有做到这一点。我的计算机正在接收请求。我知道这是因为我在运行程序时运行Wireshark,并为ICMP请求筛选。每次我运行程序时,都会收到答复。recvfrom我在做什么错?

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <errno.h>

#define UNSPEC_PROTO 0

int main(int argc, const char *argv[])
{ 
if (argc != 2) {
    printf("usage: routetracer <ip address or hostname>\n");
    return -1;
}

struct addrinfo hints; //params for ret val of getaddrinfo
struct addrinfo* ret; //return value of getaddrinfo
struct sockaddr* reply_addr;
char ipv4[INET_ADDRSTRLEN];
char* msg = "THE PORT IS OVER 9000!!!!";
int status = 0;
int ttl = 0;
int src_sock …
Run Code Online (Sandbox Code Playgroud)

c sockets icmp ttl recvfrom

1
推荐指数
1
解决办法
4119
查看次数

C++.用错误的构造函数实例化对象?

我得到一个奇怪的错误,关于我甚至没有调用的函数没有匹配函数.这是错误:

BinarySearchTree.h:18:45: error: no matching function for call to ‘BinaryTree<int>::BinaryTree()’ BinarySearchTree<T>::BinarySearchTree(T elem)

显然它没有找到那个功能,因为我没有它.我没有它,因为实例化一个完全空的二叉树(或者至少对我来说似乎是这样)是没有意义的.我也意识到有些库中有这些数据结构.我只是这样做是为了帮助学习我的数据结构类中的材料以及了解更多关于C++的知识,直到最近我才将其用作"C with classes".我不明白为什么编译器要求我提供该构造函数,如果我不想在那里.

这是源文件.我没有使用默认构造函数实例化树,这让我非常困惑.此外,preorder方法在基类中完美地工作,因此它应该与派生类一起使用(我只是使派生类添加特定于二叉搜索树的方法,但它仍然是二叉树).

#include "../headers/BinarySearchTree.h"
#include <cstdio>

void print_int_node(BTreeNode<int>* tgt)
{
    printf("%d, ", tgt->getElement());
}

int main(int argc, char* argv[])
{
    BinarySearchTree<int> myTree(5);

    printf("Pre-order: \n");
    myTree.preorder(myTree.root, print_int_node);
    printf("\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的类:

BinarySearchTree.h:

#ifndef _BINARY_SEARCH_TREE_H_
#define _BINARY_SEARCH_TREE_H_

#include "BinaryTree.h"

template <typename T>
class BinarySearchTree : public BinaryTree<T> {
    void _add(BTreeNode<T>*, T elem);

    public:
        BinarySearchTree(T);

        void add(T);
};

template <typename T>
BinarySearchTree<T>::BinarySearchTree(T elem)
{
    this->root = new BTreeNode<T>(elem); …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance constructor class

-1
推荐指数
1
解决办法
112
查看次数

标签 统计

c ×2

sockets ×2

activerecord ×1

c++ ×1

class ×1

constructor ×1

icmp ×1

inheritance ×1

ipv4 ×1

recvfrom ×1

rspec ×1

ruby ×1

sendto ×1

sinatra ×1

ttl ×1

udp ×1