#include <common.h>
Inheritance diagram for SendingPort:
Public Member Functions | |
SendingPort (char *hostname, short port) | |
Another constructor with local address given. | |
virtual | ~SendingPort () |
Deconstructor. | |
void | init () |
void | sendPacket (Packet *pkt) |
void | setBroadcast () |
void | setBroadcastOff () |
Protected Attributes | |
int | bcastflag_ |
this flag indicates the port ought to broadcast or unicast a packet. |
SendingPort is an subclass of Port for sending purpose
Definition at line 184 of file common.h.
|
Function to initialize the port Implements Port. Definition at line 287 of file common.cpp. References bcastflag_, Address::getPort(), Address::isSet(), Port::itsaddr_, Port::myaddr_, Port::mySockAddress_, Port::setHostname(), Port::setPort(), Port::setSockAddress(), and Port::sockfd_. 00288 { 00289 //"0" shows the sockfd is uninitialized, -1 means error 00290 if (sockfd_ != 0) { 00291 cout << "socket has not been properly initialized!" << endl; 00292 return; 00293 } 00294 if ( myaddr_.isSet() == false) { 00295 setHostname("localhost"); 00296 setPort(DEFAULT_SEND_PORT); 00297 } 00298 //check if itsaddr_ is set 00299 if ( itsaddr_.isSet() == false) 00300 throw "Destination address of a sending port is not set!"; 00301 00302 if ((sockfd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 00303 perror("socket"); 00304 throw "Error while opening a UDP socket"; 00305 } 00306 Address *emptyAddr = new Address("", myaddr_.getPort()); 00307 struct sockaddr* addr = setSockAddress(emptyAddr, &mySockAddress_); 00308 if ( bind(sockfd_, addr, sizeof(struct sockaddr_in)) < 0 ){ 00309 perror("bind"); 00310 throw "Scoket Bind Error"; 00311 } 00312 00313 if (bcastflag_ == 1) 00314 if (setsockopt(sockfd_,SOL_SOCKET,SO_BROADCAST,&bcastflag_,sizeof(bcastflag_)) == -1 ) 00315 { 00316 perror("setsockopt"); 00317 throw "Set broadcast option failed."; 00318 }; 00319 00320 00321 return; 00322 00323 }
|
|
Function to send a packet. The default socket file descriptor will always be used for send() only a sockfd used by the port. Definition at line 330 of file common.cpp. References Port::dstSockAddress_, Packet::getPayload(), Packet::getPayloadSize(), Port::getRemoteAddr(), Port::setSockAddress(), and Port::sockfd_. 00331 { 00332 00333 Address *dst = getRemoteAddr(); 00334 int length = sizeof(struct sockaddr_in); 00335 struct sockaddr *dest = setSockAddress(dst, &dstSockAddress_); 00336 int len = sendto(sockfd_, pkt->getPayload(), pkt->getPayloadSize(), 0, dest, length); 00337 if (len == -1) 00338 { 00339 perror("send"); 00340 throw "Sending Error."; 00341 } 00342 }
|
|
toggle broadcast option on Definition at line 205 of file common.h. References bcastflag_. 00205 {bcastflag_ = 1;}
|
|
toggle broadcast option off Definition at line 209 of file common.h. References bcastflag_. 00209 {bcastflag_ = 0;}
|
|
this flag indicates the port ought to broadcast or unicast a packet. If broadcast, an broadcast IP address (192.168.255.255, etc) need to be supplied Definition at line 216 of file common.h. Referenced by init(), setBroadcast(), and setBroadcastOff(). |