-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp_server.c~
216 lines (181 loc) · 5.61 KB
/
udp_server.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @author Jennifer Norby
* @version 11/5/2019
* udp_server.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
/*
* Definition of constants
*/
#define MAGIC_1 0x4A // ascii for J
#define MAGIC_2 0x4E // ascii for N
#define OPCODE_POST 0x01 // post
#define OPCODE_PACK 0x02 // post ack
#define OPCODE_RETR 0x03 // retrieve
#define OPCODE_RACK 0x04 // retrieve ack
// constants indicating opcodes from the supplied code
#define OPCODE_RESET 0x00
#define OPCODE_MUST_LOGIN_FIRST_ERROR 0xF0
#define OPCODE_LOGIN 0x10
// Constants indicating states, nothing to do with client state
#define STATE_OFFLINE 0
#define STATE_ONLINE 1
#define STATE_MSG_FORWARD 2
// define more states in similar way
// event constants, have nothing to do with states of client
#define EVENT_NET_LOGIN 80
#define EVENT_NET_POST 81
// define more events from network
#define EVENT_NET_INVALID 255
/*
* struct of the header
*/
struct header {
char magic1;
char magic2;
char opcode;
char payload_len;
uint32_t token;
uint32_t msg_id;
};
const int h_size = sizeof(struct header);
/*
* struct of the session
*/
struct session {
char client_id[32];
struct sockaddr_in client_addr;
time_t last_time;
uint32_t token;
int state;
// TODO: You may need to add more info such as subscription list & password
};
/*
* Start of main function
*/
int main() {
FILE *fp;
char buff[255];
// an initilization of the file, creates it if it doesn't exist
fp = fopen("/tmp/test.txt", "w+");
fgets(buff, 255, (FILE*)fp);
fclose(fp);
char opcode;
int ret;
int sockfd;
struct sockaddr_in servaddr, cliaddr;
char recv_buffer[1024];
char send_buffer[1024];
char retrv_msg[201];
char *retstr;
int recv_len;
char recent_msg[201];
socklen_t len;
// may need to use std::map to hold ass the sessions
// just using an array for demonstration, assuming we are dealing with
// at most 16 clients, essentially the database
struct session session_array[16];
// now have to load all users info and fill this array, or hardcode
// current_session is a variable temporarily hold the session upon an event
struct session *current_session;
int token;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0) {
printf("socket() error: %s.\n", strerror(errno));
return -1;
}
// The servaddr is the address and port number that the server will
// keep receiving from.
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(32000);
bind(sockfd,
(struct sockaddr *) &servaddr,
sizeof(servaddr));
// same as in the client code:
struct header *ph_send = (struct header *)send_buffer;
struct header *ph_recv = (struct header *)recv_buffer;
while (1) {
fflush(fp);
int m = 0; // initialization of m
len = sizeof(cliaddr); // size of the client address
recv_len = recvfrom(sockfd, // socket file descriptor
recv_buffer, // receive buffer
sizeof(recv_buffer), // max number of bytes to be received
0,
(struct sockaddr *) &cliaddr, // client address
&len); // length of client address structure
// start of receive handling
if (recv_buffer[0] != MAGIC_1 || recv_buffer[1] != MAGIC_2) {
// Bad message!!! Skip this iteration.
puts("error! bad message server!");
continue;
} else {
if (recv_buffer[2] == OPCODE_POST) {
m=0; // since only an ack is being sent, setting m to 0
opcode = OPCODE_PACK; // setting opcode to post ack val
time_t now = time(NULL); // for my timestamp
char *timeStr = ctime(&now);
memset(recent_msg, 0, sizeof(recent_msg));
memcpy(recent_msg, recv_buffer + 4, recv_buffer[3]);
// prints to console
printf("[%s] %s",
inet_ntoa(cliaddr.sin_addr),
recv_buffer+4);
// write to file
fp = fopen("/tmp/test.txt", "w+"); // opens the text file
fprintf(fp, "<%.24s> [%s:%d] post#%s\n",
ctime(&now),
inet_ntoa(cliaddr.sin_addr),
cliaddr.sin_port,
recv_buffer+4);
fflush(fp);
fclose(fp);
} else if (recv_buffer[2] == OPCODE_RETR) {
opcode = OPCODE_RACK; // sets the opcode to retrieve ack
// reads in the information from the text file
fp = fopen("/tmp/test.txt", "r");
fgets(retrv_msg, 255, (FILE*)fp);
// retstr is set to only the text AFTER the #
retstr = strchr(retrv_msg, '#') + 1;
m = strlen(retstr);
memcpy(send_buffer+4, retstr, m);
fclose(fp);
} else {
puts("wrong message format, skip");
// Wrong message format. Skip this iteration.
continue;
}
}
if (recv_len <= 0) {
printf("recvfrom() error: %s.\n", strerror(errno));
return -1;
}
// You are supposed to call the sendto() function here to send back
// the echoed message, using "cliaddr" as the destination address.
if(recv_len>0){
send_buffer[0] = MAGIC_1; // These are constants I define
send_buffer[1] = MAGIC_2;
send_buffer[2] = opcode;
send_buffer[3] = m;
sendto(sockfd, // the socket file descriptor
send_buffer, // the sending buffer
sizeof(send_buffer), // the number of bytes you want to send
0,
(struct sockaddr *) &cliaddr, // client address
sizeof(cliaddr)); // length of client address
}
}
fflush(fp);
close(sockfd);
return 0;
}