diff --git a/client/cli.c b/client/cli.c new file mode 100644 index 0000000..13792c3 --- /dev/null +++ b/client/cli.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +int main(int argc, char **argv) +{ + struct hostent *hname; + struct in_addr **addr_list; + int i; +struct sockaddr_in sa,cli; +int n,sockfd; +int len;char buff[100]; +sockfd=socket(AF_INET,SOCK_STREAM,0); +if(sockfd<0){ printf("\nError in Socket"); +exit(0); +} +else printf("\nSocket is Opened"); +bzero(&sa,sizeof(sa)); +sa.sin_family=AF_INET; +sa.sin_port=htons(9924); +inet_pton(AF_INET, argv[1], &sa.sin_addr); +if(connect(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) +{ +printf("\nError in connection failed"); +exit(0); +} +else +printf("\nconnected successfully"); +if(n=read(sockfd,buff,sizeof(buff))<0) +{ +printf("\nError in Reading"); +exit(0); +} +else +{printf("\nMessage Read %s",buff); +} +// printf("%s\n", inet_ntoa(sa.sin_addr)); + + if ((hname = gethostbyname(argv[1])) == NULL) { + perror("hostname error"); + exit(2); + } + printf("IP address of %s : ",argv[1]); + addr_list = (struct in_addr **)hname->h_addr_list; + for(i = 0; addr_list[i] != NULL; i++) { + printf("%s ", inet_ntoa(*addr_list[i])); + } + printf("\n"); + printf("%d\n", ntohs(sa.sin_port)); +} diff --git a/server/UDP-SErv.c b/server/UDP-SErv.c new file mode 100644 index 0000000..1845ed1 --- /dev/null +++ b/server/UDP-SErv.c @@ -0,0 +1,52 @@ +#include"netinet/in.h" +#include"sys/socket.h" +#include"stdio.h" +#include"string.h" +#include"time.h" +main( ) +{ + struct sockaddr_in sa; + struct sockaddr_in cli; + int sockfd,conntfd; + int len,ch; + char str[100]; + time_t tick; + +//socket function + + sockfd=socket(AF_INET,SOCK_DGRAM,0); + if(sockfd<0) + { + printf("error in socket\n"); + exit(0); + } + else + printf("Socket opened"); + + bzero(&sa,sizeof(sa)); + sa.sin_port=htons(9924); + sa.sin_addr.s_addr=htonl(0); +//Binding of socket to ip address and port no. + if(bind(sockfd,(struct sockaddr*)&sa,sizeof(sa))<0) + { + printf("Error in binding\n"); + } + else + printf("Binded Successfully"); + +//listens the clients requests andd max 50 + listen(sockfd,50); + + for(;;) + { + len=sizeof(ch); + //accept client request + conntfd=accept(sockfd,(struct sockaddr*)&cli,&len); + printf("Accepted"); + //prepare data to be sent to connected client + tick=time(NULL); + snprintf(str,sizeof(str),"%s",ctime(&tick)); + printf("%s",str); + write(conntfd,str,100); //send data to client + } +}