绑定失败,无效参数错误

时间:2013-09-12 20:59:45

标签: c sockets binding

这是最终的代码,没有编译错误,但我仍然收到“无效参数”错误!!    ================================================== ======================

包括

using namespace std;
#include <sys/socket.h>
#include <linux/x25.h>
#include <stdio.h>

#include <sys/types.h>
#include <linux/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>



int main(int argc,char **argv,char **envp) {

    int x25_socket;
    struct sockaddr_x25 x25_address = {0};

    char x25_host[] ="10.0.0.1";
    const char name[] = "my name";

    x25_address.sx25_family = AF_X25;

    x25_socket = socket(AF_X25, SOCK_SEQPACKET, 0);
     if (x25_socket == -1)
              cout << "error in create socket" << endl;

     strncpy(x25_address.sx25_addr.x25_addr, x25_host, 16);


    int bindNumber = bind(x25_socket,(struct sockaddr *)&x25_address,sizeof(x25_address));
     if (bindNumber == -1)
     {
         perror("error in bind ---");
         cout << "error in bind" << endl;
     }

     cout << write(x25_socket, name, 2) << endl;

    return 0;
}

2 个答案:

答案 0 :(得分:1)

请改为尝试:

int main(int argc, char **argv, char **envp)
{
    struct sockaddr_x25 x25_address = {0};
    char x25_host[] = "10.0.0.1"
    const char name[] = "my name";

    int x25_socket = socket(AF_X25, SOCK_SEQPACKET, 0);
    if(x25_socket == -1)
    {
        printf(" error in create socket");
    }

    x25address.safamily = AF_X25;
    strncpy(x25_address.sx25_addr.x25_addr, x25_host, 16);

    int bindNumber = bind(x25_socket, (struct sockaddr*)&x25_address, sizeof(x25_address));
    if (bindNumber == -1)
    {
        perror("error in bind ---");
    }

    return 0;
}

答案 1 :(得分:0)

假设这是C(如@Doorknob指向(+1)),这会产生一些编译错误。请修理它们。例如,在同一变量名的两个部分之间有一个空格。因此,以下几行:

   int x25 socket;
   struct sockaddr x25 address;
   int x25 length;
   char x25 host[] 

应该是:

   int x25_socket;
   struct sockaddr_x25 x25_address;
   int x25_length;
   char x25_host[] 

除此之外,我没有看到bind()本身存在任何逻辑缺陷。