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<stdlib.h> |
| 7 | #include "liblog/liblog.h" |
| 8 | #include "liblog/lynq_deflog.h" |
| 9 | |
| 10 | #define USER_LOG_TAG "TCPCLI" |
| 11 | #define SA struct sockaddr |
| 12 | #define BUF_LEN 128 |
| 13 | #define ERR_CMDVALID 2 |
| 14 | #define ERR_SOCK 3 |
| 15 | |
| 16 | int main(int argc, const char *argv[]) |
| 17 | { |
| 18 | int sockfd; |
| 19 | char buf[BUF_LEN]; |
| 20 | int ret_con; |
| 21 | |
| 22 | LYLOGEINIT(USER_LOG_TAG); |
| 23 | if(argc<3) |
| 24 | { |
| 25 | LYDBGLOG("arg is wrong, please input ip and port\n"); |
| 26 | LYVERBLOG("+[tcpcli]: error num = %d\n", ERR_CMDVALID); |
| 27 | return ERR_CMDVALID; |
| 28 | } |
| 29 | |
| 30 | sockfd=socket(AF_INET,SOCK_STREAM,0); |
| 31 | |
| 32 | if(sockfd<0) |
| 33 | { |
| 34 | LYDBGLOG("fail to socket\n"); |
| 35 | return ERR_SOCK; |
| 36 | } |
| 37 | struct sockaddr_in seraddr; |
| 38 | seraddr.sin_family=AF_INET; |
| 39 | seraddr.sin_port=htons(atoi(argv[2])); |
| 40 | seraddr.sin_addr.s_addr=inet_addr(argv[1]); |
| 41 | |
| 42 | |
| 43 | while(1) |
| 44 | { |
| 45 | ret_con=connect(sockfd,(SA *)&seraddr,sizeof(seraddr)); |
| 46 | if(ret_con<0) |
| 47 | { |
| 48 | LYDBGLOG("fail to connect\n"); |
| 49 | return ERR_SOCK; |
| 50 | } |
| 51 | while(1) |
| 52 | { |
| 53 | bzero(buf,sizeof(buf)); |
| 54 | fgets(buf,sizeof(buf),stdin); |
| 55 | |
| 56 | send(sockfd,buf,sizeof(buf),0); |
| 57 | |
| 58 | bzero(buf,sizeof(buf)); |
| 59 | recv(sockfd,buf,sizeof(buf),0); |
| 60 | |
| 61 | LYVERBLOG("+[tcpcli]: cli_rcv = %s\n",buf); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return 0; |
| 66 | } |