Root/
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | < html > <!-- #BeginTemplate "/Templates/tmpl.dwt" --> <!-- DW6 --> < head > <!-- #BeginEditable "doctitle" --> < title >PTypes: networking: examples</ title > <!-- #EndEditable --> < meta http-equiv = "Content-Type" content = "text/html; charset=iso-8859-1" > < link rel = "stylesheet" href = "styles.css" > </ head > < body bgcolor = "#FFFFFF" leftmargin = "40" marginwidth = "40" > < p >< a href = "../index.html" >< img src = "title-21.png" width = "253" height = "39" alt = "C++ Portable Types Library (PTypes) Version 2.1" border = "0" ></ a > < hr size = "1" noshade> <!-- #BeginEditable "body" --> < p class = "hpath" >< a href = "index.html" >Top</ a >: < a href = "inet.html" >Networking</ a >: Examples</ p > < p >< b >< br > Example 1</ b >. A simple IP client that tries to retrieve the main page at www.yahoo.com via an HTTP request. </ p > < blockquote > < pre > #include < pinet.h > USING_PTYPES int main() { ipstream client("www.yahoo.com", 80); try { client.open(); < span class = "comment" >// send a valid HTTP/1.1 request</ span > client.put("GET / HTTP/1.1\r\n"< br > "Accept: * /*\r\n"< br > "User-Agent: TestAgent/1.0\r\n"< br > "Host: www.yahoo.com\r\n"< br > "Connection: close\r\n\r\n"); client.flush(); < span class = "comment" >// receive the response and print it line by line</ span > while (!client.get_eof()) pout.putline(client.line()); client.close(); } catch(estream* e) { perr.putf("Error: %s\n", pconst(e->get_message())); delete e; } return 0; }</ pre > </ blockquote > < p >< b >Example 2 </ b >. This example demonstrates the basic usage of streaming network interfaces < span class = "lang" >ipstream</ span > and < span class = "lang" >ipstmserver</ span >. It consists of two programs: test client and test server. The server handles requests that contain a single word "Hello" by sending a response greeting back to the client. This example would work for named pipes as well, i.e. if you replace the class names < span class = "lang" >ipstream</ span > and < span class = "lang" >ipstmserver</ span > with < span class = "lang" >namedpipe</ span > and < span class = "lang" >npserver</ span > and fix the construction parameters.</ p > < p >< u >Client:</ u ></ p > < blockquote > < pre > #include < pinet.h > USING_PTYPES const int testport = 8085; const int maxtoken = 4096; int main() { < span class = "comment" >// create a client socket and send a greeting to the server // assuming that the server is on the same host (127.0.0.1)</ span > ipstream client(ipaddress(127, 0, 0, 1), testport); try { client.open(); pout.put("Sending a request to the server...\n"); client.putline("Hello"); client.flush(); < span class = "comment" >// receive the response</ span > string rsp = client.line(maxtoken); pout.putf("Received: %s\n", pconst(rsp)); < span class = "comment" >// need to close the socket explicitly to gracefully shutdown // the peer host too. otherwise, ~ipstream() will call cancel() // and leave the peer in a waiting state (not forever though)</ span >. client.close(); } catch(estream* e) { perr.putf("Error: %s\n", pconst(e->get_message())); delete e; } return 0; }</ pre > </ blockquote > < p > </ p > < p >< u >Server:</ u ></ p > < blockquote > < pre > #include < ptime.h > #include < pinet.h > USING_PTYPES const int testport = 8085; const int maxtoken = 4096; void servermain(ipstmserver& svr) { ipstream client; pout.putf("Ready to answer queries on port %d\n", testport); while(true) { < span class = "comment" >// serve() will wait for a connection request and will prepare // the supplied ipstream object for talking to the peer. // note that (unlikely) exceptions thrown in serve() will be // caught in main()</ span > svr.serve(client); < span class = "comment" >// for better performance the server would start a new thread // for each client request. for simplicity, we serve the request // in-place. see multithreading examples for the full multithreaded // server template.</ span > if (client.get_active()) { try { < span class = "comment" >// read the request line: // real-world network applications should limit input data // to prevent potential denial-of-service attacks</ span > string req = lowercase(client.line(maxtoken)); if (req == "hello") { < span class = "comment" >// try to reverse-lookup the client's IP</ span > string host = phostbyaddr(client.get_ip()); if (isempty(host)) host = iptostring(client.get_ip()); < span class = "comment" >// now send our greeting to the client</ span > client.putline("Hello, " + host + " (" + iptostring(client.get_ip()) + "), nice to see you!"); client.flush(); < span class = "comment" >// log this request</ span > pout.putf("%t greeting received from %s (%a)\n", now(), pconst(host), long(client.get_ip())); } client.close(); } catch(estream* e) { perr.putf("Error: %s\n", pconst(e->get_message())); delete e; } } } } int main() { ipstmserver svr; try { < span class = "comment" >// bind to all local addresses on port 8085</ span > svr.bindall(testport); < span class = "comment" > // enter an infinite loop of serving requests</ span > servermain(svr); } catch(estream* e) { perr.putf("FATAL: %s\n", pconst(e->get_message())); delete e; } return 0; } </ pre > </ blockquote > < p >< b >Example 3 </ b >. This example demonstrates the use of message-oriented networking interfaces < span class = "lang" >ipmessage</ span > and < span class = "lang" >ipmsgserver</ span >. The client sends a broadcast message to the local network and waits for a response. It may retry the request several times.</ p > < p >< u >Client:</ u ></ p > < blockquote > < pre > #include < pinet.h > USING_PTYPES const int testport = 8085; const int maxtoken = 4096; const int tries = 3; const int firsttimeout = 2000; bool dorequest(int timeout) { ipmessage msg(ipbcast, testport); try { pout.put("Broadcasting a request...\n"); msg.send("Hello"); < span class = "comment" >// wait for a response the specified amount of time</ span > if (!msg.waitfor(timeout)) return false; ipaddress src; string rsp = msg.receive(maxtoken, src); pout.putf("Received: '%s' (from %a)\n", pconst(rsp), long(src)); } catch(estream* e) { perr.putf("Error: %s\n", pconst(e->get_message())); delete e; } return true; } int main() { int timeout = firsttimeout; for (int i = 0; i < tries ; i++) { if (dorequest(timeout)) break; <span class = "comment" >// double the timeout value</ span > timeout *= 2; } return 0; } </ pre > </ blockquote > < p > </ p > < p >< u >Server:</ u ></ p > < blockquote > < pre > #include < ptime.h > #include < pinet.h > USING_PTYPES const int testport = 8085; const int maxtoken = 4096; void servermain(ipmsgserver& svr) { pout.putf("Ready to answer queries on port %d\n", testport); bool quit = false; do { try { < span class = "comment" >// receive the "hello" request and send a simple answer // back to the client</ span > string req = lowercase(svr.receive(maxtoken)); if (req == "hello") { string host = svr.get_host(); if (isempty(host)) host = iptostring(svr.get_ip()); svr.send("Hello, " + host + " (" + iptostring(svr.get_ip()) + "), nice to see you!"); < span class = "comment" >// log this request</ span > pout.putf("%t greeting received from %s (%a)\n", now(), pconst(host), long(svr.get_ip())); } } catch(estream* e) { perr.putf("Server error: %s\n", pconst(e->get_message())); delete e; } } while (!quit); } int main() { ipmsgserver svr; try { svr.bindall(testport); < span class = "comment" >// try to listen on socket once to generate an error right away, // before entering the main server loop</ span > svr.poll(); < span class = "comment" >// enter an infinite loop of serving requests</ span > servermain(svr); } catch(estream* e) { perr.putf("FATAL: %s\n", pconst(e->get_message())); delete e; } return 0; } </ pre > </ blockquote > < p class = "seealso" >See also: < a href = "inet.ipstream.html" >ipstream</ a >, < a href = "inet.ipstmserver.html" >ipstmserver</ a >, < a href = "inet.ipmessage.html" >ipmessage</ a >, < a href = "inet.ipmsgserver.html" >ipmsgserver</ a >, < a href = "inet.utils.html" >Utilities</ a >, < a href = "async.examples.html" >Multithreading examples</ a ></ p > <!-- #EndEditable --> < hr size = "1" > < a href = "../index.html" class = "ns" >PTypes home</ a > </ body > <!-- #EndTemplate --> </ html > |
Source at commit 8edbcdac0d39 created 11 years 11 months ago. By Nathan Adams, initial commit |
---|