-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsdchann.cpp
191 lines (158 loc) · 4.6 KB
/
bsdchann.cpp
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
/*******************************************************************
*
* DESCRIPTION: class BSDChannel
*
* AUTHOR: Amir Barylko & Jorge Beyoglonian
*
* EMAIL: mailto://amir@dc.uba.ar
* mailto://jbeyoglo@dc.uba.ar
*
* DATE: 27/6/1998
*
*******************************************************************/
/** include files **/
#include <netdb.h> // getservbyname( ... )
#include <sys/types.h> //
#include <sys/socket.h> // socket( ... )
#include <netinet/in.h> // htonl( ... )
#include <arpa/inet.h>
#include <cstdio> // close( int )
#include <cstring>
#include <unistd.h> // fork()
#include "bsdchann.h" // base header
using namespace std;
/** public data **/
/** private data **/
/** public functions **/
/*******************************************************************
* Function Name: BSDChannel
* Description: constructor
********************************************************************/
BSDChannel::BSDChannel( const string &serviceName )
: connection( -1 )
{
servent *entry = getservbyname( serviceName.c_str(), "tcp" ) ;
if( !entry )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( serviceName + "/tcp not found in the services file (/etc/services)" ) ;
throw e ;
}
this->open( ntohs( entry->s_port ) ) ;
}
/*******************************************************************
* Function Name: BSDChannel
* Description: constructor by portNumber
********************************************************************/
BSDChannel::BSDChannel( int portNumber )
: connection( -1 )
{
this->open( portNumber ) ;
}
/*******************************************************************
* Function Name: ~BSDChannel
* Description: destructor
********************************************************************/
BSDChannel::~BSDChannel()
{
if( this->connection > 0 )
close( connection ) ;
}
/*******************************************************************
* Function Name: open
* Description: open the communication
********************************************************************/
BSDChannel &BSDChannel::open( int portNumber )
{
int sockFD = socket( AF_INET, SOCK_STREAM, 0 ) ;
if( sockFD < 0 )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( "Can't open the stream socket!" ) ;
throw e ;
}
sockaddr_in serverAddress, clientAddress ;
bzero( reinterpret_cast<char*>( &serverAddress ), sizeof( serverAddress ) ) ;
serverAddress.sin_family = AF_INET ;
serverAddress.sin_addr.s_addr = htonl( INADDR_ANY ) ;
serverAddress.sin_port = htons( portNumber ) ;
if( bind( sockFD, reinterpret_cast< sockaddr* >( &serverAddress ), sizeof( serverAddress ) ) < 0 )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( "Can't bind local address!" ) ;
throw e ;
}
listen( sockFD, 5 ) ;
while( true )
{
socklen_t cliLength = sizeof( clientAddress );
connection = accept( sockFD, reinterpret_cast< sockaddr* >( &clientAddress ), &cliLength ) ;
if( connection < 0 )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( "Accept connection error!" ) ;
throw e ;
}
int childPID = fork() ;
if( childPID < 0 )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( "Fork error!" ) ;
throw e ;
}
if( childPID != 0 )
{
close( sockFD ) ;
return *this ;
}
close( connection ) ;
}
return *this ;
}
/*******************************************************************
* Function Name: readLine
* Description:
********************************************************************/
string BSDChannel::readLine()
{
const int maxLine( 1024 );
char buffer[maxLine];
int index = 0;
do
{
recv( connection, buffer+index, 1, 0 );
} while( index < maxLine && buffer[index++] != '\n' );
if( index == maxLine )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( string( "The line exceeds " ) + maxLine + " characters!" ) ;
throw e ;
}
if( index > 1 && buffer[ index-2 ] == '\r' )
buffer[ index-2 ] = '\0';
else
buffer[ index-1 ] = '\0';
return string( buffer );
}
/*******************************************************************
* Function Name: writeLine
* Description:
********************************************************************/
CommChannel &BSDChannel::writeLine( const string &line )
{
string aux( line + "\n" );
if( send( connection, aux.c_str(), aux.length(), 0 ) != static_cast<int>( aux.length() ) )
{
CommunicationError e ;
e.addLocation( MEXCEPTION_LOCATION() ) ;
e.addText( "send error!" ) ;
throw e ;
}
return *this ;
}