-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetServer.c
163 lines (135 loc) · 5.19 KB
/
tweetServer.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
/*///////////////////////////////////////////////////////////
*
* FILE: tweetServer.c
* AUTHOR: Reid Harrison
* PROJECT: CS 3251 Project 1
* DESCRIPTION: TTweet Server
* REFERENCE: TCP/IP Sockets in C (Donahoo and Calvert)
*
*////////////////////////////////////////////////////////////
/*Included libraries*/
#include <stdio.h> /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h> /* for sockaddr_in and inet_addr() */
#include <stdlib.h> /* supports all sorts of functionality */
#include <unistd.h> /* for close() */
#include <string.h> /* support any string ops */
#include "tweet.h"
#define RCVBUFSIZE 512 /* The receive buffer size */
#define SNDBUFSIZE 512 /* The send buffer size */
#define BUFSIZE 128 /* Your name can be as many as 40 chars*/
struct list *eatList;
struct list *sleepList;
struct list *networkingList;
/* The main function */
int main(int argc, char *argv[]) {
int serverSock; /* Server Socket */
int clientSock; /* Client Socket */
struct sockaddr_in servAddr; /* Local address */
struct sockaddr_in clntAddr; /* Client address */
int servPort; /* Server port */
unsigned int clntLen; /* Length of address data struct */
eatList = makeList();
sleepList = makeList();
networkingList = makeList();
char tweetBuf[RCVBUFSIZE]; /* Buff to store tweet */
char responseBuf[SNDBUFSIZE];
char *command;
char *subject;
char *newTweet;
puts("Server starting...");
servPort = atoi(argv[1]);
/* Create new TCP Socket for incoming requests*/
if((serverSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
exit(1);
/* Construct local address structure*/
memset(&servAddr, 0, sizeof(servAddr));
servAddr.sin_family = AF_INET;
servAddr.sin_addr.s_addr = htonl(INADDR_ANY);
servAddr.sin_port = htons(servPort);
/* Bind to local address structure */
if(bind(serverSock, (struct sockaddr*) &servAddr, sizeof(servAddr)) < 0) {
puts("Unable to bind socket to address.");
exit(1);
}
/* Listen for incoming connections */
if(listen(serverSock, 5) < 0) {
puts("Received bad data from socket.");
exit(1);
}
/* Loop server forever*/
while(1) {
clntLen = sizeof(clntAddr);
/* Accept incoming connection */
if ((clientSock = accept(serverSock, (struct sockaddr*) &clntAddr, &clntLen)) < 0) {
exit(1);
}
char clntName[INET_ADDRSTRLEN];
if (inet_ntop(AF_INET, &clntAddr.sin_addr.s_addr, clntName, sizeof(clntName)) != NULL) {
printf("\nHandling client %s/%d\n", clntName, ntohs(clntAddr.sin_port));
} else {
puts("Unable to get client address\n");
}
int msgSize = 0;
/* Extract the tweet and put it in tweetBuf */
if((msgSize = recv(clientSock, tweetBuf, BUFSIZE, 0)) < 0) {
puts("Message not received properly\n");
exit(1);
}
if (tweetBuf != NULL) {
command = strtok(tweetBuf, "<?>");
printf("%s\n", tweetBuf);
if (command != NULL) {
subject = strtok(NULL, "<?>");
}
if (subject != NULL && (strcmp(command, "upload") == 0)) {
newTweet = strtok(NULL, "<?>");
}
} else {
puts("ERROR: tweetBuf is NULL");
}
memset(&responseBuf, 0, SNDBUFSIZE);
if (command != NULL) {
/**************
* DOWNLOAD
**************/
if (strcmp(command, "download") == 0) {
if (strcmp(subject, "eat") == 0) {
strcpy(responseBuf, listToStr(eatList));
} else {
if (strcmp(subject, "sleep") == 0) {
strcpy(responseBuf, listToStr(sleepList));
} else {
if (strcmp(subject, "networking") == 0) {
strcpy(responseBuf, listToStr(networkingList));
}
}
}
if (send(clientSock, responseBuf, strlen(responseBuf), 0) != strlen(responseBuf)) {
puts("Download did not complete\n");
} else {
puts("Tweets sent to client.\n");
}
} else {
/**************
* UPLOAD
**************/
if (strcmp(command, "upload") == 0) {
printf("(%s) %s\n", subject, newTweet);
if (strcmp(subject, "eat") == 0) {
addTweet(eatList, makeNode(newTweet));
} else {
if (strcmp(subject, "sleep") == 0) {
addTweet(sleepList, makeNode(newTweet));
} else {
if (strcmp(subject, "networking") == 0) {
addTweet(networkingList, makeNode(newTweet));
}
}
}
}
}
}
}
close(clientSock);
}