-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrobot-server.c
More file actions
175 lines (137 loc) · 5 KB
/
robot-server.c
File metadata and controls
175 lines (137 loc) · 5 KB
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
/*
*****************************************************************************************
*
* ===============================================
* Rapid Rescuer (RR) Theme (eYRC 2019-20)
* ===============================================
*
* This script is to implement Task 1B of Rapid Rescuer (RR) Theme (eYRC 2019-20).
*
* This software is made available on an "AS IS WHERE IS BASIS".
* Licensee/end user indemnifies and will keep e-Yantra indemnified from
* any and all claim(s) that emanate from the use of the Software or
* breach of the terms of this agreement.
*
* e-Yantra - An MHRD project under National Mission on Education using ICT (NMEICT)
*
*****************************************************************************************
*/
/*
# Team ID: [ 3478 ]
# Author List: [ Jyothis P, Arun Padmanabhan, Ebin Santy, Jithin K Satheesh ]
* Filename: robot-server.c
* Functions: socket_create, receive_from_send_to_client
* [ Comma separated list of functions in this file ]
* Global variables: SERVER_PORT, RX_BUFFER_SIZE, TX_BUFFER_SIZE, MAXCHAR,
* dest_addr, source_addr, rx_buffer, tx_buffer,
* ipv4_addr_str, ipv4_addr_str_client, listen_sock, line_data, input_fp, output_fp
* [ List of global variables defined in this file ]
*/
// Include necessary header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
// Constants defined
#define SERVER_PORT 3333
#define RX_BUFFER_SIZE 1024
#define TX_BUFFER_SIZE 1024
#define MAXCHAR 1000 // max characters to read from txt file
// Global variables
struct sockaddr_in dest_addr;
struct sockaddr_in source_addr;
char rx_buffer[RX_BUFFER_SIZE]; // buffer to store data from client
char tx_buffer[RX_BUFFER_SIZE]; // buffer to store data to be sent to client
char ipv4_addr_str[128]; // buffer to store IPv4 addresses as string
char ipv4_addr_str_client[128]; // buffer to store IPv4 addresses as string
int listen_sock;
char line_data[MAXCHAR];
FILE *input_fp, *output_fp;
/*
* Function Name: socket_create
* Inputs: dest_addr [ structure type for destination address ]
* source_addr [ structure type for source address ]
* Outputs: my_sock [ socket value, if connection is properly created ]
* Purpose: the function creates the socket connection with the server
* Example call: int sock = socket_create(dest_addr, source_addr);
*/
int socket_create(struct sockaddr_in dest_addr, struct sockaddr_in source_addr){
int addr_family;
int ip_protocol;
int opt = 1;
int new_socket;
dest_addr.sin_addr.s_addr = htonl(INADDR_ANY);
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(SERVER_PORT);
addr_family = AF_INET;
int addrlen = sizeof(dest_addr);
ip_protocol = IPPROTO_IP;
int my_sock;
my_sock = socket(addr_family, SOCK_STREAM, ip_protocol);
setsockopt(my_sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt));
bind(my_sock, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
listen(my_sock, 3);
printf("Server running at 3333\n");
new_socket = accept(my_sock, (struct sockaddr *)&dest_addr, (socklen_t*)&addrlen);
printf("Connection established with the client.\n");
return new_socket;
}
/*
* Function Name: receive_from_send_to_client
* Inputs: sock [ socket value, if connection is properly created ]
* Outputs: None
* Purpose: the function receives the data from server and updates the 'rx_buffer'
* variable with it, sends the obstacle position based on obstacle_pos.txt
* file and sends this information to the client in the provided format.
* Example call: receive_from_send_to_client(sock);
*/
int receive_from_send_to_client(int sock){
char *hello = "@(4,7)@";
int valread;
valread = read( sock , rx_buffer, RX_BUFFER_SIZE);
// printf("%d\n",valread );
if (valread > 0)
{
printf("%s\n", rx_buffer);
send(sock , hello , strlen(hello) , 0 );
printf("obstacle sent\n");
}
return 0;
}
/*
* Function Name: main()
* Inputs: None
* Outputs: None
* Purpose: the function solves Task 1B problem statement by making call to
* functions socket_create() and receive_from_send_to_client()
*/
int main() {
char *input_file_name = "obstacle_pos.txt";
char *output_file_name = "data_from_client.txt";
// Create socket and accept connection from client
int sock = socket_create(dest_addr, source_addr);
input_fp = fopen(input_file_name, "r");
if (input_fp == NULL){
printf("Could not open file %s\n",input_file_name);
return 1;
}
fgets(line_data, MAXCHAR, input_fp);
output_fp = fopen(output_file_name, "w");
if (output_fp == NULL){
printf("Could not open file %s\n",output_file_name);
return 1;
}
while (1) {
// Receive and send data from client and get the new shortest path
receive_from_send_to_client(sock);
// NOTE: YOU ARE NOT ALLOWED TO MAKE ANY CHANGE HERE
fputs(rx_buffer, output_fp);
fputs("\n", output_fp);
}
return 0;
}