udp测试程序 + makefile
[@more@]
01 /*02 ** talker.c -- a datagram "client" demo03 */04 05 #include 06 #include 07 #include 08 #include 09 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 16 #define SERVERPORT "4950" // the port users will be connecting to17 18 int main(
int argc, char *argv[])19 {20 int sockfd;
21 struct addrinfo hints, *servinfo, *p;
22 int rv;
23 int numbytes;
24 25 if (
argc != 3)
{26 fprintf(
stderr,"usage: talker hostname messagen");
27 exit(
1);
28 }29 30 memset(
&hints, 0, sizeof hints);
31 hints.ai_family = AF_UNSPEC;
32 hints.ai_socktype = SOCK_DGRAM;
33 34 if ((
rv = getaddrinfo(
argv[1], SERVERPORT, &hints, &servinfo))
!= 0)
{35 fprintf(
stderr, "getaddrinfo: %sn", gai_strerror(
rv));
36 return 1;
37 }38 39 // loop through all the results and make a socket40 for(p
= servinfo; p
!= NULL; p
= p
->ai_next)
{41 if ((
sockfd = socket(p
->ai_family, p
->ai_socktype,42 p
->ai_protocol))
== -1)
{43 perror(
"talker: socket");
44 continue;
45 }46 47 break;
48 }49 50 if (p
== NULL)
{51 fprintf(
stderr, "talker: failed to bind socketn");
52 return 2;
53 }54 55 if ((
numbytes = sendto(
sockfd, argv[2], strlen(
argv[2]), 0,56 p
->ai_addr, p
->ai_addrlen))
== -1)
{57 perror(
"talker: sendto");
58 exit(
1);
59 }60 61 freeaddrinfo(
servinfo);
62 63 printf(
"talker: sent %d bytes to %sn", numbytes, argv[1]);64 close(
sockfd);
65 66 return 0;
67 }
# $Id: Makefile,v 1.5 2005/09/20 10:56:17 beah Exp $
#CROSS = ppc_440ep-
#CROSS = arm-soft_uclibc-linux-gnu-
CROSS = arm-hismall-linux-
#CROSS =
CC = $(CROSS)gcc
STRIP = $(CROSS)strip
BINDIR = /usr/local/sbin
MANDIR = /usr/local/man
CFLAGS+= -g -Wall
# CFLAGS+= -DHAS_FGETLN -DHAS_STRLCPY
# CFLAGS+= -Wall -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations
# CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual -Wsign-compare
LDFLAGS+= -lpthread
all: talker
$(STRIP) talker
cp talker /tftpboot/
cp talker /target/
talker: talker.o
$(CC) $(LDFLAGS) talker.o $(LDLIBS) -o talker
talker.o: talker.c
$(CC) $(CFLAGS) -c talker.c
install: all
install -o root -g bin -m 0555 talker $(BINDIR)
# install -o root -g wheel -m 0755 -d $(MANDIR)/man8
# install -o root -g bin -m 0444 talker.8 $(MANDIR)/man8
clean:
rm -f talker *.o
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23168012/viewspace-1048951/
,如需转载,请注明出处,否则将追究法律责任。