给定一个TCP服务器,如何获取连接域地址 [英] Given a TCP server, how to get the connection domain address

查看:50
本文介绍了给定一个TCP服务器,如何获取连接域地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的TCP服务器,当客户端连接时,我想获取用于连接的域名:

I have a simple TCP server and, when a client connects, I want to get the domain address used to connect:

package main

import (
    "fmt"
    "net"
    "os"
)

const (
    CONN_HOST = "localhost"
    CONN_PORT = "3333"
    CONN_TYPE = "tcp"
)

func main() {
    // Listen for incoming connections.
    l, err := net.Listen(CONN_TYPE, CONN_HOST+":"+CONN_PORT)
    if err != nil {
        fmt.Println("Error listening:", err.Error())
        os.Exit(1)
    }
    // Close the listener when the application closes.
    defer l.Close()
    fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
    for {
        // Listen for an incoming connection.
        conn, err := l.Accept()
        if err != nil {
            fmt.Println("Error accepting: ", err.Error())
            os.Exit(1)
        }
        // Handle connections in a new goroutine.
        go handleRequest(conn)
    }
}

// Handles incoming requests.
func handleRequest(conn net.Conn) {
    // Make a buffer to hold incoming data.
    buf := make([]byte, 1024)
    // Read the incoming connection into the buffer.
    _, err := conn.Read(buf)
    if err != nil {
        fmt.Println("Error reading:", err.Error())
    }
    // Send a response back to person contacting us.
    conn.Write([]byte("Message received."))
    // Close the connection when you're done with it.
    conn.Close()
}

我尝试调试 conn net.Conn 参数,但是找不到对域地址的任何引用.尝试使用 http://test.127.0.0.1.xip.io:3333/,我对以某种方式获取 test.127.0.0.1.xip.io 感兴趣.有什么想法吗?

I tried debugging the conn net.Conn param but I can't find any reference to the domain address. Tried with http://test.127.0.0.1.xip.io:3333/ and I'm interested in getting test.127.0.0.1.xip.io somehow. Any ideas?

推荐答案

纯TCP无法实现您要尝试的操作.TCP在没有域的纯IP地址上工作.

What you are trying to do is not possible with plain TCP. TCP works on plain IP-Addresses without domains.

解释发生了什么事:

建立连接时,例如 example.com ,首先完成 example.com 的DNS查找.在这种情况下,DNS查找将产生 93.184.216.34 .您可以在此处了解更多信息.

When you are establishing a connection to, e.g. example.com, first of all a DNS Lookup for example.com is done. In this case, the DNS Lookup would result in 93.184.216.34. You can read more about DNS here.

此后,建立具有 93.184.216.34 的TCP连接,原始域名不会随请求一起发送.

A TCP Connection with 93.184.216.34 is established after that, the original domain name is not sent with the request.

由于有时您需要用户尝试连接的原始名称,因此某些协议在连接后会发送域名.例如,HTTP通过 Host 标头.

Because you sometimes need the original name the user was trying to connect to, some protocols send the domain name after connecting. HTTP for example does this via the Host header.

也许您可以执行类似的操作,并要求首先通过TCP连接发送原始主机!

Maybe you can do something like that and require to send the original host first through your TCP Connection!

这篇关于给定一个TCP服务器,如何获取连接域地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆