-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
executable file
·115 lines (99 loc) · 3.18 KB
/
server.c
File metadata and controls
executable file
·115 lines (99 loc) · 3.18 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
/******************************************************************************
* myServer.c
*
* Writen by Prof. Smith, updated Jan 2023
* Use at your own risk.
*
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdint.h>
#include "networks.h"
#include "safeUtil.h"
#include "PDUTransmit.h"
#include "pollLib.h"
#include "serverMessageHandler.h"
#include "handleTable.h"
#define MAXUSERDATA 200 //Users can send messages of at most 200 bytes (199 + NULL Byte)
#define PDUHEADERLEN 3 //
#define DEBUG_FLAG 1
int recvFromClient(int clientSocket);
int checkArgs(int argc, char *argv[]);
//these functions already existed in the demo code, i dont see any reason we shouldnt be able to use them :)
void addNewSocket(int mainServerSocket, int debugFlag){
int clientSocket= tcpAccept(mainServerSocket, DEBUG_FLAG);
addToPollSet(clientSocket);
}
void processClient(int clientSocket){
int msgLen=recvFromClient(clientSocket);
if(msgLen==0){ //client is closed
handleTable_removeBySocket(clientSocket); //remove them from handle table and pollset
removeFromPollSet(clientSocket);
close(clientSocket); //close the file descriptor
// printf("Socket %d: Connection closed by other side\n", clientSocket);
}
}
void serverControl(int mainServerSocket){
int pollRet=pollCall(-1);
if(pollRet==mainServerSocket){
addNewSocket(mainServerSocket, DEBUG_FLAG); //performs TCP actions needed for adding a new client
}else{
processClient(pollRet); //handles all messages recieved from clients (aside from initial setup)
}
}
int main(int argc, char *argv[]){
int mainServerSocket = 0; //socket descriptor for the server socket
int portNumber = checkArgs(argc, argv); //parse the users intended port number for server if one is provided
mainServerSocket = tcpServerSetup(portNumber); //create the server socket
setupPollSet(); //create pollSet
addToPollSet(mainServerSocket);
handleTable_setup(); //create a new handler table
while(1){
serverControl(mainServerSocket); //handle client connections
}
close(mainServerSocket);
return 0;
}
int recvFromClient(int clientSocket)
{
uint8_t dataBuffer[MaxIPMsgSize];
int dataLength = 0; //includes the flag, and then the user message
//now get the data from the client_socket
if ((dataLength = recvPDU(clientSocket, dataBuffer, MaxIPMsgSize)) < 0)
{
perror("recv call");
exit(-1);
}
if (dataLength > 0)
{
// printf("Sock %d: Message received, length: %d Data: %s\n", clientSocket, dataLength, dataBuffer);
handleMessage(clientSocket, (struct PDUMsg*) dataBuffer);
}
return dataLength; //0 for connection closed, otherwise non-zero (message length)
}
int checkArgs(int argc, char *argv[])
{
// Checks args and returns port number
int portNumber = 0;
if (argc > 2)
{
fprintf(stderr, "Usage %s [optional port number]\n", argv[0]);
exit(-1);
}
if (argc == 2)
{
portNumber = atoi(argv[1]);
}
return portNumber;
}