dougekui1518 2016-11-19 19:39
浏览 217

如何设置仅适用于我的局域网的golang服务器监听器

I am using iris platform to do programming in go language (I am very beginner). I want to make all my computers at home be available for accessing to go server which is one of the local machine as well. When I set the listener with my network IP (192.168.0.0) or with any specified IP (192.168.0.15) it gives me panic error back. Only available 0.0.0.0 or 127.0.0.1/localhost or 192.168.0.19 - that is same as localhost

import "net"

...

ln, err := net.Listen("tcp4", "192.168.12:9999")
    if err != nil{
        panic(err)
    }

    iris.Serve(ln)

...

The error is: panic: listen tcp4 192.168.0.12:9999: bind: The requested address is not valid in its context.

Thanks to all for help.

  • 写回答

1条回答 默认 最新

  • donglu8549 2016-11-20 01:17
    关注

    You should read up on how networking works but here are some points to get you started.

    You listen on a port, not an IP address. So whatever machine you're using as your server, find your local IP address:

    On Linux or Mac you can do it many ways:

    ifconfig | grep netmask and get your local address eg. 192.168.x.xx

    Then start up your server with your Go program and listen on a localhost port like 8080.

    eg.

    if err := http.ListenAndServe(":8080", nil); err != nil {
        //handle error
    }
    

    Then you can access the server with other machines in your house assuming they are on the same Wifi. Use a different computer and visit 192.168.x.xx:8080 from a browser.

    To answer your question in your comments, outsiders cannot access your local server unless they have a connection to your Wifi.

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算