Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Essentially I have a server that will be listening for sockets in other programs seeking a connection:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <linux/netdevice.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define MAX_LEN 80   

int main(int argc, char *argv[]){
  int recvSocket, serverSocket;
  char buffer[1024];
  struct sockaddr_in serverAddr;
  struct sockaddr_storage serverStorage;
  socklen_t addr_size;
  char user[MAX_LEN];
  char pass[MAX_LEN];
  char name[MAX_LEN];
  char port[MAX_LEN];
  struct iovec iov[2];

  struct ifconf ifconf;
  struct ifreq ifr[50];

  if (2 != argc) {
           fprintf(stderr, "Usage: %s <port>\n", argv[0]);
               exit(1);
    }

  /*---- Create the socket. The three arguments are: ----*/
  /* 1) Internet domain 2) Stream socket 3) Default protocol (TCP in this case) */
  recvSocket = socket(PF_INET, SOCK_STREAM, 0);

  ifconf.ifc_buf = (char *) ifr;
  ifconf.ifc_len = sizeof ifr;

  if (ioctl(recvSocket, SIOCGIFCONF, &ifconf) == -1) {
    perror("ioctl");
    return 0;
  }

  char ip[INET_ADDRSTRLEN];
  struct sockaddr_in *s_in = (struct sockaddr_in *) &ifr[1].ifr_addr;

  if (!inet_ntop(AF_INET, &s_in->sin_addr, ip, sizeof(ip))) {
    perror("inet_ntop");
    return 0;
  }

  /*---- Configure settings of the server address struct ----*/
  /* Address family = Internet */
  serverAddr.sin_family = AF_INET;
  /* Set port number, using htons function to use proper byte order */
  serverAddr.sin_port = htons(atoi(argv[1]));
  /* Set IP address to localhost */
  serverAddr.sin_addr.s_addr = inet_addr(ip);
  /* Set all bits of the padding field to 0 */
  memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);  

  /*---- Bind the address struct to the socket ----*/
  bind(recvSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

  if(listen(recvSocket,5)==0)
    printf("Listening\n");
  else
    printf("Error\n");

  /*---- Accept call creates a new socket for the incoming connection ----*/
  addr_size = sizeof serverStorage;
  serverSocket = accept(recvSocket, (struct sockaddr *) &serverStorage, &addr_size);

Here, I am trying to get the IP of the computer in order to set it up the socket on this program. Later on, a client will be connecting to it, where it will be given the same address by the user (so no worries on that side).

It seems to work just fine, but I get the feeling I am doing extra steps that could be simplified. Does anyone have any idea if it could be so? I guided myself from this question and this article to create mine.

share|improve this question

migrated from stackoverflow.com Oct 31 '15 at 19:02

This question came from our site for professional and enthusiast programmers.

INADDR_ANY

Instead of looking up the host's IP address, and then using it to bind the socket, you could use INADDR_ANY like this:

serverAddr.sin_addr.s_addr = INADDR_ANY;
share|improve this answer
    
right, but in my requirements it specifies that the client will be looking to connect its socket with the host's server IP address. So it's either use their specific IPv4 address or 127.0.0.1, so in the worst of cases I am suggested to use INADDR_LOOPBACK – Yokhen Oct 31 '15 at 20:12
    
@Yokhen How does the client affect the server? I thought you said the user would give the ip address to the client? – JS1 Oct 31 '15 at 22:35
    
It doesn't affect it. However if the server socket isn't started with the IPv4 address, then how can the client connect to it with the same one? Sure, I can set it up as INADDR_ANY like you said. But in the requirements I am told to have the ip address be the IPv4 address (or as they call it network interface address) or 127.0.0.1(which is easier but lazy and my last resort) as the socket ip address. They don't mention INADDR_ANY or 0.0.0.0. Why? I have no clue. – Yokhen Oct 31 '15 at 22:45
    
INADDR_ANY should bind your socket to all of your local interfaces, so you should be able to connect with your client no matter whether the client is running on a different machine or the same machine. It's not more correct than specifying the ip address manually, but you asked if you were doing any extra steps. Have you tried using INADDR_ANY, and did it not work for you? – JS1 Nov 1 '15 at 0:13
    
Yes it worked, however it's not what I am looking for atm. – Yokhen Nov 1 '15 at 0:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.