:v/\S/d
Tuesday, 21 August 2012
IPV6 Hostname Resolution in Linx
/* IPV6 resolving host name
arg: hostname*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
struct addrinfo *res;
char ip_addr[INET6_ADDRSTRLEN];
struct sockaddr_in6 *saddr;
socklen_t len ;
saddr = NULL;
if( argc != 2 )
{
return -1;
}
res = NULL;
printf("host[%s]\n", argv[1]);
getaddrinfo(argv[1], NULL ,NULL,&res);
if ( res != NULL )
{
printf("ai_flags -> %i\n", res->ai_flags) ;
printf("ai_family -> %i\n", res->ai_family) ;
printf("ai_socktype -> %i\n", res->ai_socktype) ;
printf("ai_protocol -> %i\n", res->ai_protocol) ;
printf("ai_addrlen -> %i\n", res->ai_addrlen) ;
if ( res->ai_family == 2 )
{
struct sockaddr_in *saddr = (struct sockaddr_in*)res->ai_addr;
printf("ai_addr hostname -> %s\n", inet_ntoa( saddr->sin_addr));
}
if ( res->ai_family == 10 )
{
saddr = (struct sockaddr_in6*)res->ai_addr;
inet_ntop(AF_INET6, &saddr->sin6_addr, ip_addr, INET6_ADDRSTRLEN);
printf("address[%s]\n", ip_addr);
}
}
else
{
printf("host is not found\n");
}
freeaddrinfo(res);
return 0 ;
}
arg: hostname*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main (int argc, char *argv[])
{
struct addrinfo *res;
char ip_addr[INET6_ADDRSTRLEN];
struct sockaddr_in6 *saddr;
socklen_t len ;
saddr = NULL;
if( argc != 2 )
{
return -1;
}
res = NULL;
printf("host[%s]\n", argv[1]);
getaddrinfo(argv[1], NULL ,NULL,&res);
if ( res != NULL )
{
printf("ai_flags -> %i\n", res->ai_flags) ;
printf("ai_family -> %i\n", res->ai_family) ;
printf("ai_socktype -> %i\n", res->ai_socktype) ;
printf("ai_protocol -> %i\n", res->ai_protocol) ;
printf("ai_addrlen -> %i\n", res->ai_addrlen) ;
if ( res->ai_family == 2 )
{
struct sockaddr_in *saddr = (struct sockaddr_in*)res->ai_addr;
printf("ai_addr hostname -> %s\n", inet_ntoa( saddr->sin_addr));
}
if ( res->ai_family == 10 )
{
saddr = (struct sockaddr_in6*)res->ai_addr;
inet_ntop(AF_INET6, &saddr->sin6_addr, ip_addr, INET6_ADDRSTRLEN);
printf("address[%s]\n", ip_addr);
}
}
else
{
printf("host is not found\n");
}
freeaddrinfo(res);
return 0 ;
}
Monday, 20 August 2012
IPV6 Host Name Resolving in Linux
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
char tmphstbuf [4096];
size_t hstbuflen = sizeof (tmphstbuf);
struct hostent hostinfo_buf = {0,};
struct hostent *hostresult = NULL;
int herr ;
int ret = -1 ;
size_t len;
char ip_addr[INET6_ADDRSTRLEN] = {0,};
struct sockaddr_in6 addr;
len = sizeof(tmphstbuf);
ret = gethostbyname_r(argv[1], &hostinfo_buf, tmphstbuf, len, &hostresult, &herr);
printf("Addresses:\t");
inet_ntop(AF_INET6, hostinfo_buf.h_addr_list[0], ip_addr, INET6_ADDRSTRLEN);
printf("%s\n", ip_addr);
}
/* Keep add your hostnames in /etc/hosts file */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main(int argc, char *argv[])
{
char tmphstbuf [4096];
size_t hstbuflen = sizeof (tmphstbuf);
struct hostent hostinfo_buf = {0,};
struct hostent *hostresult = NULL;
int herr ;
int ret = -1 ;
size_t len;
char ip_addr[INET6_ADDRSTRLEN] = {0,};
struct sockaddr_in6 addr;
len = sizeof(tmphstbuf);
ret = gethostbyname_r(argv[1], &hostinfo_buf, tmphstbuf, len, &hostresult, &herr);
printf("Addresses:\t");
inet_ntop(AF_INET6, hostinfo_buf.h_addr_list[0], ip_addr, INET6_ADDRSTRLEN);
printf("%s\n", ip_addr);
}
/* Keep add your hostnames in /etc/hosts file */
Saturday, 4 August 2012
Simple UDP EchoServer
/*
* @filename : server.c
* @brief: Server Program
* */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_LINE 256
int conn_count=0;
void *HandlingClient(void *);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, client_addr;
int sockid, newsockid, len, ret;
char buf[MAX_LINE];
if(argc < 2 )
{
printf("usage: %s <port>\n", argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM, 0);
if(sockid < 0 )
{
printf("Failed to get socket\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if((bind(sockid,(struct sockaddr*)&serv_addr,sizeof(serv_addr))) != 0)
{
printf("Failed to bind\n");
return -2;
}
len = sizeof(client_addr);
while(1)
{
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&client_addr, &len);
if ( ret == 0 )
{
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
if(!strncasecmp(buf,"bye",3))
{
break;
}
/* send back to client*/
if( (ret = sendto(sockid, &buf, ret, 0, (struct sockaddr*)&client_addr, sizeof(client_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
}
close(sockid);
return 0;
}
/*
* @filename : client.c
* @brief: Client Program
* */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_LINE 256
void *SendReceive(void* sockid);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, cli_addr ;
int sockid;
char buf[MAX_LINE],sendbuf[MAX_LINE];
int length,ret ;
socklen_t len;
if(argc < 3 )
{
printf("usage: %s <server-address> <port> \n",argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM,0) ;
if(sockid == -1 )
{
printf("Failed to get socket\n");
return -2;
}
serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]) ;
serv_addr.sin_port = htons(atoi(argv[2])) ;
len = 1;
while(1)
{
memset(buf,0,sizeof(buf));
if(fgets(buf,MAX_LINE,stdin) == NULL )
{
printf ("received EOF from stdin\n");
break;
}
length = strlen(buf);
if( (ret = sendto(sockid, &buf, length, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&cli_addr, &len);
if ( ret == 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
printf("%s",buf);
}
close(sockid);
return 0;
}
MESSAGE="Echo Program Built Successfully"
MESSAGE1 = " ------------------------- "
CC = gcc
CFLAGS = -g -I./include
all: server client
@echo $(MESSAGE1)
@echo $(MESSAGE)
@echo $(MESSAGE1)
server: server.o
${CC} -o $@ server.o
client: client.o
${CC} -o $@ client.o
clean:
-@rm *.o
distclean: clean
-@rm server client
* @filename : server.c
* @brief: Server Program
* */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define MAX_LINE 256
int conn_count=0;
void *HandlingClient(void *);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, client_addr;
int sockid, newsockid, len, ret;
char buf[MAX_LINE];
if(argc < 2 )
{
printf("usage: %s <port>\n", argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM, 0);
if(sockid < 0 )
{
printf("Failed to get socket\n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if((bind(sockid,(struct sockaddr*)&serv_addr,sizeof(serv_addr))) != 0)
{
printf("Failed to bind\n");
return -2;
}
len = sizeof(client_addr);
while(1)
{
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&client_addr, &len);
if ( ret == 0 )
{
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
if(!strncasecmp(buf,"bye",3))
{
break;
}
/* send back to client*/
if( (ret = sendto(sockid, &buf, ret, 0, (struct sockaddr*)&client_addr, sizeof(client_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
}
close(sockid);
return 0;
}
/*
* @filename : client.c
* @brief: Client Program
* */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define MAX_LINE 256
void *SendReceive(void* sockid);
int main(int argc,char *argv[])
{
struct sockaddr_in serv_addr, cli_addr ;
int sockid;
char buf[MAX_LINE],sendbuf[MAX_LINE];
int length,ret ;
socklen_t len;
if(argc < 3 )
{
printf("usage: %s <server-address> <port> \n",argv[0]);
return -1;
}
sockid = socket(AF_INET,SOCK_DGRAM,0) ;
if(sockid == -1 )
{
printf("Failed to get socket\n");
return -2;
}
serv_addr.sin_family = AF_INET ;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]) ;
serv_addr.sin_port = htons(atoi(argv[2])) ;
len = 1;
while(1)
{
memset(buf,0,sizeof(buf));
if(fgets(buf,MAX_LINE,stdin) == NULL )
{
printf ("received EOF from stdin\n");
break;
}
length = strlen(buf);
if( (ret = sendto(sockid, &buf, length, 0, (struct sockaddr*)&serv_addr, sizeof(serv_addr))) <= 0 )
{
printf("Client coultn't send message to server\n");
break;
}
ret = recvfrom(sockid,&buf,MAX_LINE,0, (struct sockaddr*)&cli_addr, &len);
if ( ret == 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
else if ( ret < 0 )
{
printf("Error Occurs Err[#%d,%s]\n", errno, strerror(errno) );
break;
}
printf("%s",buf);
}
close(sockid);
return 0;
}
MESSAGE="Echo Program Built Successfully"
MESSAGE1 = " ------------------------- "
CC = gcc
CFLAGS = -g -I./include
all: server client
@echo $(MESSAGE1)
@echo $(MESSAGE)
@echo $(MESSAGE1)
server: server.o
${CC} -o $@ server.o
client: client.o
${CC} -o $@ client.o
clean:
-@rm *.o
distclean: clean
-@rm server client
Subscribe to:
Posts (Atom)