-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUDP_server_independent.c
178 lines (149 loc) · 4.56 KB
/
UDP_server_independent.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
/*
* =====================================================================================
*
* Filename: UDP_server.c
*
* Description: Code to create a UDP server built on top of/ by https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=3&ved=2ahUKEwjC9YiX0ZPhAhUJ8HMBHYzIDeQQFjACegQIBBAB&url=https%3A%2F%2Fwww.cs.cmu.edu%2Fafs%2Fcs%2Facademic%2Fclass%2F15213-f99%2Fwww%2Fclass26%2Fudpserver.c&usg=AOvVaw1ZwUkY8jZDhz-iq9Xp8VEG
*
* Version: 1.0
* Created: Thursday 21 March 2019 08:02:35 IST
* Revision: none
* Compiler: gcc
* Original Autthor : cs.cmu.edu
* Modified by: Aswin P Ajayan (), [email protected]
* Organization: iitb
*
*
* =====================================================================================
*/
/*udp server.c - A simple UDP echo server
* usage: udpserver <port>
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#define CMD_SIZE 128
#define BUFSIZE 256
#define PACKET_SIZE 512
#define PORT_NUMBER 50001
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
exit(1);
}
/* shared buffered locked through the mutex*/
unsigned short in_buf[BUFSIZE]; /* message buf data from MCU*/
char out_buf[CMD_SIZE]; /*control messages from UI*/
pthread_mutex_t lock_buf = PTHREAD_MUTEX_INITIALIZER;
pthread_t server_t_id;
void* socketThread(void *arg){
unsigned char recv_buf[PACKET_SIZE];
int sockfd; /* socket */
int portno; /* port to listen on */
unsigned int clientlen; /* byte size of client's address */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
//struct hostent *hostp; /* client host info */
//struct addrinfo *host_info;
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
int n; /* message byte size */
int i,j;
/*FILE IO*/
FILE *out_fp;
/*
* check command line arguments
*/
portno = atoi((char*)arg);
/*
* socket: create the parent socket
*/
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/* setsockopt: Handy debugging trick that lets
* us rerun the server immediately after we kill it;
* otherwise we have to wait about 20 secs.
* Eliminates "ERROR on binding: Address already in use" error.
*/
optval = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
/*
* build the server's Internet address
*/
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons((unsigned short)portno);
/*
* bind: associate the parent socket with a port
*/
if (bind(sockfd, (struct sockaddr *) &serveraddr,
sizeof(serveraddr)) < 0)
error("ERROR on binding");
/*
* main loop: wait for a datagram, then echo it
*/
clientlen = sizeof(clientaddr);
while (1) {
/*
* recvfrom: receive a UDP datagram from a client
*/
bzero(recv_buf, PACKET_SIZE);
n = recvfrom(sockfd, recv_buf, PACKET_SIZE, 0,
(struct sockaddr *) &clientaddr, &clientlen);
if (n < 0)
error("ERROR in recvfrom");
pthread_mutex_lock(&lock_buf);
bzero(in_buf,BUFSIZE);
// memcpy(in_buf,recv_buf,sizeof(recv_buf)+1);
bzero(in_buf,BUFSIZE);
hostaddrp = inet_ntoa(clientaddr.sin_addr);
printf("server received %ld/%d bytes \n", sizeof(recv_buf), n);
out_fp = fopen("./inputdata.txt","w+");
if(out_fp== NULL)
printf("error fatal");
j=0;
for(i = 0 ; i < PACKET_SIZE ; i++){
in_buf[j] = (recv_buf[i+1]<<8) & 0x000000300;
in_buf[j] = (in_buf[j]) | (recv_buf[i] & 0x0000FF);
fprintf(out_fp,"%u\n",in_buf[j]);
i++;
j++;
}
fclose(out_fp);
pthread_mutex_unlock(&lock_buf);
/*
* sendto: echo the input back to the client
*/
// n = sendto(sockfd, out_buf, strlen(out_buf), 0,
// (struct sockaddr *) &clientaddr, clientlen);
// if (n < 0)
// error("ERROR in sendto");
}
pthread_exit(NULL);
}
int main(int argc, char **argv) {
//test initialisation of command buffer
memcpy(out_buf,"TEST_COMMAND",12);
/*
* check command line arguments
*/
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
pthread_create(&server_t_id,NULL,&socketThread,argv[1]);
pthread_join(server_t_id,NULL);
}