gethostname linux函数
时间: 2024-02-18 19:15:07 浏览: 221
`gethostname` 是一个 Linux 系统调用函数,它用于获取本地主机的名称。该函数的原型如下:
```c
#include <unistd.h>
int gethostname(char *name, size_t len);
```
其中,`name` 参数是一个指向字符数组的指针,用于存放获取到的主机名;`len` 参数则是 `name` 所指向的字符数组的长度。
该函数会将获取到的主机名写入到 `name` 所指向的字符数组中,并返回 0 表示成功,否则返回 -1 并设置相应的错误码。
相关问题
linux 使用标准IO函数实现图片本地下载
可以使用标准IO函数来实现图片本地下载。以下是一个示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#define MAXLINE 1024
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "usage: %s <url>\n", argv[0]);
exit(1);
}
char *url = argv[1];
char *filename = strrchr(url, '/') + 1;
char *hostname = url + strlen("http://");
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("socket error");
exit(1);
}
struct hostent *he = gethostbyname(hostname);
if (he == NULL) {
perror("gethostbyname error");
exit(1);
}
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr = *((struct in_addr *)he->h_addr);
if (connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect error");
exit(1);
}
char request[MAXLINE];
snprintf(request, MAXLINE, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", filename, hostname);
if (write(sockfd, request, strlen(request)) < 0) {
perror("write error");
exit(1);
}
char response[MAXLINE];
int nread;
while ((nread = read(sockfd, response, MAXLINE)) > 0) {
if (strstr(response, "\r\n\r\n") != NULL) {
break;
}
}
char *image_data = strstr(response, "\r\n\r\n") + 4;
int image_size = nread - (image_data - response);
int fd = open(filename, O_CREAT | O_WRONLY, 0644);
if (fd < 0) {
perror("open error");
exit(1);
}
if (write(fd, image_data, image_size) < 0) {
perror("write error");
exit(1);
}
close(sockfd);
close(fd);
return 0;
}
```
这个程序使用 `socket` 函数创建一个 TCP 套接字,使用 `gethostbyname` 函数获取主机名对应的 IP 地址,使用 `connect` 函数连接到主机的 HTTP 端口,构造 HTTP 请求报文,使用 `write` 函数向主机发送请求报文,使用 `read` 函数从主机接收响应报文,从响应报文中提取图片数据,使用标准IO函数将图片数据写入本地文件。
linux ping源码
以下是Linux系统中的ping命令的源代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd, datalen = 56;
int nsend = 0, nreceived = 0;
struct sockaddr_in dest_addr;
pid_t pid;
struct timeval tvrecv;
void statistics(int signo);
unsigned short cal_chksum(unsigned short *addr, int len);
int pack(int pack_no);
void send_packet();
void recv_packet();
void tv_sub(struct timeval *out, struct timeval *in);
void sig_int(int signo);
int main(int argc, char *argv[])
{
struct hostent *host;
struct protoent *protocol;
unsigned long inaddr = 0l;
int size = 50 * 1024;
if (argc < 2)
{
printf("usage: %s hostname/IP address\n", argv[0]);
exit(1);
}
if ((protocol = getprotobyname("icmp")) == NULL)
{
perror("getprotobyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
{
perror("socket error");
exit(1);
}
setuid(getuid());
setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size));
bzero(&dest_addr, sizeof(dest_addr));
dest_addr.sin_family = AF_INET;
if ((inaddr = inet_addr(argv[1])) == INADDR_NONE)
{
if ((host = gethostbyname(argv[1])) == NULL)
{
perror("gethostbyname error");
exit(1);
}
memcpy((char *)&dest_addr.sin_addr, host->h_addr, host->h_length);
}
else
{
dest_addr.sin_addr.s_addr = inaddr;
}
pid = getpid();
signal(SIGALRM, statistics);
signal(SIGINT, sig_int);
printf("PING %s (%s): %d data bytes\n", argv[1], inet_ntoa(dest_addr.sin_addr), datalen);
send_packet();
recv_packet();
return 0;
}
void statistics(int signo)
{
printf("\n%d packets transmitted, %d received, %%%d lost\n",
nsend, nreceived, (nsend - nreceived) / nsend * 100);
close(sockfd);
exit(0);
}
unsigned short cal_chksum(unsigned short *addr, int len)
{
int nleft = len;
int sum = 0;
unsigned short *w = addr;
unsigned short answer = 0;
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
if (nleft == 1)
{
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
answer = ~sum;
return answer;
}
int pack(int pack_no)
{
int packsize;
struct icmp *icmp;
struct timeval *tval;
icmp = (struct icmp *)sendpacket;
icmp->icmp_type = ICMP_ECHO;
icmp->icmp_code = 0;
icmp->icmp_cksum = 0;
icmp->icmp_seq = pack_no;
icmp->icmp_id = pid;
packsize = 8 + datalen;
tval = (struct timeval *)icmp->icmp_data;
gettimeofday(tval, NULL);
icmp->icmp_cksum = cal_chksum((unsigned short *)icmp, packsize);
return packsize;
}
void send_packet()
{
int packetsize;
while (nsend < MAX_NO_PACKETS)
{
nsend++;
packetsize = pack(nsend);
if (sendto(sockfd, sendpacket, packetsize, 0,
(struct sockaddr *)&dest_addr, sizeof(dest_addr)) < 0)
{
perror("sendto error");
continue;
}
sleep(1);
}
}
void recv_packet()
{
int n, fromlen;
extern int errno;
struct sockaddr_in from;
fromlen = sizeof(from);
while (nreceived < nsend)
{
alarm(MAX_WAIT_TIME);
if ((n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0,
(struct sockaddr *)&from, &fromlen)) < 0)
{
if (errno == EINTR)
{
continue;
}
perror("recvfrom error");
continue;
}
gettimeofday(&tvrecv, NULL);
recv_packet();
}
}
void tv_sub(struct timeval *out, struct timeval *in)
{
if ((out->tv_usec -= in->tv_usec) < 0)
{
--out->tv_sec;
out->tv_usec += 1000000;
}
out->tv_sec -= in->tv_sec;
}
void sig_int(int signo)
{
statistics(signo);
}
```
这是一个简单的程序,使用了原始套接字来发送和接收 ICMP 报文,实现了 ping 命令的功能。它通过发送 ICMP ECHO 请求报文,然后等待接收 ICMP ECHO 应答报文来判断目标主机的状态。其中,变量 nsend 表示已发送的报文数量,变量 nreceived 表示已接收的报文数量,变量 sockfd 表示套接字描述符,变量 dest_addr 表示目标主机的地址信息,变量 datalen 表示 ICMP 数据负载的长度,变量 pid 表示进程 ID。函数 pack 是用来构造 ICMP 报文,函数 send_packet 是用来发送报文,函数 recv_packet 是用来接收报文,函数 tv_sub 是用来计算时间差的。当用户按下 Ctrl+C 组合键时,程序会调用函数 sig_int 来输出统计信息并退出。
阅读全文