绑定到特定的IP C语言为Ubuntu的 [英] Bind to a specific IP in C for ubuntu

查看:160
本文介绍了绑定到特定的IP C语言为Ubuntu的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想提出一个简单的服务器,在一个IP地址需要从getaddrinfo()与结合到它。使用ifconfig,我发现,我为wlan0 192.168.2.10的IP地址,我想结合。不幸的是,我的地址似乎是结合是我的卤味设备。因为当我初始化的getaddrinfo某种原因(192.168.2.10,3490,&安培; hings,&安培; RES);资源获取返回为NULL指针。我将展示我的code波纹管。

Hi I am trying to make a simple server that takes in an IP address from getaddrinfo() and binds to it. Using ifconfig, I've found that I have an ip address of wlan0 192.168.2.10 which I would like to bind to. Unfortunately the address I seem to be binding to is my lo device. For some reason when I initialize getaddrinfo("192.168.2.10","3490",&hings,&res); res gets returned to a NULL pointer. I will show off my code bellow.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>

#define MAXDATASIZE 500;

int main(int argc, char *argv[]){
    // dealing with client socket
    struct sockaddr_storage their_addr;
    socklen_t addr_size;
    // server socket
    struct addrinfo serverSide,*serverInfo,*sortIP;
    int optValRet;
    int listenSock, newSock;
    // this is for reading in information
    char buf[501];
    char point[INET6_ADDRSTRLEN];
    char compare[INET6_ADDRSTRLEN] = "192.168.2.10";
    // this is for handeling child processes and signals
    struct sigaction sa;
    sa.sa_handler = NULL;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    if(sigaction(SIGCHLD, &sa, NULL) == -1){
        printf("We have a problem, sigaction is not working.\n");
        perror("\n");
        exit(1);    
    }   
    // this sets up addrinfo
    memset(&serverSide, 0, sizeof serverSide);
    serverSide.ai_family = AF_UNSPEC;
    serverSide.ai_socktype = SOCK_STREAM;
    serverSide.ai_flags = INADDR_ANY;
    // set up the address
    if(getaddrinfo("192.168.2.10","3490",&serverSide,&serverInfo)!=0){
        printf("get addr not success\n");
        perror("\n");
        return 1;
    }

    printf("Got address lists\n");

    for(sortIP = serverInfo; sortIP = sortIP->ai_next; sortIP != NULL){

        if((listenSock = socket(sortIP->ai_family, sortIP->ai_socktype, sortIP->ai_protocol))==-1){
            continue;
        }
        if(setsockopt(listenSock,SOL_SOCKET,SO_REUSEADDR,&optValRet,sizeof(int))==-1){
            perror("\n");
            exit(1);
        }
        if(bind(listenSock,sortIP->ai_addr,sortIP->ai_addrlen) == -1 ){
            perror("\n");
            close(listenSock);
            continue;       
        }
        break;
    }
    if(sortIP == NULL){printf("sort ip is null.");}
    inet_ntop(sortIP->ai_family,sortIP->ai_addr,point,sizeof point);
    printf("Tell the clients connect to ip address %s on port 3490\n",point);
    listen(listenSock, 10);
    addr_size = sizeof their_addr;
    newSock = accept(listenSock,(struct sockaddr *)&their_addr,&addr_size);
    recv(newSock, buf, 500, 0);
    printf("%s\n",buf);
    close(listenSock);
    close(newSock);
    freeaddrinfo(serverInfo);
    return 0;
}

现在我有一个事实,即我返回null之外的其他问题。由于WiFi路由器已指派我的IP地址192.168.2.10为我的子网,我怎么找到我的IP地址是什么,如果我是外网,并试图联系我的服务器?我假设内部网络的IP是从外网ip不同......我错了?反正那些都是我的两个问题。

Now I have some other questions beside the fact that I'm returning null. Since the wifi router has assigned me the ip address 192.168.2.10 for my subnet, how do I find out what my ip address is if I'm outside the network and trying to contact my server? I'm assuming the inside network ip is different from the outside network ip ... am I wrong? Anyways those are my two questions.

感谢您的帮助!

推荐答案

这是错误的,是你的直接问题:

This is wrong and is your immediate problem:

for (sortIP = serverInfo; sortIP = sortIP->ai_next; sortIP != NULL)

您想是这样的:

for (sortIP = serverInfo; sortIP != NULL; sortIP = sortIP->ai_next)

但我会去与while循环往心里去。

but I would go with a while loop personally.

这篇关于绑定到特定的IP C语言为Ubuntu的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆