Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7419331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:03:04+00:00 2026-05-29T08:03:04+00:00

Hi I am trying to make a simple server that takes in an IP

  • 0

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;
}

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.

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:03:04+00:00Added an answer on May 29, 2026 at 8:03 am

    This is wrong and is your immediate problem:

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

    You want something like:

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

    but I would go with a while loop personally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a simple HTML server that will read a request from
I'm trying to make a simple chat server in Java. Now I have a
I'm trying to make a simple C# web server that, at this stage, you
I'm trying to make simple http server, that can be pause and resume,, I've
I'm trying for a week to make my XSP server work with simple static
Trying to make simple minesweeper game in python, but have one problem. I have
I'm trying to make simple script using jquery $post function to pass data to
I'm trying to make a simple php script to find all src attributes from
I am trying to make a simple Client-Server application but when I execute the
I have made up a simple set of web services (trying to make a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.