Skip to content

Instantly share code, notes, and snippets.

@shadeslayer
Created April 18, 2011 09:21
Show Gist options
  • Save shadeslayer/925062 to your computer and use it in GitHub Desktop.
Save shadeslayer/925062 to your computer and use it in GitHub Desktop.
Exp 6
/********************************************* * Author: Rohan Garg <[email protected]> * * License: MIT ( 2011 ) * * client.c [ Experiment 6 ] *
*********************************************/
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/errno.h>
#include<string.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>
# define MYPORT 21750
int main()
{
int socketid, k,len;
char buf[100];
char ch[100];
struct sockaddr_in server, client;
socketid=socket(AF_INET,SOCK_STREAM,0);
server.sin_family=AF_INET;
server.sin_port=htons(MYPORT);
server.sin_addr.s_addr=inet_addr("192.168.56.150");
len=sizeof(server);
k=connect(socketid,(struct sockaddr*) &server,len);
while(1){
printf("Enter site name :\n ");
len = read(0 , ch , 100);
ch[len-1] = '\0';
write(k, ch, len);
len = read(socketid,buf, 100);
buf[len-1]='\0';
if(len>0){
printf(" %s\n",inet_ntoa(*((struct in_addr*) buf)));
}
}
close(socketid);
return 1;
}
/*********************************************
* OUTPUT *
* student@ubuntu:~/08EL285$ ./client *
* Enter site name : *
* www.google.com *
* 88.243.100.237 *
* Enter site name : *
* www.gmail.com *
* 88.243.100.237 *
*********************************************/
/********************************************* * Author: Rohan Garg <[email protected]> *
* License: MIT ( 2011 )
* server.c [ Experiment 6 ] *
*********************************************/
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<arpa/inet.h>
#include<sys/socket.h>
#include<sys/errno.h>
#include<netdb.h>
#define MYPORT 21750
int main()
{
struct hostent *host;
int sockid, len;
int retbind, retaccept;
char ch[100];
struct sockaddr_in server, client;
sockid=socket(AF_INET,SOCK_STREAM,0);
int k;
server.sin_family=AF_INET;
server.sin_port=htons(MYPORT);
server.sin_addr.s_addr=inet_addr("192.168.56.150");
retbind = bind(sockid,(struct sockaddr*) &server,sizeof(server));
listen(sockid,10);
len=sizeof(client);
retaccept = accept(sockid , (struct sockaddr*) &client, &len);
while(1){
len = read(retaccept , ch , 100);
ch[len] = '\0';
host = gethostbyname(ch);
if(host == NULL)
write(retaccept , " Invalid site name \n" , 20 );
else
write(retaccept ,host->h_addr , 40 );
}
close(sockid);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment