/*
******************************************************************************
* @file main.c
* @author Wang
* @version V1.0.0
* @date 9-August-2019
* @brief This file call to function in other files
********************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
/*
* @brief main entry point
* @param argc : the number of param
* @param **argv : command-line pass arguments
* @retval return 0
*/
int main(int argc , const char **argv)
{
int socket_fd;
struct sockaddr_in server_addr;
char buf[100] = {0};
int n = 0 , len = sizeof(server_addr);
if(argc != 3)
{
printf(&quot;Usage : %s ip port \n&quot;,argv[0]);
return -1;
}
/* use socket() obtain sock_fd */
socket_fd = socket(AF_INET , SOCK_STREAM , 0);
if(socket_fd < 0)
{
perror(&quot;Fail to socket&quot;);
}
/* Fill the information about server */
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(atoi(argv[2]));
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
/* request connect server */
if((connect(socket_fd , (struct sockaddr *)&server_addr , len)) < 0)
{
perror(&quot;Fail to connect&quot;);
return -1;
}
printf(&quot;Please input data:\n&quot;);
while(1)
{
memset(buf , 0 , sizeof(buf));
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf) - 1] = '\0';
n = send (socket_fd , buf , strlen(buf) , 0);
if(n < 0)
{
perror(&quot;Fail to send&quot;);
return -1;
}
if(strncmp(buf , &quot;quit&quot; , 4) == 0)
break;
printf(&quot;send %d bytes : %s\n&quot;,n,buf);
}
close(socket_fd);
return 0;
}
/**************************************END OF FILE**************************************/ |