简体   繁体   English

通过 Internet 的 TCP 套接字连接

[英]TCP socket connection over internet

I am doing a project for which connection between server and client is required.我正在做一个需要服务器和客户端之间连接的项目。 I did it by adding TCP sockets.我是通过添加 TCP 套接字来实现的。

Here is the code fraction : Server:这是代码部分:服务器:

    ServerSocket welcomeSocket = new ServerSocket(80);
    while(true)
    {
        Socket connectionSocket = welcomeSocket.accept();
        WorkerThread wt = new WorkerThread(connectionSocket, id);
        Thread t = new Thread(wt);
        t.start();
        workerThreadCount++;

    }

Client :客户 :

        Socket skt = new Socket("192.168.0.108", 80); // The IP address is from cmd->ipconfig/all-> IPv4 Address
        outToServer = new PrintWriter(skt.getOutputStream(), true);
        inFromServer = new BufferedReader(new InputStreamReader(skt.getInputStream()));

It all works when both ends are in same device/under same WiFi.But I don't understand what to do for creating connection over internet.当两端都在同一设备中/在同一个 WiFi 下时,这一切都有效。但我不明白如何通过互联网创建连接。

Please help with clear steps.请帮助提供明确的步骤。

Here:这里:

Socket skt = new Socket("192.168.0.108", 80);

That is local address.那是本地地址。 If you want to have a server that is reachable on the internet, then that server needs to have its global public IP address!如果您想拥有一台可在 Internet 上访问的服务器,那么该服务器需要拥有其全球公共 IP 地址!

In other words: you have to make sure that the server can be reached from the internet somehow.换句话说:您必须确保可以通过 Internet 以某种方式访问​​服务器。 For example by turning to some service provider that hosts servers that you can then equip with your code!例如,通过转向一些托管服务器的服务提供商,然后您可以配备您的代码!

The whole purpose of 192.168 addresses is to be defined only in a local subnet. 192.168 地址的全部目的是仅在本地子网中定义。

Alternatively, you have to check if your ISP has a service where the ISP assigns an IP address to your connection, and that allows calls from the internet to go to your "place".或者,您必须检查您的 ISP 是否有一项服务,即 ISP 为您的连接分配一个 IP 地址,并允许来自 Internet 的呼叫转到您的“位置”。

Meaning: when you want to receive phone calls, you need a phone that is connected to the phone net!意思是:当您要接听电话时,您需要一部已连接电话网的电话!

In order to connect to a socket over WAN, you must port forward that port to your local device.为了通过 WAN 连接到套接字,您必须将该端口转发到本地设备。 This can be done in your routers' settings.这可以在路由器的设置中完成。

192.168.0.108 --> That's your local IP-address. 192.168.0.108 --> 那是你的本地 IP 地址。

This can be used on your local network without any requirements for port forwarding whatsoever.这可以在您的本地网络上使用,而无需任何端口转发要求。 However, to use it over WAN, execute the following steps:但是,要通过 WAN 使用它,请执行以下步骤:

Step 1: Search for your routers' model number and port forwarding on Google on how-to forward port 80 to your local IP-address.第 1 步:在 Google 上搜索路由器的型号和端口转发,了解如何将端口 80 转发到您的本地 IP 地址。 Warning: use a static IP-address on your local device to prevent your IP from changing after a reboot.警告:在您的本地设备上使用静态 IP 地址以防止您的 IP 在重新启动后更改。

Step 2: Go to a website like IP Chicken and find your external IP-address.第 2 步:访问IP Chicken等网站并找到您的外部 IP 地址。

You can then connect to your socket using:然后,您可以使用以下方法连接到您的套接字:

Socket skt = new Socket("[EXTERNALIP]", 80);

Please be noticed: unless you have a business network, your external IP-address will probably change from time to time.请注意:除非您有业务网络,否则您的外部 IP 地址可能会不时更改。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM