blob: 339478dc7561502ffd42e6f7f90ffda0d0260b06 [file] [log] [blame]
Emeric Vigier2f625822012-08-06 11:09:52 -04001// Copyright (C) 1999-2005 Open Source Telecom Corporation.
2// Copyright (C) 2009 Leandro Melo de Sales <leandroal@gmail.com>
3//
4// This example demonstrates the operation of a server DCCP,
5// where awaiting connections locally, when a client connects,
6// the server creates a client to make operations for reading and writing data
7// in socket.
8//
9// This program is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 2 of the License, or
12// (at your option) any later version.
13//
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22//
23// As a special exception to the GNU General Public License, permission is
24// granted for additional uses of the text contained in its release
25// of Common C++.
26//
27// The exception is that, if you link the Common C++ library with other
28// files to produce an executable, this does not by itself cause the
29// resulting executable to be covered by the GNU General Public License.
30// Your use of that executable is in no way restricted on account of
31// linking the Common C++ library code into it.
32//
33// This exception does not however invalidate any other reasons why
34// the executable file might be covered by the GNU General Public License.
35//
36// This exception applies only to the code released under the
37// name Common C++. If you copy code from other releases into a copy of
38// Common C++, as the General Public License permits, the exception does
39// not apply to the code that you add in this way. To avoid misleading
40// anyone as to the status of such modified files, you must delete
41// this exception notice from them.
42//
43// If you write modifications of your own for Common C++, it is your choice
44// whether to permit this exception to apply to your modifications.
45// If you do not wish that, delete this exception notice.
46
47#include <cc++/socket.h>
48#include <iostream>
49#include <cstdlib>
50
51#ifdef CCXX_NAMESPACES
52using namespace std;
53using namespace ost;
54#endif
55
56class MyDCCPSocket : public DCCPSocket
57{
58
59public:
60 MyDCCPSocket(IPV4Host &ia, tpport_t port);
61 ssize_t myReadLine(char* buf);
62 ssize_t myWriteData(const char *buf);
63 void myBufferSize(unsigned size);
64};
65
66void MyDCCPSocket::myBufferSize(unsigned size){
67 this->bufferSize(size);
68}
69
70MyDCCPSocket::MyDCCPSocket(IPV4Host &ia, tpport_t port) : DCCPSocket(ia, port) {}
71
72//This method read data on socket
73ssize_t MyDCCPSocket::myReadLine(char* buf){
74 return this->readLine(buf, 200);
75}
76
77//This method writes data on socket
78ssize_t MyDCCPSocket::myWriteData(const char *buf){
79 return this-> writeData(buf,strlen(buf));
80}
81
82int main(int argc, char *argv[])
83{
84 //address of local interface
85 tpport_t port = 7000;
86 IPV4Host addr = "127.0.0.1";
87
88 //Startup of server DCCP
89 MyDCCPSocket server(addr,port);
90
91 //message to be written to the socket
92 char mss[] = "Hello Client\n";
93
94 char buffer[200];
95 cout << "Server wait connections in " << addr << ":" << port << endl;
96
97 //loop of waiting for connections
98 while(server.isPendingConnection(30000)) {
99 //accept the connection to the client
100 if (server.onAccept(addr,port)) {
101 //Is created to a client operation of reading and writing data in socket
102 MyDCCPSocket client(server);
103
104 //reading data from socket
105 client.myReadLine(buffer);
106
107 client.myBufferSize(15);
108 //printing the data read
109 cout << buffer << endl;
110
111 ///call the method of writing data
112 client.myWriteData(mss);
113
114 }
115 else cout << "Unable to accept the connection to the remote client" << endl;
116 }
117 cout << "timeout after 30 seconds inactivity, exiting" << endl;
118 return 0;
119}