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 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题