#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <memory.h>
void is_host(struct hostent *host,char *name)
{
if(!strcmp(host->h_name,name)) return 1;
int i=0;
while(host->h_aliases[i] !=NULL)
{
if(!strcmp(host->h_aliases[i],name))
i++;
}
}
unsigned int get_ip_by_name(char *name)
{
unsigned int ip=0;
structr hostent *host;
while((host = gethostent()) != NULL)
{
if(is_host(host,name))
{
memcpy(&ip,host->h_addr_list[0],4);
break;
}
}
endhostent();
return ip;
}
int main(int argc,char * argv[])
{
if(argc < 3)
{
printf("usage: %s ip port
",argv[0]);
exit(1);
}
/*步骤1 创建socket*/
int sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(sockfd < 0)
{
perror("socket error");
exit(1);
}
/*步骤2 调用recvfrom和sendto等函数和服务器端双向通信*/
struct sockaddr_in serveraddr;
memset(&serveradd,0,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
//serveraddr.sin_port = htons(atoi(argv[2]));
//inet_pton(AF_INET,argv[1],&serveraddr.sin_addr.s_addr);
unsigned int ip = get_ip_by_name(argv[1]);
if(ip != 0)
{
serveraddr.sin_addr.s_addr =ip;
}
else
{
inet_pton(AF_INET,argv[1],&serveraddr.sin_addr.s_addr);
}
char buffer[1024] = "hello iotek";
//向服务器发送数据报文
if(sendto(sockfd,buffer,sizeof(buffer),0,
(struct sockaddr*)&serveraddr),sizeof(serveraddr) < 0)
{ perror("sendto error");
exit(1);
}
else
{
//接受服务器端发送的数据报文
memset(buffer,0,sizeof(buffer));
if(recv(sockfd,buffer,sizeof(buffer),0) < 0)
{
perror("recv error");
exit(1);
}
else
{
printf("%s",buffer);
}
}
close(sockfd);
}