Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Print out host IP when loading.

  • Loading branch information...
commit d948aa614243f28b27cd0741a8e5c2e6dc810eb8 1 parent d8b0c2c
Chris Pearce cpearce authored committed
1  HttpMediaServer.cpp
View
@@ -134,6 +134,7 @@ int main(int argc, char* argv[])
return 1;
}
+ cout << "Local IP: " << listener->GetIP().c_str() << std::endl;
cout << "Now listening on port: " << PORT << std::endl;
vector<Connection*> Connections;
BIN  Release/HttpMediaServer.exe
View
Binary file not shown
28 Sockets.cpp
View
@@ -169,6 +169,11 @@ int Socket::Shutdown() {
#include <unistd.h>
#include <netinet/in.h>
#include <sys/select.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <arpa/inet.h>
+
+#define SOCKET_ERROR -1
class UnixSocket : public Socket {
public:
@@ -182,12 +187,19 @@ class UnixSocket : public Socket {
Socket* Socket::Open(int aPort) {
int sockfd;
+ int flags = 1;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("ERROR opening socket");
return 0;
}
+
+ if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &flags, sizeof(int))) {
+ perror("Can't set SO_REUSEADDR.");
+ return 0;
+ }
+
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
@@ -274,3 +286,19 @@ bool Socket::WaitForRead(unsigned timeout) {
return select(mSocket + 1, &socks, 0, 0, &to) > 0;
}
+
+string Socket::GetIP() const {
+ char buf[1024];
+ if (gethostname(buf, sizeof(buf)) == SOCKET_ERROR) {
+ return "Unknown";
+ }
+
+ struct hostent *host = gethostbyname(buf);
+ if(host == NULL) {
+ return "Unknown";
+ }
+
+ char* localIP;
+ localIP = inet_ntoa(*(struct in_addr *)*host->h_addr_list);
+ return string(localIP);
+}
7 Sockets.h
View
@@ -32,6 +32,10 @@
#ifndef __SOCKETS_H__
#define __SOCKETS_H__
+#include <string>
+
+using std::string;
+
// Wraps platform-specific socket API.
class Socket {
protected:
@@ -69,6 +73,9 @@ class Socket {
// connection is closing. <0 on error. Blocking.
virtual int Receive(char* aBuf, int aSize) = 0;
+ // Returns a string representation of the local IP address.
+ string GetIP() const;
+
protected:
Socket() {}
};
Please sign in to comment.
Something went wrong with that request. Please try again.