未定义对“ socketpair”的引用

时间:2019-07-31 09:34:42

标签: c++ client-server qnx

我编写了套接字客户端代码,可以在Linux GCC上正常运行,但是在QNX平台上却出错。由于错误,它作为对

编译命令:aarch64-unknown-nto-qnx7.0.0-gcc ss1.c

Buid错误

/tmp/cc8ssnim.o: In function `main':
ss1.c:(.text+0x34): undefined reference to `socketpair'
collect2: error: ld returned 1 exit status

在约翰评论后编辑

通过添加-lsocket修复了同时间错误,我将运行时错误作为

setsockopt:协议不可用

示例代码取自HERE

// Server side C/C++ program to demonstrate Socket programming
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include<iostream>
#include <sys/types.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
    int server_fd, new_socket, valread;
    struct sockaddr_in address;
    int opt = 1;
    int addrlen = sizeof(address);
    char buffer[1024] = {0};
    char hello[500] = {};

    // Creating socket file descriptor
    if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
    {
        perror("socket failed");
        exit(EXIT_FAILURE);
    }

    // Forcefully attaching socket to the port 8080
    if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
                &opt, sizeof(opt)))
    {
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons( PORT );

    // Forcefully attaching socket to the port 8080
    if (bind(server_fd, (struct sockaddr *)&address,
                sizeof(address))<0)
    {
        perror("bind failed");
        exit(EXIT_FAILURE);
    }
    if (listen(server_fd, 3) < 0)
    {
        perror("listen");
        exit(EXIT_FAILURE);
    }
    if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
                    (socklen_t*)&addrlen))<0)
    {
        perror("accept");
        exit(EXIT_FAILURE);
    }
    ab:
    std::cout<<"Enter send to client\n";
    std::cin>>hello;
    valread = read( new_socket , buffer, 1024);
    printf("Received in server = %s\n",buffer );
        memset( buffer, '\0', sizeof(char)*1024 );

    send(new_socket , hello , strlen(hello) , 0 );
    memset( hello, '\0', sizeof(char)*500 );
    goto ab;
    //printf("Hello message sent\n");
    return 0;
}

0 个答案:

没有答案