rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <sys/socket.h> |
| 3 | #include <netinet/in.h> |
| 4 | #include <arpa/inet.h> |
| 5 | #include <strings.h> |
| 6 | #include <unistd.h> |
| 7 | #include "liblog/liblog.h" |
| 8 | #include "liblog/lynq_deflog.h" |
| 9 | |
| 10 | #define USER_LOG_TAG "TCPSER" |
| 11 | #define SA struct sockaddr |
| 12 | #define BUF_LEN 128 |
| 13 | #define ERR_SOCK 3 |
| 14 | |
| 15 | int main(int argc, const char *argv[]) |
| 16 | { |
| 17 | int sockfd,confd; |
| 18 | char buf[BUF_LEN]; |
| 19 | int ret_recv; |
| 20 | int ret_bind; |
| 21 | |
| 22 | LYLOGEINIT(USER_LOG_TAG); |
| 23 | sockfd=socket(AF_INET,SOCK_STREAM,0); |
| 24 | |
| 25 | if(sockfd<0) |
| 26 | { |
| 27 | LYDBGLOG("fail to socket\n"); |
| 28 | return ERR_SOCK; |
| 29 | } |
| 30 | struct sockaddr_in seraddr; |
| 31 | seraddr.sin_family=AF_INET; |
| 32 | seraddr.sin_port=htons(50000); |
| 33 | seraddr.sin_addr.s_addr=inet_addr("0.0.0.0"); |
| 34 | |
| 35 | ret_bind=bind(sockfd,(SA *)&seraddr,sizeof(seraddr)); |
| 36 | if(ret_bind<0) |
| 37 | { |
| 38 | LYDBGLOG("fail to bind\n"); |
| 39 | return ERR_SOCK; |
| 40 | } |
| 41 | |
| 42 | listen(sockfd,5); |
| 43 | |
| 44 | while(1) |
| 45 | { |
| 46 | confd=accept(sockfd,NULL,NULL); |
| 47 | //printf("confd =%d\n",confd); |
| 48 | if(confd<0) |
| 49 | { |
| 50 | LYDBGLOG("fail to accept\n"); |
| 51 | return ERR_SOCK; |
| 52 | } |
| 53 | |
| 54 | while(1) |
| 55 | { |
| 56 | bzero(buf,sizeof(buf)); |
| 57 | |
| 58 | ret_recv=recv(confd,buf,sizeof(buf),0); |
| 59 | |
| 60 | if(ret_recv<0) |
| 61 | { |
| 62 | LYDBGLOG("fail to recv\n"); |
| 63 | return ERR_SOCK; |
| 64 | } |
| 65 | if(ret_recv==0) |
| 66 | { |
| 67 | close(confd); |
| 68 | LYDBGLOG("closed,confd =%d\n",confd); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | LYVERBLOG("+[tcpser]: ser_rcv = %s\n",buf); |
| 73 | send(confd,buf,sizeof(buf),0); |
| 74 | } |
| 75 | |
| 76 | } |
| 77 | |
| 78 | |
| 79 | return 0; |
| 80 | } |