-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
140 lines (135 loc) · 3.93 KB
/
Copy pathserver.cpp
File metadata and controls
140 lines (135 loc) · 3.93 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
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
#include "gsys.h"
#include "Sock2.h"
#include "ServerLog.h"
#include <stdio.h>
#ifdef TGOSLINUX
#include <signal.h>
#include <sys/socket.h> /* shutdown() */
#include <netdb.h> /* getservent() */
#include <netinet/in.h> /* struct sockaddr_in, INADDR_NONE */
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <arpa/inet.h> /* inet_addr() */
#include <sys/ioctl.h> /* ioctl() */
#include <sys/times.h> /* times() */
#include <sys/termios.h> /* fstat() */
#include <sched.h>
#include <fcntl.h> /* fcntl() */
#include <string.h> /* bcopy() */
#include <errno.h> /* errno */
#include <stdlib.h> /* exit() */
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h> /* close(),sleep(),chdir(), select(),getopt() */
#include <sys/types.h>
#include <sys/stat.h> /* fstat() */
#endif
#define DEBUG
ServerLog *slog = new ServerLog("");
extern int testcon(char *,Sock2 *);
int main(int argc,char *argv[])
{
Sock2 s;
Sock2 c;
int port;
int ret;
int i;
fd_set readset;
char buf[4096];
char tbuf[1024];
char *d;
uint32_t len;
if (argc < 2) {
slog->info((char *)"Must specify port #\n");
return 0;
}
port=atoi(argv[1]);
if (port <1024 || port > 49149) {
slog->info ((char *)"Invalid port #\n");
return 0;
}
s.open(port); // serve main port
if (s.fd == INVALID_SOCKET) {
sprintf(buf,"Unable to serve Port:%d\n",port);
slog->error(buf);
return 0;
}
sprintf(buf,"Serving Port:%d",port);
slog->info(buf);
#ifdef TGOSLINUX
signal(SIGCHLD,SIG_IGN);
#endif
while (1) {
FD_ZERO(&readset);
FD_SET(s.fd,&readset);
ret=select((int)(s.fd+1),&readset,NULL,NULL,NULL);
if (ret == -1 && errno == EINTR) {
continue;
}
if (ret == -1 || s.fd == INVALID_SOCKET) {
slog->error((char *)"Error Ret from select\n");
return 0;
}
c.open(s);
if (c.fd == INVALID_SOCKET) {
slog->error((char *)"open failed ??\n");
return 0;
}
break;
}
s.close();
c.DoHandShake();
if (c.st == HTMLSOCK) {
// Sock2 saved the request header in hsbuf
if (memcmp(c.hsbuf,"GET / HTTP/1.1\r\n",16) != 0) {
slog->info("Only know 1 page to server up");
}
else {
// you cannot do a put must use put data
strcpy(buf,"<!DOCTYPE html><head></head><body><H1>Hello World</h1></body>");
sprintf(tbuf,"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: %u\r\n\r\n",
(uint32_t)strlen(buf));
c.put_data(tbuf,strlen(tbuf));
c.put_data(buf,strlen(buf));
#ifdef TGOSLINUX
sleep(3);
#else
Sleep(3000);
#endif
}
c.close();
return 0;
}
for (i=0;i!=100;++i) {
memset(buf,'A',200);
buf[199]='\n';
buf[200]='\0';
sprintf(buf,"Hello Iteration %d",i);
buf[strlen(buf)]=' ';
if (Sock2::READY != c.put(buf,strlen(buf)+1)) {
printf("error sending data\n");
return 0;
}
if(Sock2::READY != c.getwait(&d,&len,-1)) {
printf("error waiting for got it\n");
return 0;
}
if (len > sizeof(buf)) {
printf("write res greater than %d\n",(int)sizeof(buf));
return 0;
}
memcpy(buf,d,len);
free(d);
if (strcmp(buf,"got it") != 0){
printf("No got it returned:%s\n",buf);
return 0;
}
}
strcpy(buf,"!!!!");
if(Sock2::READY != c.put(buf,strlen(buf)+1)) {
printf("Error sending !!!!\n");
return 0;
}
c.close();
return 0;
}