简体   繁体   English

如何从另一台计算机和网络访问我的 C 语言套接字服务器?

[英]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.我有一个 C 套接字,它侦听本地主机上的端口 1001。 I also have the client code that connects to port 1001 on the ip 127.0.0.1.我还有连接到 IP 127.0.0.1 上的端口 1001 的客户端代码。 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?我是否可以仅通过更改服务器代码来使我的公共 IP 对端口 1001 上的连接开放? Below is the simple server code:下面是简单的服务器代码:

obs: I learning C. obs:我在学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;
}

I have a C socket that listens on port 1001 on localhost.我有一个 C 套接字,它侦听本地主机上的端口 1001。 I also have the client code that connects to port 1001 on the ip 127.0.0.1.我还有连接到 IP 127.0.0.1 上的端口 1001 的客户端代码。 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.地址 127.0.0.1 是环回地址。 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?我是否可以仅通过更改服务器代码来使我的公共 IP 对端口 1001 上的连接开放?

Do you have a public IP?公网IP吗? 127.0.0.1 certainly isn't one, and most people with consumer-grade internet service don't have one. 127.0.0.1 当然不是一个,大多数拥有消费级互联网服务的人都没有。 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.但是假设您确实有一个公共 IP 或者您已经安排了一个公共 IP,不,您不能确保通过您的服务器程序打开一个端口。 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.此外,在设置公共服务器之前,最好检查一下 ISP 的政策和用户协议。 It is not uncommon for ISPs to forbid running outward-facing services on consumer internet connections. ISP 禁止在消费者互联网连接上运行向外服务的情况并不少见。 They typically want you to pay more for that privilege, and that also makes it easier for ISPs to police their networks.他们通常希望您为该特权支付更多费用,这也使 ISP 更容易监管他们的网络。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用C套接字API连接到我自己的计算机上的服务器? - How to use C socket API to connect to server on my own computer? 如何在C中的套接字编程中从服务器程序中调用另一个服务器程序 - How to call another server program from a server program in socket programing in c 如何区分在P2P网络中同一计算机上运行的节点(使用语言C的套接字) - How to distinguish among nodes running on a same machine in a p2p network, socket in language C 如何使用C语言访问另一台计算机上的共享文件夹 - how to access a shared folder in another machine using C language C语言的计算机精度 - Computer Precision in C language 代码可在MinGW上运行,但不能在我的uni的计算机上运行(语言C) - Code works on MinGW but not on my uni's computer (Language C) 如何以编程方式查找我的计算机的套接字等待队列大小? - How to programmatically find the socket waiting queue size of my computer? 如何使用 C 语言将目录从一个地方复制到另一个地方? - How to copy the directory from one place to another using C language? 在C语言上使用套接字创建服务器和客户端应用程序以检查文件 - on the C language using a socket to create server and client application checks for file 如何在我的计算机上获得C标准? - How to get C standard on my computer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM