1、tcpclient(Linux)
1 SocketAddress sa("192.168.20.43", 7000);
2 StreamSocket ss(sa);
3 int n = ss.sendBytes("hello", 5);
4 char buffer[256];
5 n = ss.receiveBytes(buffer, sizeof(buffer));
6 printf("n = %d\n",n);
2、tcpserver(win7)
TCPServer.cpp
1 #include "stdafx.h"
2 #include <Poco/Net/TCPServer.h>
3 #include <Poco/Net/TCPServerConnection.h>
4 #include <Poco/Net/TCPServerConnectionFactory.h>
5 #include <Poco/Util/Application.h>
6 #include <Poco/Util/ServerApplication.h>
7 using namespace Poco::Net;
8 using namespace Poco::Util;
9
10 class TestConnection: public TCPServerConnection
11 {
12 public:
13 TestConnection(const StreamSocket& s):
14 TCPServerConnection(s)
15 {
16 socket().setBlocking(false);
17 }
18 void run()
19 {
20 StreamSocket& ss = socket();
21 try
22 {
23 char buffer[256];
24 int n = ss.receiveBytes(buffer, sizeof(buffer));
25 while (n > 0)
26 {
27 ss.sendBytes(buffer, n);
28 n = ss.receiveBytes(buffer, sizeof(buffer));
29 }
30 }
31 catch (Poco::Exception& exc)
32 {
33 std::cerr << "EchoConnection: " << exc.displayText() << std::endl;
34 }
35 }
36 };
37 class TestConnectionFactory: public TCPServerConnectionFactory
38 {
39 public:
40 TCPServerConnection* createConnection(const StreamSocket& socket)
41 {
42 TestConnection* tc = new TestConnection(socket);
43 return tc;
44 }
45 };
46 //ServerApplication
47 class Server: public ServerApplication
48 {
49 protected:
50 void initialize(Application& self)
51 {
52 loadConfiguration();
53 ServerApplication::initialize(self);
54 }
55
56 void uninitialize()
57 {
58 ServerApplication::uninitialize();
59 }
60
61 int main(const std::vector<std::string>& args)
62 {
63 ServerSocket serverSock(7000);
64 TCPServer tcpServer(new TestConnectionFactory(), serverSock);
65 tcpServer.start();
66 waitForTerminationRequest();
67 tcpServer.stop();
68 return Application::EXIT_OK;
69 }
70 };
71 int _tmain(int argc, _TCHAR* argv[])
72 {
73 Server app;
74 return app.run(argc, argv);
75 }