-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.c
137 lines (116 loc) · 2.98 KB
/
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <pthread.h>
#include "form.h"
#define CLIENT_MAX 30
void server(int socket);
void error_handling(char *message);
int main(int argc, char *argv[])
{
int serv_sock;
int clnt_sock;
int connectCnt = 0;
pthread_t thread_id;
struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
socklen_t clnt_addr_size;
unsigned char writeBuf[BUFSIZE] = {0, };
unsigned char readBuf[BUFSIZE] = {0, };
if(argc != 2)
{
printf("Usage : %s <port>\n", argv[0]);
exit(1);
}
serv_sock = socket(PF_INET, SOCK_STREAM, 0);
if(serv_sock == -1)
error_handling("socket() error");
memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));
if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) == -1)
error_handling("bind() error");
if(listen(serv_sock, 5) == -1)
error_handling("listen() error");
clnt_addr_size = sizeof(clnt_addr);
List *list;
list = calloc(1, sizeof(List));
InitList(list);
unsigned char *global_meta;
global_meta = calloc(BUFSIZE, sizeof(unsigned char));
while(1)
{
clnt_sock=accept(serv_sock, (struct sockaddr_in*)&clnt_addr, &clnt_addr_size);
if(clnt_sock==-1)
continue;
else
printf("Connected client %d \n", ++i);
while(1)
{
writeBuf[0] = GLOBAL_META;
memcpy(&writeBuf+1, global_meta, BUFSIZE -1);
write(clnt_sock, &writeBuf, sizeof(writeBuf));
memset(&writeBuf, 0, BUFSIZE);
Node *node = calloc(1, sizeof(Node));
node = list->head->next;
for(int i = 0; i < list->count; i++)
{
PACKET *packet = calloc(1, sizeof(packet));
packet->msgType = DATA;
NODE_SEND *node_send = calloc(1, sizeof(NODE_SEND));
packet->data = node_send;
node_send->inst = INSERT;
node_send->index = i;
memcpy(node_send->data, node->data, 16);
packing_data(packet, &writeBuf);
write(clnt_sock, &writeBuf, sizeof(writeBuf));
memset(&writeBuf, 0, BUFSIZE);
node = node->next;
free(packet);
free(node_send);
}
write(clnt_sock, "finish", BUFSIZE);
while(1)
{
read(clnt_sock, &readBuf, sizeof(readBuf));
if(strncmp(readBuf, "finish", 6) == 0)
break;
else if(readBuf[0] == GLOBAL_META)
unpacking_global(readBuf, global_meta);
else if(readBuf[0] == DATA)
{
Node *new_node = calloc(1, sizeof(Node));
unpacking_data(readBuf, new_node, list);
}
memset(&readBuf, 0, BUFSIZE);
}
//printf("%s\n", readBuf);
}
}
close(serv_sock);
return 0;
}
void server(int socket)
{
char writeBuf[256] = "received well!";
char readBuf[256];
while(1)
{
read(socket, &readBuf, sizeof(readBuf));
printf("%s\n", readBuf);
write(socket, &writeBuf, sizeof(writeBuf));
if((strcmp(readBuf, "quit"))==0)
break;
}
close(socket);
}
void error_handling(char *message)
{
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}