#include <common.h>
Inheritance diagram for SendingPort:
Public Member Functions | |
SendingPort () | |
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. | |
char * | sendingbuf_ |
SendingPort is an subclass of Port for sending purpose
Definition at line 253 of file common.h.
|
Function to initialize the port Implements Port. Definition at line 363 of file common.cpp. References Address::isSet(), Port::myaddr_, Port::setHostname(), Port::setPort(), and Port::sockfd_. 00364 { 00365 //"0" shows the sockfd is uninitialized, -1 means error 00366 if (sockfd_ != 0) { 00367 cout << "socket has not been properly initialized!" << endl; 00368 return; 00369 } 00370 if ( myaddr_.isSet() == false) { 00371 setHostname("localhost"); 00372 setPort(DEFAULT_SEND_PORT); 00373 } 00374 //check if itsaddr_ is set 00375 if ( itsaddr_.isSet() == false) 00376 throw "Destination address of a sending port is not set!"; 00377 00378 if ((sockfd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 00379 perror("socket"); 00380 throw "Error while opening a UDP socket"; 00381 } 00382 Address *emptyAddr = new Address("", myaddr_.getPort()); 00383 struct sockaddr* addr = setSockAddress(emptyAddr, &mySockAddress_); 00384 if ( bind(sockfd_, addr, sizeof(struct sockaddr_in)) < 0 ){ 00385 perror("bind"); 00386 throw "Scoket Bind Error"; 00387 } 00388 00389 if (bcastflag_ == 1) 00390 if (setsockopt(sockfd_,SOL_SOCKET,SO_BROADCAST,&bcastflag_,sizeof(bcastflag_)) == -1 ) 00391 { 00392 perror("setsockopt"); 00393 throw "Set broadcast option failed."; 00394 }; 00395 00396 //create sending buffer 00397 sendingbuf_ = new char[MTU_SIZE+1]; 00398 return; 00399 00400 }
|
|
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 407 of file common.cpp. References Port::dstSockAddress_, Port::getRemoteAddr(), Packet::makePacket(), sendingbuf_, Port::setSockAddress(), and Port::sockfd_. 00408 { 00409 int pktlen = pkt->makePacket(sendingbuf_); 00410 Address *dst = getRemoteAddr(); 00411 int length = sizeof(struct sockaddr_in); 00412 struct sockaddr *dest = setSockAddress(dst, &dstSockAddress_); 00413 int len = sendto(sockfd_, sendingbuf_, pktlen, 0, dest, length); 00414 if (len == -1) 00415 { 00416 perror("send"); 00417 throw "Sending Error."; 00418 } 00419 }
|
|
toggle broadcast option on Definition at line 274 of file common.h. References bcastflag_. 00274 {bcastflag_ = 1;}
|
|
toggle broadcast option off Definition at line 278 of file common.h. References bcastflag_. 00278 {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 285 of file common.h. Referenced by setBroadcast(), and setBroadcastOff(). |
|
Sending buffer Definition at line 289 of file common.h. Referenced by sendPacket(). |