diff --git a/src/cpp/socket/src/socket.cc b/src/cpp/socket/src/socket.cc index 86867a9..2d42c56 100644 --- a/src/cpp/socket/src/socket.cc +++ b/src/cpp/socket/src/socket.cc @@ -1,4 +1,15 @@ +#include "socket.h" +#include +sockaddr_in getSocketAddr(char *hostname, int port) { + struct sockaddr_in serv_addr; + struct hostent *hPtr; + hPtr = gethostbyname(hostname); + memcpy((char *)&serv_addr.sin_addr, hPtr->h_addr, hPtr->h_length); + serv_addr.sin_family = AF_INET; + serv_addr.sin_port = htons(port); + return serv_addr; +} #ifdef OS_LINUX #include "socket_linux.cc" @@ -8,3 +19,5 @@ #include "socket_win.cc" #endif + + diff --git a/src/cpp/socket/src/socket.h b/src/cpp/socket/src/socket.h index 91b8552..0d0f04d 100644 --- a/src/cpp/socket/src/socket.h +++ b/src/cpp/socket/src/socket.h @@ -10,6 +10,9 @@ #endif #ifdef OS_WIN + #include + #include + using namespace std; #endif diff --git a/src/cpp/socket/src/socket_linux.cc b/src/cpp/socket/src/socket_linux.cc index 090fa62..f3ecd0e 100644 --- a/src/cpp/socket/src/socket_linux.cc +++ b/src/cpp/socket/src/socket_linux.cc @@ -1,5 +1,3 @@ -#include "socket.h" -#include //http://linux.die.net/man/2/socket int phplikeSocketConnect(char *hostname, int port) {/*{{{*/ diff --git a/src/cpp/socket/src/socket_win.cc b/src/cpp/socket/src/socket_win.cc index 3000e11..a1d0b34 100644 --- a/src/cpp/socket/src/socket_win.cc +++ b/src/cpp/socket/src/socket_win.cc @@ -1,14 +1,9 @@ -#include -#include "stdio.h" -#include -using namespace std; // http://blog.pusheax.com/2013/07/windows-api-winsock-create-your-first.html int phplikeSocketConnect(char *hostname, int port) {/*{{{*/ - struct hostent *hPtr; struct sockaddr_in serv_addr; - WSAData version; //We need to check the version. + WSAData version; WORD mkword = MAKEWORD(2,2); int what = WSAStartup(mkword,&version); if(what!=0) { @@ -20,14 +15,9 @@ int phplikeSocketConnect(char *hostname, int port) {/*{{{*/ std::cout<<"Creating socket fail\n"; } - hPtr = gethostbyname(hostname); - memcpy((char *)&serv_addr.sin_addr, hPtr->h_addr, hPtr->h_length); - serv_addr.sin_family = AF_INET; - serv_addr.sin_port = htons(port); - /*==========Addressing finished==========*/ + serv_addr = getSocketAddr(hostname, port); - //Now we connect - int conn=connect(u_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); + int conn = connect(u_sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)); if (conn == SOCKET_ERROR) { std::cout<<"Error - when connecting "<