Winsock inside a service problem

  • Thread starter Thread starter Turda Alexander
  • Start date Start date
T

Turda Alexander

Guest
I got the sample from here: click

The code:

void ServStart()
{
WSADATA wsaData;

if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
{
printf("WSAStartup failed with error %ld.\n", WSAGetLastError());
exit(0);
}


if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
printf("The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
WSACleanup();
exit(0);
}

// Start listening
ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

if (ListeningSocket == INVALID_SOCKET)
{
printf("Error at socket, error code: %ld.\n", WSAGetLastError());
WSACleanup();
exit(0);
}

ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons(Port);
ServerAddr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR)
{
printf("bind failed. Error code: %ld.\n", WSAGetLastError());
MessageBoxW(NULL,L"ERROR",L"E",NULL);
closesocket(ListeningSocket);
WSACleanup();
exit(0);
}

if (listen(ListeningSocket, 5) == SOCKET_ERROR)
{
printf("listen: Error listening on socket %ld.\n", WSAGetLastError());
closesocket(ListeningSocket);
WSACleanup();
exit(0);
}


// Gets your external (public) IP < Internet
//MyIP = WebPost("api.externalip.net", "/ip/", "", 16); // Get my IP
//char HostName[255];
//hostent *HostEntry;
//gethostname(HostName, 255);
//HostEntry = gethostbyname(HostName);
//MyIP = inet_ntoa(*(struct in_addr *)*HostEntry->h_addr_list);


char ac[80];
if (gethostname(ac, sizeof(ac)) == SOCKET_ERROR)
{
printf("Error when getting local host name: ", WSAGetLastError());
exit(0);
}

struct hostent *phe = gethostbyname(ac);
if (phe == 0)
{
printf("Error: ", WSAGetLastError());
exit(0);
}

struct in_addr addr;
memcpy(&addr, phe->h_addr_list[0], sizeof(struct in_addr)); // use the first ip-address
printf("IP used by Server: %s\n", inet_ntoa(addr)); // inet_ntoa(addr) provides the local address.
MyIP = inet_ntoa(addr);

char SendBuf[32];
// * is used as a separator, because its not allowed in the hostname.
//So it wont interfere with it.
sprintf(SendBuf, "%hhu|%s*%s", cAddIP, MyIP, ac); // Send the server the IP and host name
WebPost(WEBSITE, WEBPAGE, SendBuf, 0);

printf("listening for connections...\n\n");

}




void CSampleService::ServiceWorkerThread(void)
{
ServStart();
// Periodically check if the service is stopping.
while (!m_fStopping)
{
SOCKADDR_IN SenderInfo;
SOCKET NewConnection;

int ByteReceived, nlen;
char recvbuff[1024];


// Main program loop
NewConnection = SOCKET_ERROR;
if(NewConnection == SOCKET_ERROR)
{
NewConnection = accept(ListeningSocket, NULL, NULL); // this is a blocking function
printf("New client got connected, ready to receive and send data...\n\n");

ByteReceived = recv(NewConnection, recvbuff, sizeof(recvbuff), 0);

if (ByteReceived > 0)
{
getsockname(ListeningSocket, (SOCKADDR *)&ServerAddr, (int *)sizeof(ServerAddr));

memset(&SenderInfo, 0, sizeof(SenderInfo));
nlen = sizeof(SenderInfo);

getpeername(NewConnection, (SOCKADDR *)&SenderInfo, &nlen);
}

}

if (shutdown(NewConnection, 2) != 0)
printf("there is something wrong with the shutdown. The error code: %ld\n", WSAGetLastError());
else
printf("shutdown is working...\n");

}



// Signal the stopped event.
SetEvent(m_hStoppedEvent);
}


As you can see,its the code form a simple server.But,when I try to connect to it,nothing.If I just run it as a console app,it works fine.


Whats the problem?

Continue reading...
 
Back
Top