Search code examples
csocketsserversocket

How to access my socket server in C language from another computer and network?


I have a C socket that listens on port 1001 on localhost. I also have the client code that connects to port 1001 on the ip 127.0.0.1. If I send the client's code to my friend, how could he have access to my machine when we would be on different networks? Is it possible for me just by changing the server code to make my public IP open for connections on port 1001? Below is the simple server code:

obs: I learning C.

    #include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

#define BUFSIZE 256

short SocketCreate(void)
{
  short hSocket;
  printf("Create the socket\n");

  hSocket = socket(AF_INET, SOCK_STREAM, 0);
  // close(hSocket);
  return hSocket;
}

int BindCreatedSocket(int hSocket)
{
  int iRetval = -1, ClientPort = 1001;
  struct sockaddr_in remote = {0};

  remote.sin_family = AF_INET;
  remote.sin_addr.s_addr = htonl(INADDR_ANY);
  remote.sin_port = htons(ClientPort);
  iRetval = bind(hSocket, (struct sockaddr *)&remote, sizeof(remote));

  return iRetval;
}

int main(int argc, char *argv[])
{
  int socket_desc, sock, clientLen;
  struct sockaddr_in client;
  char client_message[200] = {0}, message[9999] = {0};

  char buf[BUFSIZE];

  socket_desc = SocketCreate();
  if (socket_desc == -1)
  {
    printf("Could not create socket");
    return 1;
  }
  printf("Socket created\n");

  if (BindCreatedSocket(socket_desc) < 0)
  {
    perror("bind failed.");
    return 1;
  }

  printf("Waiting for incoming connections...\n");

  listen(socket_desc, 3);

  while (1)
  {
    clientLen = sizeof(struct sockaddr_in);

    sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t *)&clientLen);
    if (sock < 0)
    {
      perror("accept failed");
      return 1;
    }
    // printf("Connection accepted\n");

    memset(client_message, '\0', sizeof(client_message));
    memset(message, '\0', sizeof(message));

    if (recv(sock, client_message, 200, 0) < 0)
    {
      printf("recv failed");
      break;
    }

    if (strcmp(client_message, "exitserver") == 0)
    {
      close(socket_desc);
      close(sock);
      break;
    }
  }
  return 0;
}

Solution

  • I have a C socket that listens on port 1001 on localhost. I also have the client code that connects to port 1001 on the ip 127.0.0.1. If I send the client's code to my friend, how could he have access to my machine when we would be on different networks?

    They couldn't. Address 127.0.0.1 is a loopback address. Packets sent to that address are always directed to the machine that sent them.

    Is it possible for me just by changing the server code to make my public IP open for connections on port 1001?

    Do you have a public IP? 127.0.0.1 certainly isn't one, and most people with consumer-grade internet service don't have one. If you did have one, you probably would have had to make special arrangements to get it, and you would probably be paying extra for the privilege.

    But supposing that you did have a public IP or that you made arrangements to get one, no, you cannot ensure that a port is open via your server program. Your program can listen on that address without much fuss, but you have to consider also firewalls -- probably one on your local machine and one at your local router, at least.

    Also, before you set up a public server, you would be wise to check your ISP's policy and user agreement. It is not uncommon for ISPs to forbid running outward-facing services on consumer internet connections. They typically want you to pay more for that privilege, and that also makes it easier for ISPs to police their networks.