diff options
Diffstat (limited to 'CommonLibs')
-rw-r--r-- | CommonLibs/Sockets.cpp | 14 | ||||
-rw-r--r-- | CommonLibs/Sockets.h | 8 | ||||
-rw-r--r-- | CommonLibs/SocketsTest.cpp | 4 |
3 files changed, 13 insertions, 13 deletions
diff --git a/CommonLibs/Sockets.cpp b/CommonLibs/Sockets.cpp index 9030a86..a65d62b 100644 --- a/CommonLibs/Sockets.cpp +++ b/CommonLibs/Sockets.cpp @@ -223,18 +223,18 @@ int DatagramSocket::read(char* buffer, size_t length, unsigned timeout) -UDPSocket::UDPSocket(unsigned short wSrcPort) +UDPSocket::UDPSocket(const char *wSrcIP, unsigned short wSrcPort) :DatagramSocket() { - open(wSrcPort); + open(wSrcPort, wSrcIP); } -UDPSocket::UDPSocket(unsigned short wSrcPort, - const char * wDestIP, unsigned short wDestPort ) +UDPSocket::UDPSocket(const char *wSrcIP, unsigned short wSrcPort, + const char *wDestIP, unsigned short wDestPort) :DatagramSocket() { - open(wSrcPort); + open(wSrcPort, wSrcIP); destination(wDestPort, wDestIP); } @@ -246,7 +246,7 @@ void UDPSocket::destination( unsigned short wDestPort, const char * wDestIP ) } -void UDPSocket::open(unsigned short localPort) +void UDPSocket::open(unsigned short localPort, const char *wlocalIP) { // create mSocketFD = socket(AF_INET,SOCK_DGRAM,0); @@ -265,7 +265,7 @@ void UDPSocket::open(unsigned short localPort) size_t length = sizeof(address); bzero(&address,length); address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + address.sin_addr.s_addr = inet_addr(wlocalIP); address.sin_port = htons(localPort); if (bind(mSocketFD,(struct sockaddr*)&address,length)<0) { perror("bind() failed"); diff --git a/CommonLibs/Sockets.h b/CommonLibs/Sockets.h index 0a70269..8312843 100644 --- a/CommonLibs/Sockets.h +++ b/CommonLibs/Sockets.h @@ -144,11 +144,11 @@ class UDPSocket : public DatagramSocket { public: /** Open a USP socket with an OS-assigned port and no default destination. */ - UDPSocket( unsigned short localPort=0); + UDPSocket(const char *localIP, unsigned short localPort); /** Given a full specification, open the socket and set the dest address. */ - UDPSocket( unsigned short localPort, - const char * remoteIP, unsigned short remotePort); + UDPSocket(const char *localIP, unsigned short localPort, + const char *remoteIP, unsigned short remotePort); /** Set the destination port. */ void destination( unsigned short wDestPort, const char * wDestIP ); @@ -157,7 +157,7 @@ public: unsigned short port() const; /** Open and bind the UDP socket to a local port. */ - void open(unsigned short localPort=0); + void open(unsigned short localPort=0, const char *wlocalIP="127.0.0.1"); /** Give the return address of the most recently received packet. */ const struct sockaddr_in* source() const { return (const struct sockaddr_in*)mSource; } diff --git a/CommonLibs/SocketsTest.cpp b/CommonLibs/SocketsTest.cpp index 1fa8bbd..c2849e0 100644 --- a/CommonLibs/SocketsTest.cpp +++ b/CommonLibs/SocketsTest.cpp @@ -37,7 +37,7 @@ static const int gNumToSend = 10; void *testReaderIP(void *) { - UDPSocket readSocket(5934, "localhost", 5061); + UDPSocket readSocket("127.0.0.1", 5934, "localhost", 5061); readSocket.nonblocking(); int rc = 0; while (rc<gNumToSend) { @@ -82,7 +82,7 @@ int main(int argc, char * argv[] ) Thread readerThreadUnix; readerThreadUnix.start(testReaderUnix,NULL); - UDPSocket socket1(5061, "127.0.0.1",5934); + UDPSocket socket1("127.0.0.1", 5061, "127.0.0.1", 5934); UDDSocket socket1U("testSource","testDestination"); COUT("socket1: " << socket1.port()); |