udp测试程序 + makefile

udp测试程序 + makefile

[@more@]
C语言: Codee#18774
01 /*
02 ** talker.c -- a datagram "client" demo
03 */
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 to
17
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 message n ");
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: %s n " , gai_strerror( rv));
36         return 1;
37     }
38
39     // loop through all the results and make a socket
40     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 socket n ");
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 %s n " , numbytes , argv [ 1 ]);
64     close( sockfd);
65
66     return 0;
67 }
Makefile语言: Codee#18775
#    $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博客 ” ,链接:http://blog.itpub.net/23168012/viewspace-1048951/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/23168012/viewspace-1048951/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值