Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Shell commands udp server

/*shell commands udp server*/


#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAX 1024
main()
{

int sockfd,n,len;

char buff[MAX];
struct sockaddr_in ser_addr,cli_addr;
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("sockert error");
exit(1);
}
ser_addr.sin_family=AF_INET;
ser_addr.sin_port=htons(7888);
ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr*)&ser_addr,sizeof(ser_addr))<0)
{
perror("bind error");
exit(1);
}
printf("Server started and wating\n");
fflush(stdout);
len=sizeof(cli_addr);
while(1)
{
n=recvfrom(sockfd,buff,1024,0,(struct sockaddr*)&cli_addr,&len);
system(buff);
printf("%s \n",buff);
fflush(stdout);
sendto(sockfd,buff,n,0,(struct sockaddr*)&cli_addr,len);
fflush(stdout);
}
if((system(buff))<0)

{
printf("\nServer:Command cannot be executed\n");
exit(1);
}
close(sockfd);
}

Shell commands udp client


/*shell commands udp client*/
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAX 1024
main()
{

int sockfd,n,len;

char buff[MAX];
struct sockaddr_in ser_addr,cli_addr;
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("sockert error");
exit(1);
}
ser_addr.sin_family=AF_INET;
ser_addr.sin_port=htons(7888);
ser_addr.sin_addr.s_addr=htonl(INADDR_ANY);
if(bind(sockfd,(struct sockaddr*)&ser_addr,sizeof(ser_addr))<0)
{
perror("bind error");
exit(1);
}
printf("Server started and wating\n");
fflush(stdout);
len=sizeof(cli_addr);
while(1)
{
n=recvfrom(sockfd,buff,1024,0,(struct sockaddr*)&cli_addr,&len);
system(buff);
printf("%s \n",buff);
fflush(stdout);
sendto(sockfd,buff,n,0,(struct sockaddr*)&cli_addr,len);
fflush(stdout);
}
if((system(buff))<0)

{
printf("\nServer:Command cannot be executed\n");
exit(1);
}
close(sockfd);
}

You might also like