Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 54b651d

Browse files
1 parent 91a511b commit 54b651d

File tree

5 files changed

+65
-35
lines changed

5 files changed

+65
-35
lines changed

‎u11_testat/SocketIO.cpp‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@
22

33
using namespace std;
44

5-
SocketIO::SocketIO(int fd) : sock(fd), writePtr(buf), readPtr(buf), endPtr(buf + sizeof (buf)), eof_reached(false) {
5+
SocketIO::SocketIO(int fd) : sock(fd), writePtr(buf), readPtr(buf), endPtr(buf + sizeof (buf)), eof_reached(true) {
66
}
77

88
SocketIO::~SocketIO() {
9-
9+
this->doClose();
1010
}
1111

1212
string SocketIO::readline() {
13-
// TODO: implement this
13+
return "";
14+
}
15+
16+
string SocketIO::readlines() {
17+
fillbuf();
18+
if (eof_reached) {
19+
return "";
20+
}
21+
eof_reached = true;
22+
return lines;
1423
}
1524

1625
void SocketIO::writeN(const char *buf, int len) {
17-
write(sock, buf, len);
26+
cout << "write: " << write(sock, buf, len) << " chars of " << len << ": " << endl << buf << "...";
1827
}
1928

2029
void SocketIO::doClose() {
@@ -29,9 +38,16 @@ string SocketIO::getPeerInfo() {
2938
}
3039

3140
bool SocketIO::fillbuf() { // corresponds to streambuf::underflow()
32-
// TODO: implement this
41+
int i = recv(sock, buf, BUF_SIZE, 0);
42+
if (i < 0) {
43+
lines = "";
44+
return false;
45+
}
46+
eof_reached = (i == 0);
47+
lines = string(buf, buf + i);
48+
return true;
3349
}
3450

3551
int SocketIO::getc() {
36-
// TODO: implement this
52+
return0;
3753
}

‎u11_testat/SocketIO.h‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <fstream>
1212
#include <cerrno>
1313
#include <stdlib.h>
14+
#include <vector>
1415

1516
class SocketIO {
1617
public:
@@ -20,18 +21,21 @@ class SocketIO {
2021
SocketIO(int fd);
2122
virtual ~SocketIO();
2223
std::string readline();
24+
std::string readlines();
2325
void writeN(const char *buf, int len);
2426
void doClose();
2527
std::string getPeerInfo();
2628
private:
2729
int sock; // socket file descriptor
2830
char buf[BUF_SIZE]; // a bit stupid way to mimick an istream
29-
char *writePtr;
30-
char *readPtr;
31-
const char *endPtr;
31+
std::string lines;
32+
char *writePtr;
33+
char *readPtr;
34+
char *endPtr;
35+
//const char *endPtr;
3236

33-
bool fillbuf();
34-
int getc();
37+
bool fillbuf();
38+
int getc();
3539
};
3640

3741
#endif /* SOCKETIO_H */

‎u11_testat/build/Debug/Cygwin_4.x-Windows/SocketIO.o.d‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

‎u11_testat/build/Debug/Cygwin_4.x-Windows/htechoserv.o.d‎

Lines changed: 0 additions & 6 deletions
This file was deleted.

‎u11_testat/htechoserv.cpp‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,32 @@
1515
using namespace std;
1616

1717
string getHeaders(int len) {
18-
return "";
18+
//return "";
1919
stringstream ss;
20-
ss << "HTTP/1.1 200 OK\r\n";
21-
ss << "Date: 2005年5月23日 22:38:34 GMT\r\n";
22-
ss << "Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)\r\n";
23-
ss << "Last-Modified: 2003年1月08日 23:11:55 GMT\r\n";
24-
ss << "Etag: \"3f80f-1b6-3e1cb03b\"\r\n";
25-
ss << "Accept-Ranges: bytes\r\n";
26-
ss << "Content-Length: " << len << "\r\n";
27-
ss << "Connection: close\r\n";
28-
ss << "Content-Type: text/html; charset=UTF-8\r\n";
20+
ss << "HTTP/1.1 200 OK\n";
21+
ss << "\n";
22+
//ss << "HTTP/1.1 200 OK\r\n";
23+
//ss << "Date: 2005年5月23日 22:38:34 GMT\r\n";
24+
//ss << "Server: LukesServer/0.0.0.1 (Unix) (Windows/NT6.1)\r\n";
25+
//ss << "Last-Modified: 2003年1月08日 23:11:55 GMT\r\n";
26+
//ss << "Etag: \"3f80f-1b6-3e1cb03b\"\r\n";
27+
//ss << "Accept-Ranges: bytes\r\n";
28+
//ss << "Content-Length: " << len << "\r\n";
29+
//ss << "Connection: close\r\n";
30+
//ss << "Content-Type: text/html\r\n";
31+
//ss << "Content-Type: text/html; charset=UTF-8\r\n";
2932
return ss.str();
3033
}
3134

35+
string generateResponse(string request, int counter) {
36+
stringstream ss1, ss2;
37+
ss1 << "<html><head></head><body><h1>Dum di dum</h1>This is request #" << counter << "." <<
38+
"<br/>The request was:" <<
39+
"<pre>" << request << "</pre></body></html>\n";
40+
ss2 << getHeaders(ss1.str().length()) << ss1.str();
41+
return ss2.str();
42+
}
43+
3244
int main(int argc, char**argv) {
3345
if (argc < 2) {
3446
cerr << argv[0] << ": port" << endl;
@@ -45,18 +57,25 @@ int main(int argc, char**argv) {
4557
while (!quit) {
4658
++counter;
4759
int clientfd = ss.doAccept();
60+
//if (fork()) {
4861
cout << "Starting request...";
49-
if (clientfd < 0) continue; // retry
62+
if (clientfd < 0) {
63+
cout << "connection failed!" << endl;
64+
exit(0);
65+
//continue; // retry
66+
}
5067
SocketIO sio(clientfd);
5168

52-
stringstream ss1, ss2;
53-
ss1 << "<html><head></head><body><h1>Huuuhuuu</h1><pre>yeah yeah, this is request #" << counter << "!</pre></body></html>\r\n";
54-
ss2 << getHeaders(ss1.str().length()) << ss1.str();
55-
string reply = ss2.str();
69+
string lines_received = sio.readlines();
70+
cout << lines_received << endl;
71+
string reply = generateResponse(lines_received, counter);
5672
sio.writeN(reply.c_str(), reply.length());
5773
sio.doClose();
5874

5975
cout << " done!" << endl;
76+
exit;
77+
//}
78+
cout << "server continues listening..." << endl;
6079
}
6180
ss.doClose();
6281
return 0;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /