Thursday, 26 September 2013

Raw Socket Application 2


We can do spoofing using row socket . we can take the ip packet and change the ip address so receiver will think that packet came from some one else. This concept is widely used for hacking a network . Program one get message and change IP header

1. Program 1 : It get message from Program 3 and modify Ip header and send to program 2 . program 2 think packet is coming from program 1
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/select.h>
#include<sys/un.h>
#include<netinet/ip.h>
#include<netinet/udp.h>
unsigned short csum (unsigned short *buf, int nwords)
{
 unsigned long sum;
 for (sum = 0; nwords > 0; nwords--)
  sum += *buf++;
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 return ~sum;
}


int main()
{
 int rsfd=socket(AF_INET,SOCK_RAW,IPPROTO_UDP);
 char buf[4096]={0};
 char msg[1000]={0};
 struct ip* ipheader=(struct ip*)buf;
 ipheader->ip_hl=5;
 ipheader->ip_v=4;
 ipheader->ip_tos=0;
 ipheader->ip_len=20+8+sizeof(msg);
 ipheader->ip_id=0;
 ipheader->ip_off=0;
 ipheader->ip_ttl=64;
 ipheader->ip_p=17;
 inet_pton(AF_INET,"127.0.0.1",&(ipheader->ip_src));
 inet_pton(AF_INET,"127.0.0.1",&(ipheader->ip_dst));
 ipheader->ip_sum=csum((unsigned short*)buf,9);

     int k=1;
     const int *l = &k;
     if (setsockopt (rsfd, IPPROTO_IP, IP_HDRINCL ,l, sizeof (k)) < 0)
        printf ("ERROR IN HDRINCL!\n");
      
 struct udphdr *udpheader=(struct udphdr*)(buf+ipheader->ip_hl*4);

 udpheader->source=htons(3400);
 udpheader->dest=htons(2300);
 udpheader->len=htons(8+sizeof(msg));
 udpheader->check=0;
 memcpy(buf+20,udpheader,8);

 struct sockaddr_in addr;
 addr.sin_port=htons(2300);
 addr.sin_family=AF_INET;
 inet_pton(AF_INET,"127.0.0.1",&(addr.sin_addr));
 while(1)
 {
  fgets(msg,1000,stdin);
  strcpy(buf+28,msg);
  sendto(rsfd,buf,4096,0,(struct sockaddr*) &addr,sizeof addr);
 }

 return 0;
}



2. program 2
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<sys/ipc.h>
#include<sys/stat.h>
#include<signal.h>
#include<sys/sem.h>
#include<pthread.h>

int sfd;
struct sockaddr_in serv;
void* rcv()
{
 struct sockaddr_in clt;
 int length;
char buf[512];
while(1)
{
recvfrom(sfd,buf,512,0,(struct sockaddr*)&clt,&length);
printf("received msg is:%s\n",buf);
}
}

int main()
{
sfd=socket(AF_INET,SOCK_DGRAM,0);
struct sockaddr_in myaddr;
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(3400);
myaddr.sin_addr.s_addr=INADDR_ANY;
int b=bind(sfd,(struct sockaddr*)&myaddr,sizeof(struct sockaddr_in));
if(b==-1)
perror("bind error\n"); 



serv.sin_family=AF_INET;
serv.sin_port=htons(2300);
inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr);
pthread_t t;
pthread_create(&t,NULL,&rcv,NULL);
while(1)
{
char buf[512]={0};
fgets(buf,512,stdin);
sendto(sfd,buf,512,0,(struct sockaddr*)&serv,sizeof serv);
}
return 0;
}




3. program 3
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/select.h>
#include<sys/un.h>
#include<netinet/ip.h>
#include<netinet/udp.h>

int main()
{
int usfd=socket(AF_INET,SOCK_DGRAM,0);
if(usfd==-1)
perror("sock error\n");
struct sockaddr_in servaddr,clientaddr;
socklen_t length=sizeof(struct sockaddr_in);
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(2300);
servaddr.sin_addr.s_addr=INADDR_ANY;
int b=bind(usfd,(struct sockaddr*)&servaddr,sizeof(struct sockaddr_in));
if(b<0)
perror("Bind error\n");

while(1)
{
char msg[512]={0}; 
recvfrom(usfd,msg,512,0,(struct sockaddr*)&clientaddr,&length);
printf(" msg received from client of port id %d\n",htons(clientaddr.sin_port));
printf("msg received is:%s",msg);
printf("enter msg to send \n");
gets(msg);
sendto(usfd,msg,512,0,(struct sockaddr*)&clientaddr,length);
 perror("send");
}
return 0;
}


RAW Socket Application 1


In this program two processes p1 and p2 of different system will communicate with each other using row socket. each process will have one raw socket with unique id which will only be known by other so packet sent by one will only be received by other. Here is the program

1. process 1
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/ip.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<netinet/udp.h>

int sfd;
unsigned short csum (unsigned short *buf, int nwords)
{
 unsigned long sum;
 for (sum = 0; nwords > 0; nwords--)
  sum += *buf++;
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 return ~sum;
}

void *send_msg(void* arg)
{

        printf("send\n");
 char buf[4096],msg[512];
 struct ip* iph=(struct ip*)buf;
iph->ip_hl=5;
iph->ip_v=4;
iph->ip_tos=0;
iph->ip_len=20+sizeof(struct ip);
iph->ip_id=0;
 iph->ip_off=0;
 iph->ip_ttl=64;
 iph->ip_p=21;
 inet_pton(AF_INET,"127.0.0.1",&(iph->ip_src));
 inet_pton(AF_INET,"127.0.0.1",&(iph->ip_dst));
 iph->ip_sum=csum((unsigned short*)buf,iph->ip_len>>1);
int one =1;
 const int *val=&one;
 if(setsockopt(sfd,IPPROTO_IP,IP_HDRINCL,val,sizeof(one))<0)
 {
  printf("hdr include error\n");
 }



struct sockaddr_in server;
 server.sin_family=AF_INET;
 server.sin_port=htons(3500);
 server.sin_addr.s_addr=inet_addr("127.0.0.1");
 while(1)
 {
  fgets(msg,512,stdin);
  strcpy(buf+5*4,msg);
  if(sendto(sfd,buf,iph->ip_len,0,(struct sockaddr*) &server,sizeof server)<0)
   perror("Send error\n");
 }





}
void *recv_msg(void* arg)
{
printf("receive\n");
 char buf[4096];
 struct ip* ipheader=(struct ip*)buf;
 struct sockaddr_in client;
 int length;
 while(1)
 {
  if(recvfrom(sfd,buf,4096,0,(struct sockaddr*) &client,&length)<0)
   perror("receive error\n");


  printf("version is:%d\n",ipheader->ip_v);
  printf("header length:%d\n",ipheader->ip_hl*4);
  printf("service:%d\n",ipheader->ip_tos);
  printf("len:%d\n",ipheader->ip_len);
  printf("sequence id:%d\n",ipheader->ip_id);
  printf("frag offset:%d\n",ipheader->ip_off);
  printf("ttl:%d\n",ipheader->ip_ttl);
  printf("Higher level protocol:%u\n",ipheader->ip_p);
  printf("Check Sum:%d\n",ipheader->ip_sum);
  printf("Source addr:%s\n",inet_ntoa(ipheader->ip_src));
  printf("Destination addr:%s\n\n\n",inet_ntoa(ipheader->ip_dst));

  printf("msg received is:%s\n",buf+sizeof(struct ip));
 }

}

int main()
{


sfd=socket(AF_INET,SOCK_RAW,20);//here 20 is uinque protocol
if(sfd==-1)
{

perror("socket...");exit(1);
}
pthread_t t1,t2;
pthread_create(&t1,NULL,send_msg,NULL);
pthread_create(&t2,NULL,recv_msg,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);

return 0;
}



2. process 2
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/ip.h>
#include<errno.h>
#include<string.h>
#include<pthread.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<netinet/udp.h>

int sfd;
unsigned short csum (unsigned short *buf, int nwords)
{
 unsigned long sum;
 for (sum = 0; nwords > 0; nwords--)
  sum += *buf++;
 sum = (sum >> 16) + (sum & 0xffff);
 sum += (sum >> 16);
 return ~sum;
}

void *send_msg(void* arg)
{

        printf("send\n");
 char buf[4096],msg[512];
 struct ip* iph=(struct ip*)buf;
iph->ip_hl=5;
iph->ip_v=4;
iph->ip_tos=0;
iph->ip_len=20+sizeof(struct ip);
iph->ip_id=0;
 iph->ip_off=0;
 iph->ip_ttl=64;
 iph->ip_p=20;
 inet_pton(AF_INET,"127.0.0.1",&(iph->ip_src));
 inet_pton(AF_INET,"127.0.0.1",&(iph->ip_dst));
 iph->ip_sum=csum((unsigned short*)buf,iph->ip_len>>1);
int one =1;
 const int *val=&one;
 if(setsockopt(sfd,IPPROTO_IP,IP_HDRINCL,val,sizeof(one))<0)
 {
  printf("hdr include error\n");
 }



struct sockaddr_in server;
 server.sin_family=AF_INET;
 server.sin_port=htons(3500);
 server.sin_addr.s_addr=inet_addr("127.0.0.1");
 while(1)
 {
  fgets(msg,512,stdin);
  strcpy(buf+5*4,msg);
  if(sendto(sfd,buf,iph->ip_len,0,(struct sockaddr*) &server,sizeof server)<0)
   perror("Send error\n");
 }





}
void *recv_msg(void* arg)
{
printf("receive\n");
 char buf[4096];
 struct ip* ipheader=(struct ip*)buf;
 struct sockaddr_in client;
 int length=16;
 while(1)
 {
  if(recvfrom(sfd,buf,2000,0,(struct sockaddr*) &client,&length)<0)
   perror("receive error\n");


  printf("version is:%d\n",ipheader->ip_v);
  printf("header length:%d\n",ipheader->ip_hl*4);
  printf("service:%d\n",ipheader->ip_tos);
  printf("len:%d\n",ipheader->ip_len);
  printf("sequence id:%d\n",ipheader->ip_id);
  printf("frag offset:%d\n",ipheader->ip_off);
  printf("ttl:%d\n",ipheader->ip_ttl);
  printf("Higher level protocol:%u\n",ipheader->ip_p);
  printf("Check Sum:%d\n",ipheader->ip_sum);
  printf("Source addr:%s\n",inet_ntoa(ipheader->ip_src));
  printf("Destination addr:%s\n\n\n",inet_ntoa(ipheader->ip_dst));

  printf("msg received is:%s\n",buf+sizeof(struct ip));
 }

}

int main()
{


sfd=socket(AF_INET,SOCK_RAW,21);//here 21 is unique protocol
if(sfd==-1)
{

perror("socket...");exit(1);
}
pthread_t t1,t2;
pthread_create(&t1,NULL,send_msg,NULL);
pthread_create(&t2,NULL,recv_msg,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);

return 0;
}

Geting mac Address


This is a program which returns mac address of a system using raw socket

#include<sys/socket.h>
#include<sys/types.h>
#include<fcntl.h>
#include<errno.h>
#include<string.h>
#include<net/if_arp.h>
#include<stdio.h>
#include<unistd.h>
#include<signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>

#include <net/if.h>
#include <netinet/ether.h>
int main(int argc,char **argv)
{
int sockfd;
struct ifi_info *ifi;
unsigned char *ptr;
struct arpreq arpreq;
struct sockaddr_in *sin;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
for(ifi=get_ifi_info(AF_INET,0);ifi!=NULL;ifi=ifi->ifi_next)
{
printf("%s: ",sock_ntop(ifi->ifi_addr,sizeof(struct sockaddr_in*)));
sin=(struct sockaddr_in *)&arpreq.arp_pa;
memcpy(sin,ifi->ifi_addr,sizeof(struct sockaddr_in*));
if(ioctl(sockfd,SIOCGARP,&arpreq)<0)
{
perror("ioctl SIOCGARP");
continue;
}
ptr=&arpreq.arp_ha.sa_data[0];
printf("%x:%x:%x:%x:%x:%x\n",*ptr,*(ptr+1),*(ptr+2),*(ptr+3),*(ptr+4),*(ptr+5));
}

return 0;
}

PING


This Is A Socket program To Implement Ping . Program Uses Ip header and icmp packet. It is an application of Raw Socket.

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netdb.h>
#include<netinet/ip.h>
#include<netinet/ip_icmp.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
#include<signal.h>

unsigned short csum(unsigned short *buf,int nowords)//error correction of packet
{
 unsigned short sum=0;
    for ( ; nowords > 0; nowords--)
    {sum += *buf;buf++;}
    return ~sum;
}

char pingaddr[100]={0};
double maxtime=-1,mintime=10000,totaltime=0;

int noofpackets=0,success=0,fail=0;


void ping()
{

printf("\n---%s ping statistics ---\n",pingaddr);
printf("%d packets transmitted, %d received, %f percentage packet loss,time %f ms\n",noofpackets,success,(double)(noofpackets-fail)/(double)(noofpackets)*100,totaltime); 
printf("rtt min/avg/max %f/%f/%f ms\n",mintime,totaltime/((double)noofpackets),maxtime);
exit(0);
}

int main(int argc,char* argv[])
{
if(argc<2)
{

printf("argument error\n");exit(1);}

signal(SIGINT,ping);//ping  function  is called using signal

int rsfd=socket(AF_INET,SOCK_RAW,IPPROTO_ICMP);//creating raw socket
if(rsfd==-1)
{perror("socket");exit(1);}
char buf[1000]={0};
char msg[1000]={0};
struct ip* ipheader=(struct ip*)buf;//creating IP Header
ipheader->ip_hl=5;
ipheader->ip_v=4;//version
ipheader->ip_tos=0;//type of service
ipheader->ip_len=20+8;//Header length
ipheader->ip_id=0;
ipheader->ip_off=0;
ipheader->ip_ttl=64;
ipheader->ip_p=1;
inet_pton(AF_INET,"127.0.0.2",&(ipheader->ip_src));//sorce address
inet_pton(AF_INET,argv[1],&(ipheader->ip_dst));//destination address
strcpy(pingaddr,argv[1]);
ipheader->ip_sum=csum((unsigned short *)buf,9);
int a=1;
int *one=&a;
if(setsockopt(rsfd,IPPROTO_IP,IP_HDRINCL,one,sizeof(a))<0)//setting socket option 
{perror("HDRINCL ERROR\n");
exit(1);
}

struct icmphdr *ihdr=(struct icmphdr*)(buf+20);//icmp headrer
ihdr->type=8;
ihdr->code=0;
ihdr->checksum=csum((unsigned short*)(buf+20),1);


struct sockaddr_in my,client;
my.sin_family=AF_INET;
inet_pton(AF_INET,argv[1],&(my.sin_addr));
struct timeval tvstart,tvend,timeout;
socklen_t len=sizeof(my);
timeout.tv_sec=1;
timeout.tv_usec=0;
if(setsockopt(rsfd,SOL_SOCKET,SO_RCVTIMEO,(void*)&timeout,sizeof(timeout)) < 0)
{

perror("sockopt error");exit(1);}
printf("PING %s (%s) 56(84) bytes of data.\n",argv[1],argv[1]);
while(1)
{
gettimeofday(&tvstart,NULL);
if(sendto(rsfd,buf,28,0,(struct sockaddr*)&my,sizeof(my))<0)
perror("send error");
noofpackets++;
int r;
r=recvfrom(rsfd,buf,1000,0,(struct sockaddr*)&client,&len);
gettimeofday(&tvend,NULL);
double timeelapsed=(double)(tvend.tv_sec-tvstart.tv_sec)*1000+((double)(tvend.tv_usec-tvstart.tv_usec))/1000;
totaltime+=timeelapsed;


if(r==-1)
{
fail++;
}
else
{
success++;

printf("64 bytes from %s: time=%f ms\n",argv[1],timeelapsed);
if(timeelapsed>maxtime)
maxtime=timeelapsed;
if(timeelapsed<mintime)
mintime=timeelapsed;
sleep(1);
}
}
return 0;
}


MultiThreaded MultiProcess MultiProtocol Server /Apache Server


In this program we are going to see the concept used to build apache server . That is also called MMM Server.

1.MMM Server
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<pthread.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
char mainbuf[100];
int count=1;
int tsfd[5],usfd[5];
struct sockaddr_in server_tcp,server_udp,client_tcp,client_udp;
void *tcp_service(void *arg)
{
 printf("thread\n");
 char buf[100];
 int nsfd=*(int *)arg;
 //while(1)
 //{
 recv(nsfd,buf,100,0);
 printf("msg rcvd is:%s\n",buf);
 gets(buf);
 send(nsfd,buf,100,0);
 //}
}


char buf1[100];
void *udp_service(void *arg)
{       printf("thread\n");
 int udp=*(int *)arg;
 struct sockaddr_in client1;
 int length1;
 recvfrom(udp,buf1,100,0,(struct sockaddr*)&client1,&length1);
    printf("msg received from in udp is %s\n",buf1);
 gets(buf1);
 sendto(udp,buf1,100,0,(struct sockaddr*)&client1,sizeof(client1));
}


void *fun(void *arg)

{

struct timeval tv;
tv.tv_sec=10;
tv.tv_usec=0;
fd_set rd,wr,er;
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&er);
while(1)
{
int i;
for(i=0;i<5;i++)
{
FD_SET(tsfd[i],&rd);
FD_SET(usfd[i],&rd);
}

int f=select(FD_SETSIZE,&rd,NULL,NULL,&tv);
for(i=0;i<5;i++)
{

int length;
if(FD_ISSET(tsfd[i],&rd))

{
length=sizeof(client_tcp);
int nsfd=accept(tsfd[i],(struct sockaddr *)&client_tcp,&length);
if(nsfd==-1)
{printf("accept error");exit(1);
}
pthread_t p;
   
   pthread_create(&p,NULL,&tcp_service,(void *)&nsfd);
   pthread_join(p,NULL);
  
   
   tv.tv_sec=10;
   tv.tv_usec=0;
   FD_CLR(tsfd[i],&rd);
}

if(FD_ISSET(usfd[i],&rd))
{
pthread_t p;
char buf[100];
pthread_create(&p,NULL,&udp_service,(void *)&usfd[i]);
pthread_join(p,NULL);
    
tv.tv_sec=10;
tv.tv_usec=0;
FD_CLR(usfd[i],&rd);

}

}
}
}
int main()
{int c;
int a=mkfifo("/home/gaurav/cn/assign10/msg.txt",0666);
if(a==-1)
{printf("fifo error");exit(1);}
int i,j;



for(i=0;i<5;i++)
{
tsfd[i]=socket(AF_INET,SOCK_STREAM,0);
if(tsfd[i]==-1)
{

perror("tcp socket");exit(1);
}
server_tcp.sin_family=AF_INET;
server_tcp.sin_port=htons(1500+i);
server_tcp.sin_addr.s_addr=INADDR_ANY;
if(bind(tsfd[i],(struct sockaddr *)&server_tcp,sizeof(server_tcp))==-1)
{perror("tcp bind error");exit(1);}
listen(tsfd[i],5);
usfd[i]=socket(AF_INET,SOCK_DGRAM,0);
if(usfd[i]==-1)
{
perror("udp socket");exit(1);
}
server_udp.sin_family=AF_INET;
server_udp.sin_port=htons(2000+i);
server_udp.sin_addr.s_addr=INADDR_ANY;
if(bind(usfd[i],(struct sockaddr *)&server_udp,sizeof(server_udp))==-1)
{perror("udp bind error");exit(1);}
}


for(i=0;i<10;i++)
{
c=fork();
if(c==0)
{
pthread_t p[10];
int j;
for(j=0;j<10;j++)
{
pthread_create(&p[j],NULL,&fun,&count);count=count+1;}

for(j=0;j<10;j++)
pthread_join(p[j],NULL);
}

}
if(c>0)
{

while(1)
{

int b=open("/home/gaurav/cn/assign10/msg.txt",O_RDWR);
read(b,mainbuf,100);
printf("in main process%s\n",mainbuf);
memset(mainbuf,0,100);

}
}




return 0;
}




2. TCP Client
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<netdb.h>
#include<netinet/in.h>
#include<string.h>
int main()
{
 char msg[100],msg1[100];
 int k,c;
 int sfd=socket(AF_INET,SOCK_STREAM,0);
 struct sockaddr_in client;
 client.sin_family=AF_INET;
 client.sin_addr.s_addr=INADDR_ANY;
 printf("enter the port  number u want from 1500 to 1504\n");
 scanf("%d\n",&k);
 client.sin_port=htons(k);
 connect(sfd,(struct sockaddr *)&client,sizeof(client));
  //while(1)
  //{
  printf("enter msg\n");
  
  gets(msg);
  printf("sent msg is:%s\n",msg);
  send(sfd,msg,100,0);
  recv(sfd,msg1,100,0);
  printf("msg received is %s\n",msg1);
  //}
 
 
 return 0;
}



3. UDP CLient
#include<stdlib.h>
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<pthread.h>
#include<netinet/in.h>
#include<errno.h>
#include<string.h>
int main()
{
 int length,k;
 char buf[100];
 struct sockaddr_in client,server;
 int sfd=socket(AF_INET,SOCK_DGRAM,0);
 server.sin_family=AF_INET;
 printf("enter port no in b/w 2000 to 2004 to be connected\n");
 scanf("%d\n",&k);
 server.sin_port=htons(k);
 server.sin_addr.s_addr=inet_addr("127.0.0.1");
 length=sizeof(server);
 
 //while(1)
 //{
 gets(buf);
 if(sendto(sfd,buf,100,0,(struct sockaddr *)&server,(sizeof(server)))==-1)
  printf("error %s\n",strerror(errno));
printf("msg sent :%s\n",buf);
 if(recvfrom(sfd,buf,100,0,(struct sockaddr *)&server,&length)==-1)
  printf("recv error\n");
 printf("msg receved is:%s\n",buf);
 //}
 return 0;
}
 


Internet Super Server


In This program Server Will use Select System call To manage The TCP And UDP Request From Client . See Here About Select System call This Concept is used widely used in internet . This is Called Internet Super Server

1. Server program using fork
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<sys/types.h>
#include<netdb.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<errno.h>
int main()
{
int i,j,tsfd[5],usfd[5];
struct sockaddr_in server_tcp,server_udp,client_tcp,client_udp;


for(i=0;i<5;i++)
{

tsfd[i]=socket(AF_INET,SOCK_STREAM,0);//creating 5 tcp socket
if(tsfd[i]==-1)
{

perror("tcp socket");exit(1);
}
server_tcp.sin_family=AF_INET;
server_tcp.sin_port=htons(1500+i);
server_tcp.sin_addr.s_addr=INADDR_ANY;
if(bind(tsfd[i],(struct sockaddr *)&server_tcp,sizeof(server_tcp))==-1)//binding tcp socket
{perror("tcp bind error");exit(1);}
listen(tsfd[i],5);
usfd[i]=socket(AF_INET,SOCK_DGRAM,0);//creating 5 udp socket
if(usfd[i]==-1)
{
perror("udp socket");exit(1);
}
server_udp.sin_family=AF_INET;
server_udp.sin_port=htons(2000+i);
server_udp.sin_addr.s_addr=INADDR_ANY;
if(bind(usfd[i],(struct sockaddr *)&server_udp,sizeof(server_udp))==-1)//binding udp socket
{perror("udp bind error");exit(1);}
}
struct timeval tv;
tv.tv_sec=10;
tv.tv_usec=0;
fd_set rd,wr,er;//set fd  for read write and error
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&er);
while(1)
{

for(i=0;i<5;i++)
{
FD_SET(tsfd[i],&rd);
FD_SET(usfd[i],&rd);
}

int f=select(FD_SETSIZE,&rd,NULL,NULL,&tv);//selecting tcp  or udp request  based on parameters
for(i=0;i<5;i++)
{

int length;
if(FD_ISSET(tsfd[i],&rd))//checking which tcp request is active

{
length=sizeof(client_tcp);
int nsfd=accept(tsfd[i],(struct sockaddr *)&client_tcp,&length);//accepting tcp request
if(nsfd==-1)
{printf("accept error");exit(1);
}
int k=fork();
if(k==0)
{
for(j=0;j<5;j++)
     close(tsfd[j]);

    dup2(nsfd,0);//duplicating file discriptor to read
    dup2(nsfd,1);//duplicating file discriptor to write
    execl("./s1",NULL,NULL);//calling another application  ./s1 is not added here u can add any executable program here


}
else
close(nsfd);
tv.tv_sec=10;
tv.tv_usec=0;
FD_CLR(tsfd[i],&rd);

}

if(FD_ISSET(usfd[i],&rd))//checking which udp request came
{length=sizeof(client_udp);
int k=fork();
   if(k==0)
   {
    char buf[100];
    recvfrom(usfd[i],buf,100,0,(struct sockaddr*)&client_udp,&length);
    printf("msg received from %d in udp is %s\n",i,buf);
gets(buf);
sendto(usfd[i],buf,100,0,(struct sockaddr*)&client_udp,length);

   }
   tv.tv_sec=10;
   tv.tv_usec=0;
   FD_CLR(usfd[i],&rd);

}

else
{
printf("NO DATA AVAILABLE in recent 10 seconds\n");
                   tv.tv_sec = 10;
                   tv.tv_usec = 0;

}
}
}
return 0;
}



2. Server program using Pthread //light weight process

#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<sys/types.h>
#include<netdb.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<arpa/inet.h>
#include<errno.h>

void *tcp_service(void *arg)//thread for tcp service
{
 printf("thread\n");
 char buf[100];
 int nsfd=*(int *)arg;
 //while(1)
 //{
 recv(nsfd,buf,100,0);
 printf("msg rcvd is:%s\n",buf);
 gets(buf);
 send(nsfd,buf,100,0);
 //}
}


char buf1[100];
void *udp_service(void *arg)//thread for udp service
{       printf("thread\n");
 int udp=*(int *)arg;
 struct sockaddr_in client1;
 int length1;
 recvfrom(udp,buf1,100,0,(struct sockaddr*)&client1,&length1);
    printf("msg received from in udp is %s\n",buf1);
 gets(buf1);
 sendto(udp,buf1,100,0,(struct sockaddr*)&client1,sizeof(client1));
}





int main()
{
int i,j,tsfd[5],usfd[5];
struct sockaddr_in server_tcp,server_udp,client_tcp,client_udp;


for(i=0;i<5;i++)
{

tsfd[i]=socket(AF_INET,SOCK_STREAM,0);//creating 5 tcp socket
if(tsfd[i]==-1)
{

perror("tcp socket");exit(1);
}
server_tcp.sin_family=AF_INET;
server_tcp.sin_port=htons(1500+i);
server_tcp.sin_addr.s_addr=INADDR_ANY;
if(bind(tsfd[i],(struct sockaddr *)&server_tcp,sizeof(server_tcp))==-1)//binding tcp socket
{perror("tcp bind error");exit(1);}
listen(tsfd[i],5);
usfd[i]=socket(AF_INET,SOCK_DGRAM,0);//creating 5 udp socket
if(usfd[i]==-1)
{
perror("udp socket");exit(1);
}
server_udp.sin_family=AF_INET;
server_udp.sin_port=htons(2000+i);
server_udp.sin_addr.s_addr=INADDR_ANY;
if(bind(usfd[i],(struct sockaddr *)&server_udp,sizeof(server_udp))==-1)//binding udp socket
{perror("udp bind error");exit(1);}
}
struct timeval tv;
tv.tv_sec=10;
tv.tv_usec=0;
fd_set rd,wr,er;
FD_ZERO(&rd);
FD_ZERO(&wr);
FD_ZERO(&er);
while(1)
{

for(i=0;i<5;i++)
{
FD_SET(tsfd[i],&rd);
FD_SET(usfd[i],&rd);
}

int f=select(FD_SETSIZE,&rd,NULL,NULL,&tv);//select system call
for(i=0;i<5;i++)
{

int length;
if(FD_ISSET(tsfd[i],&rd))//checking which tcp  is active

{
length=sizeof(client_tcp);
int nsfd=accept(tsfd[i],(struct sockaddr *)&client_tcp,&length);
if(nsfd==-1)
{printf("accept error");exit(1);
}
pthread_t p;
   
   pthread_create(&p,NULL,&tcp_service,(void *)&nsfd);//creating tcp thread
   pthread_join(p,NULL);
  
   
   tv.tv_sec=10;
   tv.tv_usec=0;
   FD_CLR(tsfd[i],&rd);
}

if(FD_ISSET(usfd[i],&rd))//checking which udp is active
{
pthread_t p;
char buf[100];
pthread_create(&p,NULL,&udp_service,(void *)&usfd[i]);//creating udp thread
pthread_join(p,NULL);
    
tv.tv_sec=10;
tv.tv_usec=0;
FD_CLR(usfd[i],&rd);

}

}
}
return 0;
}



3. TCP Client
#include<sys/socket.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<netdb.h>
#include<netinet/in.h>
#include<string.h>
int main()
{
 char msg[100],msg1[100];
 int k,c;
 int sfd=socket(AF_INET,SOCK_STREAM,0);
 struct sockaddr_in client;
 client.sin_family=AF_INET;
 client.sin_addr.s_addr=INADDR_ANY;
 printf("enter the port  number u want from 1500 to 1504\n");
 scanf("%d\n",&k);
 client.sin_port=htons(k);
 connect(sfd,(struct sockaddr *)&client,sizeof(client));
  //while(1)
  //{
  printf("enter msg\n");
  
  gets(msg);
  printf("sent msg is:%s\n",msg);
  send(sfd,msg,100,0);
  recv(sfd,msg1,100,0);
  printf("msg received is %s\n",msg1);
  //}
 
 
 return 0;
}



4. UDP Clint
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
 int length,k;
 char buf[100];
 struct sockaddr_in client,server;
 int sfd=socket(AF_INET,SOCK_DGRAM,0);
 server.sin_family=AF_INET;
 printf("enter port no in b/w 2000 to 2004 to be connected\n");
 scanf("%d\n",&k);
 server.sin_port=htons(k);
 server.sin_addr.s_addr=inet_addr("127.0.0.1");
 length=sizeof(server);
 
 while(1)
 {
 gets(buf);
 if(sendto(sfd,buf,100,0,(struct sockaddr *)&server,(sizeof(server)))==-1)
  printf("error %s\n",strerror(errno));
printf("msg sent :%s\n",buf);
 if(recvfrom(sfd,buf,100,0,(struct sockaddr *)&server,&length)==-1)
  printf("recv error\n");
 printf("msg receved is:%s\n",buf);
 }
 return 0;
}
 


TCP/UDP Socket programming


This is a Multi Protocal Socket programming where server will have tcp as well as udp request . It will manage both request using poll system call .

1. multiserver programe
#include<stdio.h>
#include<unistd.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<poll.h>


int main()
{
struct pollfd p[2];

int tsfd,usfd,nsfd,tcpsize,udpsize;
int pstatus;
struct sockaddr_in tcpserver,udpserver,tcpclient,udpclient;
tsfd=socket(AF_INET,SOCK_STREAM,0);//tcp socket
if(tsfd==-1)
{printf("socket error");exit(1);}
usfd=socket(AF_INET,SOCK_DGRAM,0);//udp socket
if(usfd==-1)
{printf("socket error");exit(1);}
bzero(&tcpserver,sizeof(tcpserver));
tcpserver.sin_family=AF_INET;
tcpserver.sin_addr.s_addr=INADDR_ANY;
tcpserver.sin_port=htons(1500);
if(bind(tsfd,(struct sockaddr *)&tcpserver,sizeof(tcpserver))==-1)//tcp bind
{printf("bind error\n");exit(1);}
udpserver.sin_family=AF_INET;
udpserver.sin_addr.s_addr=INADDR_ANY;
udpserver.sin_port=htons(2000);
if(bind(usfd,(struct sockaddr *)&udpserver,sizeof(udpserver))==-1)//udp bind
{printf("bind error\n");exit(1);}
listen(tsfd,5);
p[0].events=POLLIN;
p[1].events=POLLIN;
p[0].fd=tsfd;
p[1].fd=usfd;
while(1)
{

pstatus=poll(p,2,2000);//polling tcp and udp  connection

if(pstatus>0)
{
if(p[0].revents&POLLIN)//tcp request
{

tcpsize=sizeof(tcpclient);
nsfd=accept(tsfd,(struct sockaddr*)&tcpclient,&tcpsize);//accepting tcp connection
int c=fork();//creating chield for each tcp client
if(c==0)
{
close(tsfd);
char tcpbuf[100];
while(1)
{

recv(nsfd,tcpbuf,100,0);
printf("tcp client says\t%s\n",tcpbuf);
printf("message\n");
memset(tcpbuf,0,100);
gets(tcpbuf);
send(nsfd,tcpbuf,100,0);
memset(tcpbuf,0,100);
}
}
else if(c>0)
close(nsfd);
}
if(p[1].revents&POLLIN)//udp request

{
udpsize=sizeof(udpclient);
char udpbuf[100];
memset(udpbuf,0,100);

recvfrom(usfd,udpbuf,100,0,(struct sockaddr*)&udpclient,&udpsize);
printf("message from %s with port no %d is %s\n",inet_ntoa(udpclient.sin_addr),ntohs(udpclient.sin_port),udpbuf);
memset(udpbuf,0,100);
printf("messge is\n");
gets(udpbuf);
sendto(usfd,udpbuf,100,0,(struct sockaddr *)&udpclient,udpsize);
}


}

}
return 0;
}




2. TCP clint program
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
int main()
{
char buf[100];
int sfd;
sfd=socket(AF_INET,SOCK_STREAM,0);//tcp socket
struct sockaddr_in client;
bzero(&client,sizeof(client));
client.sin_family=AF_INET;
client.sin_addr.s_addr=inet_addr("127.0.0.1");
client.sin_port=htons(1500);

connect(sfd,(struct sockaddr*)&client,sizeof(client));//connecting to server
while(1)
{
memset(buf,0,100);
printf("message\n");
gets(buf);
send(sfd,buf,100,0);
memset(buf,0,100);
recv(sfd,buf,100,0);
printf("server said\t%s\n",buf);
}


return 0;
}




3. UDP clint program
#include<stdio.h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<stdlib.h>
#include<string.h>
#include<netdb.h>

int main()
{
char buf[100];
int sfd,size;
sfd=socket(AF_INET,SOCK_DGRAM,0);//udp socket
if(sfd==-1)
{
printf("error in socket");exit(1);}

struct sockaddr_in server,client;
server.sin_family=AF_INET;
server.sin_addr.s_addr=inet_addr("127.0.0.1");
server.sin_port=htons(1500);
size=sizeof(client);
while(1)
{memset(buf,0,100);
printf("your message \n");
gets(buf);

sendto(sfd,buf,100,0,(struct sockaddr *)&server,sizeof(server));

memset(buf,0,100);

recvfrom(sfd,buf,100,0,(struct sockaddr *)&client,&size);
printf("%s\n",buf);
}
return 0;
}