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 <netinet/in.h> |
| 6 | #include <strings.h> |
| 7 | #include "liblog/liblog.h" |
| 8 | #include "liblog/lynq_deflog.h" |
| 9 | |
| 10 | #define USER_LOG_TAG "UDPSER" |
| 11 | #define BUF_LEN 128 |
| 12 | #define ERR_SOCK 3 |
| 13 | #define SA struct sockaddr |
| 14 | |
| 15 | int main(int argc, const char *argv[]) |
| 16 | { |
| 17 | int sockfd; |
| 18 | char buf[128]; |
| 19 | |
| 20 | LYLOGEINIT(USER_LOG_TAG); |
| 21 | sockfd=socket(AF_INET,SOCK_DGRAM,0); |
| 22 | |
| 23 | if(sockfd<0) |
| 24 | { |
| 25 | LYDBGLOG("fail to sockfd\n"); |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | struct sockaddr_in seraddr,peeraddr; |
| 30 | seraddr.sin_family=AF_INET; |
| 31 | seraddr.sin_port=htons(50000); |
| 32 | |
| 33 | seraddr.sin_addr.s_addr=inet_addr("0.0.0.0"); |
| 34 | |
| 35 | int ret_bind; |
| 36 | ret_bind=bind(sockfd,(struct sockaddr *)&seraddr,sizeof(seraddr)); |
| 37 | |
| 38 | if(ret_bind<0) |
| 39 | { |
| 40 | LYDBGLOG("fail to bind\n"); |
| 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | int len=sizeof(peeraddr); |
| 45 | |
| 46 | while(1) |
| 47 | { |
| 48 | bzero(buf,sizeof(buf)); |
| 49 | |
| 50 | recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&peeraddr,&len); |
| 51 | |
| 52 | LYVERBLOG("+[udpser]: ser_rcv = %s\n", buf); |
| 53 | sendto(sockfd,buf,sizeof(buf),0,(SA *)&peeraddr,len); |
| 54 | } |
| 55 | return 0; |
| 56 | } |