#include <stdio.h> /* standard C i/o facilities */
#include <stdlib.h> /* needed for atoi() */
#include <unistd.h> /* defines STDIN_FILENO, system calls,etc */
#include <sys/types.h> /* system data type definitions */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h> /* gethostbyname */
/* this routine echos any messages (UDP datagrams) received */
#define MAXBUF 1024*1024
void echo( int sd ) {
int len,n;
char bufin[MAXBUF];
struct sockaddr_in remote;
/* need to know how big address struct is, len must be set before the
call to recvfrom!!! */
len = sizeof(remote);
while (1) {
/* read a datagram from the socket (put result in bufin) */
n=recvfrom(sd,bufin,MAXBUF,0,(struct sockaddr *)&remote,&len);
/* print out the address of the sender */
printf("Got a datagram from %s port %d\n",
inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
if (n<0) {
perror("Error receiving data");
} else {
printf("GOT %d BYTES\n",n);
/* Got something, just send it back */
sendto(sd,bufin,n,0,(struct sockaddr *)&remote,len);
}
}
}
/* server main routine */
int main() {
int ld;
struct sockaddr_in skaddr;
int length;
/* create a socket
IP protocol family (PF_INET)
UDP protocol (SOCK_DGRAM)
*/
if ((ld = socket( PF_INET, SOCK_DGRAM, 0 )) < 0) {
printf("Problem creating socket\n");
exit(1);
}
/* establish our address
address family is AF_INET
our IP address is INADDR_ANY (any of our IP addresses)
the port number is assigned by the kernel
*/
skaddr.sin_family = AF_INET;
skaddr.sin_addr.s_addr = htonl(INADDR_ANY);
skaddr.sin_port = htons(0);
if (bind(ld, (struct sockaddr *) &skaddr, sizeof(skaddr))<0) {
printf("Problem binding\n");
exit(0);
}
/* find out what port we were assigned and print it out */
length = sizeof( skaddr );
if (getsockname(ld, (struct sockaddr *) &skaddr, &length)<0) {
printf("Error getsockname\n");
exit(1);
}
/* port number's are network byte order, we have to convert to
host byte order before printing !
*/
printf("The server UDP port number is %d\n",ntohs(skaddr.sin_port));
/* Go echo every datagram we get */
echo(ld);
return(0);
}
我是 Linux 新手,按照一些教程在我的 zedboard 上启动了 Linux。现在我必须制作一个在其上运行 udp echo 服务器的应用程序,我通过互联网获得了这个程序,现在我很困惑 arpa/inet.h netinet/in.h 是否已经在 Linux 中可用,还是我们必须制作它们?我应该如何以及在哪里提供 IP 地址和端口号?
答案1
不,你不必自己写,arpa/inet.h
或者netinet/in.h
- 它们由libc6-dev包裹:
$ dpkg -L libc6-dev | grep 'arpa\|netinet'
/usr/include/arpa
/usr/include/arpa/nameser_compat.h
/usr/include/arpa/telnet.h
/usr/include/arpa/tftp.h
/usr/include/arpa/inet.h
/usr/include/arpa/nameser.h
/usr/include/arpa/ftp.h
/usr/include/netinet
/usr/include/netinet/igmp.h
/usr/include/netinet/in_systm.h
/usr/include/netinet/ip.h
/usr/include/netinet/tcp.h
/usr/include/netinet/if_fddi.h
/usr/include/netinet/ip_icmp.h
/usr/include/netinet/ip6.h
/usr/include/netinet/ether.h
/usr/include/netinet/if_tr.h
/usr/include/netinet/udp.h
/usr/include/netinet/if_ether.h
/usr/include/netinet/icmp6.h
/usr/include/netinet/in.h