rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <arpa/inet.h> |
| 5 | #include<stdlib.h> |
| 6 | #include <netinet/in.h> |
| 7 | #include <strings.h> |
| 8 | #include "liblog/liblog.h" |
| 9 | #include "liblog/lynq_deflog.h" |
| 10 | |
| 11 | |
| 12 | #define USER_LOG_TAG "UDPCLI" |
| 13 | #define BUF_LEN 128 |
| 14 | #define ERR_CMDVALID 2 |
| 15 | #define ERR_SOCK 3 |
| 16 | #define SA struct sockaddr |
| 17 | |
| 18 | int main(int argc, const char *argv[]) |
| 19 | { |
| 20 | int sockfd; |
| 21 | char buf[BUF_LEN]; |
| 22 | |
| 23 | |
| 24 | LYLOGEINIT(USER_LOG_TAG); |
| 25 | if(argc!=3) |
| 26 | { |
| 27 | LYDBGLOG("argv is wrong, please input ip and port\n"); |
| 28 | LYVERBLOG("+[udpcli]: error num = %d\n", ERR_CMDVALID); |
| 29 | return ERR_CMDVALID; |
| 30 | } |
| 31 | |
| 32 | sockfd=socket(AF_INET,SOCK_DGRAM,0); |
| 33 | |
| 34 | if(sockfd<0) |
| 35 | { |
| 36 | LYDBGLOG("fail to sockfd\n"); |
| 37 | return ERR_SOCK; |
| 38 | } |
| 39 | |
| 40 | struct sockaddr_in seraddr; |
| 41 | seraddr.sin_family=AF_INET; |
| 42 | seraddr.sin_port=htons(atoi(argv[2])); |
| 43 | seraddr.sin_addr.s_addr=inet_addr(argv[1]); |
| 44 | |
| 45 | int len=sizeof(seraddr); |
| 46 | |
| 47 | while(1) |
| 48 | { |
| 49 | bzero(buf,sizeof(buf)); |
| 50 | |
| 51 | fgets(buf,sizeof(buf),stdin); |
| 52 | |
| 53 | sendto(sockfd,buf,sizeof(buf),0,(SA *)&seraddr,len); |
| 54 | |
| 55 | recvfrom(sockfd,buf,sizeof(buf),0,NULL,NULL); |
| 56 | |
| 57 | LYVERBLOG("+[udpcli]: cli_rcv = %s\n",buf); |
| 58 | } |
| 59 | return 0; |
| 60 | } |