forked from sidhartha1384/DLMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSocket.c
188 lines (146 loc) · 4.38 KB
/
Socket.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<stdio.h> //for standard I/O
#include<unistd.h> //defines miscellaneous symbolic constants and types, and declares miscellaneous functions.
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include <errno.h>
#include<signal.h>
#include<stdlib.h>
#include <pthread.h>
int PORT;
char line[256];
char PORT_NUMBER[100];
FILE *fc1;
struct sockaddr_in servAddr, client_addr;
unsigned short sin_port;
int socket_desc, client_sock, c, *new_sock,last_fd;
int sock_fd;
int address, address1, data, data1, ader;
void *connection_handler (void *);
void sigintHandler (int sig_num);
/*
* Socket_Creation of the Program
*/
int Socket_Creation(){
int cnt = 1; //To know NO of Connections
/*
* Signal for Termination of the Program
*/
signal(SIGINT, sigintHandler);
if (signal (SIGINT, sigintHandler) == SIG_ERR)
{
printf("\n :::: UNABLE TO TERMINATE THE PROGRAM ! TRY AGAIN :::: \n");
}
/*
* Opening the FILE for PORT Number
*/
// sprintf(PORT_NUMBER,"%s/port_no.dat",(char*)getenv("TEST"));
//if ((fc1 = fopen (PORT_NUMBER,"r")) == NULL) //opening particular dat file
if ((fc1 = fopen ("port_no.dat","r")) == NULL) //opening particular dat file
{
printf("\n :::: *** port_no.dat *** FILE DOES NOT EXIST, PLEASE CONFIGURE THE FILE *** :::: \n");
return 0;
}
if ((fgets(line, sizeof (line), fc1)) == NULL)
{
perror("\n::::PORT_NO NOT CONFIGURE :::: \n");
}
sscanf(line, "%d", &PORT);
#ifdef DEBUG
printf("\n :::: PORT is: %d ::::\n",PORT);
#endif
fclose(fc1);
/*
*CREATING THE SOCKET WITH NAME::::: socket_desc
*/
socket_desc = socket (AF_INET, SOCK_STREAM, 0);
if(socket_desc < 0)
{
printf("\n :::: FAILED TO CREATE THE SOCKET :::: \n");
exit (0);
}
sock_fd = socket_desc;
/*
*ASSIGNING THE IP AND PORT
*/
memset(&servAddr, 0, sizeof (servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = INADDR_ANY;
servAddr.sin_port = htons (PORT);
/*
*BINDING THE SOCKET TO THE PORT
*/
if (bind(socket_desc, (struct sockaddr *) &servAddr,sizeof (struct sockaddr)) == -1)
{
printf("\n :::: UNABLE TO BIND THE PORT: %d :::: \n",PORT);
exit(1);
}
#ifdef DEBUG
printf("\n :::: Bind Done Successfully with the Port: %d :::: \n",PORT);
#endif
/*
*LISTEN TO ACCEPTED CONNECTIONS
*/
if (listen (socket_desc, 100) != 0)
{
printf("\n :::: Unable to Listen to the the Socket Connected :::: \n");
}
printf("\n :::: SLDC Server Waiting for connection at port: %d at :%s:::: \n", PORT,__DATE__);
fflush(stdout);
c = sizeof(struct sockaddr_in);
while(1)
{
/*
*ACCEPT THE ALL THE INCOMING CONNECTIONS
*/
client_sock =accept(socket_desc, (struct sockaddr *) &client_addr, &c);
if (client_sock < 0)
{
printf("\n:::: Error in Accepting the Connection ::::\n");
exit(1);
}
fprintf(stderr,"\n:::: Waiting For Meter Connection :::: \n");
fprintf(stderr,"\n:::: Connection from Meter SBC with IP: %s and PORT: %d :::: \n",inet_ntoa (client_addr.sin_addr),ntohs (client_addr.sin_port));
fprintf(stderr,"\n:::: Connection Established Successfully :::: \n");
/*
*CREATING THE THREAD PROCESS FOR EACH AND EVERY CONNECTION
*/
pthread_t sniffer_thread;
new_sock = malloc (1);
*new_sock = client_sock;
/*
* Calling the Function For New Thread Created
*/
if(pthread_create(&sniffer_thread, NULL, connection_handler ,(void *) new_sock) == -1)
{
perror("\n :::: Unable to Create Threads for accepting the ABT Meter Connections :::: \n");
return -1;
}
fprintf(stderr, "\n:::: ABT Meter Connection Connected: %d ::::\n",cnt);
fflush (stdout);
cnt++;
} /*** END OF WHILE LOOP ***/
return 0;
}
/*******************************
* 5.FUNCTION FOR CODE TERMINATION*
*******************************/
void sigintHandler (int sig_num)
{
if (sig_num == SIGINT)
{
printf("\n:::: CONNECTION TERMINATED BY USING Ctrl+C SIGNAL \n");
fflush(stdout);
if(client_sock>0){
close(client_sock);
printf("\n :::: CLOSING THE CLIENT SOCKET :::: \n");
sleep(1);
}
close(socket_desc);
printf("\n:::: CLOSING THE SERVER SOCKET :::: \n");
sleep(2);
exit(0);
}
}