download2014711 2017-09-12 03:08
浏览 35
已采纳

从外界连接到我自己的golang服务器[关闭]

So I do have this code written in go:

// firstserver - my first server
package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", Handler)
    err := http.ListenAndServe(":80", nil)
    if err != nil {
        log.Fatal(err)
    }
}

// Handler - a handler function for a normal enter
func Handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "You're connected")
}

Now, when I go to my browser and type localhost:80 (or just localhost), the page loads, and I see the message You're connected. Now I wanted to try to connect from other devices too. So I took my other laptop. I checked my adapter address, its 192.168.0.15. So when I typed 192.168.0.15:80 on the other laptop, it worked again, cool!

Primarly I am trying to do some simple server to use it on my phone when I am at college, check the schedules, have some notes, etc. I took my smartphone, I disconnected from my WiFi, switched to my smartphones internet and dumbly typed 192.168.0.15:80 again. I knew it wouldn't work, cuz it's not my external IP. And it didn't.

So I looked up the IP address that my ISP provider gives me, it looks like this: 88.156.xxx.xx. Now on my phone I typed 88.156.xxx.xx:80 and it didn't work. I tried to search SO and other resources on the web on how to connect remotely to my server from outside of LAN. And I didn't find an answer. Is there a way to do that without, I don't know, paying for sorts of things, or something like this?

P.S I'm living in a rented room, there's not only me in the house, so I don't have a way to configure a router (manually or over the web)

  • 写回答

1条回答 默认 最新

  • drqvsx1228 2017-09-17 20:21
    关注

    One way you can do it is deploy it on an AWS Free Tier instance with public IP(https://aws.amazon.com/free/). Once you are done spinning up the EC2 instance (https://aws.amazon.com/free/), copy the binary of your web server to that instance scp -i mykey.pem somefile.txt root@my.ec2.id.amazonaws.com:/ You need to ensure that the security group on that instance allows ingress from the internet(0.0.0.0/0) on that port

    To access it: Use either the public IP or public DNS of the EC2 instance along with the port number of your server(in your case 80)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么