linux c - get server hostname?

前端 未结 3 1881
眼角桃花
眼角桃花 2021-02-14 18:09

Does anyone know a function to get the hostname of the linux server? I don\'t really want to have to include any headers or compile other libraries, hoping there is a function b

3条回答
  •  醉酒成梦
    2021-02-14 18:55

    Building on the answer from Alain Pannetier, you can spare a few bytes by using HOST_NAME_MAX:

    #include 
    ...
      char hostname[HOST_NAME_MAX+1];
      gethostname(hostname, HOST_NAME_MAX+1);
    ...
    

提交回复
热议问题