-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc-php.c
109 lines (79 loc) · 2.2 KB
/
c-php.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// C program to connect to the socket opened by php module
// written by -- kartik
#include<stdio.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<malloc.h>
#include"myfunc.h"
//#include<conio.h>
//max buffer size
#define buffer_size 100
// main function
// takes port number as argument
int main(int argc , char *argv[])
{
if(argc<2)
{
printf("Error : pass the argument <address> <port number>\n");
exit(EXIT_FAILURE);
}
int listenfd=0,connfd=0;
struct sockaddr_in serv_addr; // creating variable of data type 'struct sockaddr_in'
// create a memory buffer
char buffer[buffer_size];
char *send_ack="your message received\n";
// create socket
listenfd=socket(AF_INET, SOCK_STREAM,0);
if(!listenfd)
{
printf("Error : Scoket failed\n");
exit(EXIT_FAILURE);
}
memset(&serv_addr, '0', sizeof(serv_addr));
memset(buffer, '0', sizeof(buffer));
serv_addr.sin_family=AF_INET;
//server address
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); //listen to any internal address
// set port number
int port=atoi(argv[1]);
serv_addr.sin_port=htons(port);
//bind address
if(bind(listenfd,(struct sockaddr*)&serv_addr, sizeof(serv_addr))<0)
{
printf("Error : Binding failed\n");
// free(buffer);
exit(EXIT_FAILURE);
}
//listen to 1 php script
listen(listenfd,1);
while(1)
{
printf("Server started\n");
connfd=accept(listenfd,(struct sockaddr *)NULL,NULL);
//get the input data from php
int byteRead=read(connfd,buffer,buffer_size);
//trim the garbage value
buffer[byteRead]='\0';
//check received data is exit ? if yes , shutdown the server
if((strcmp(buffer,"exit"))==0)
{
write(connfd,"server close",sizeof("server close"));
printf("Server shutdown\n");
close(connfd);
exit(EXIT_SUCCESS);
}
printf("Call to myfunction\n");
//call to user-defined function present in myfunc.h
myfunc(buffer);
printf("return from myfunction\n\n");
//write the computed data back to php
write(connfd,send_ack,strlen(send_ack));
close(connfd); // close the connection
sleep(1);
}
exit(EXIT_SUCCESS);
}