Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
apsrcreatix
GitHub Repository: apsrcreatix/Socket-Programming-With-C
Path: blob/master/09-remote-command-execution-udp/server.c
137 views
1
/*
2
* Title : Remote command execution using UDP
3
* Name : Aditya Pratap Singh Rajput
4
* Subject : Network Protocols And Programming using C
5
*
6
Note : please consider the TYPOS in comments.
7
Thanks.
8
*/
9
#include <sys/types.h>
10
#include <sys/socket.h>
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <netdb.h>
14
#include <netinet/in.h>
15
#include <string.h>
16
#include <sys/stat.h>
17
#include <arpa/inet.h>
18
#include <unistd.h>
19
#define MAX 1000
20
int main()
21
{
22
23
int serverDescriptor = socket(AF_INET, SOCK_DGRAM, 0);
24
int size;
25
char buffer[MAX], message[] = "Command Successfully executed !";
26
struct sockaddr_in clientAddress, serverAddress;
27
28
socklen_t clientLength = sizeof(clientAddress);
29
30
bzero(&serverAddress, sizeof(serverAddress));
31
serverAddress.sin_family = AF_INET;
32
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
33
serverAddress.sin_port = htons(9976);
34
35
bind(serverDescriptor, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
36
while (1)
37
{
38
bzero(buffer, sizeof(buffer));
39
recvfrom(serverDescriptor, buffer, sizeof(buffer), 0, (struct sockaddr *)&clientAddress, &clientLength);
40
system(buffer);
41
printf("Command Executed ... %s ", buffer);
42
sendto(serverDescriptor, message, sizeof(message), 0, (struct sockaddr *)&clientAddress, clientLength);
43
}
44
close(serverDescriptor);
45
return 0;
46
}
47