Click to See Complete Forum and Search --> : socket programming


ashwath_h
December 22nd, 2004, 08:38 AM
hi all,
pls. help me with the problem i am facing.
I am trying to get current time and date from a time server and syncronize that with my computer.
I have written the following lines of code.I took help of MSDN.
I am trying to connect to below PORTNUM and HOSTNAME of a timeserver.
But everytime i get a connection error such as
"No connection could be made because the target machine actively refused it."
error no. is 10061
I have tried with other time servers also such as
"time-a.nist.gov"
"time-b.nist.gov"

but in all cases i am getting the same error.
pls. help me out.


the code segment is given below:

#define PORTNUM 13
#define HOSTNAME "time.nist.gov"

int index = 0, iReturn; // Return value of recv function
char szClientA[100]; // ASCII string
TCHAR szClientW[100]; // Unicode string
TCHAR szError[100]; // Error message string

SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server
SOCKADDR_IN destination_sin; // Server socket address
PHOSTENT phostent = NULL; // Points to the HOSTENT structure
// of the server

WSADATA WSAData; // Contains details of the
// Winsocket implementation
// Initialize Winsocket.
if(WSAStartup(MAKEWORD(1,1),&WSAData)!= 0)
{
wsprintf (szError, TEXT("WSAStartup failed. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
return FALSE;
}

// Create a TCP/IP socket that is bound to the server.
if((ServerSock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
wsprintf (szError, TEXT("Allocating socket failed. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
return FALSE;
}

// Fill out the server socket's address information.
destination_sin.sin_family = AF_INET;

// Retrieve the host information corresponding to the host name.
if((phostent = gethostbyname(HOSTNAME)) == NULL)
{
wsprintf (szError, TEXT("Unable to get the host name. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
closesocket(ServerSock);
return FALSE;
}


// Assign the socket IP address.
memcpy((char FAR *)&(destination_sin.sin_addr),
phostent->h_addr,phostent->h_length);

// Convert to network ordering.
destination_sin.sin_port =htons(PORTNUM);

// Establish a connection to the server socket.
if(connect(ServerSock,(PSOCKADDR)&destination_sin,sizeof(destination_sin)) == SOCKET_ERROR)
{
wsprintf(szError,TEXT("Connecting to the server failed. Error: %d"),
WSAGetLastError());
MessageBox(NULL, szError, TEXT("Error"), MB_OK);
closesocket(ServerSock);
return FALSE;
}


if(send(ServerSock, "To Server.", strlen("To Server.")+1, 0)== SOCKET_ERROR)
{
wsprintf (szError,
TEXT("Sending data to the server failed. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
}

// Disable sending on ServerSock.
shutdown(ServerSock, 0x01);

for (;;)
{
// Receive data from the server socket.
iReturn = recv(ServerSock, szClientA, sizeof (szClientA), 0);
// Check if there is any data received.
//If there is, display it.

if(iReturn == SOCKET_ERROR)
{
wsprintf (szError, TEXT("No data is received, recv failed.")
TEXT("Error: %d"), WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Client"), MB_OK);
break;
}
else if (iReturn == 0)
{
MessageBox (NULL,TEXT("Finished receiving data"),TEXT("Client"),MB_OK);
break;
}
else
{
// Convert the ASCII string to a Unicode string.
for (index = 0; index <= sizeof (szClientA); index++)
szClientW[index] = szClientA[index];

// Display the string received from the server.
MessageBox(NULL,szClientW,TEXT("Received From Server"),MB_OK);
}
}
// Disable receiving on ServerSock.
shutdown(ServerSock, 0x00);
// Close the socket.
closesocket(ServerSock);
WSACleanup();

NoHero
December 22nd, 2004, 08:51 AM
I copied your code and it worked properly for me. The error code just means that the server is down for a moment.


#include <winsock2.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>

#pragma comment(lib, "ws2_32.lib")

#define PORTNUM 13
#define HOSTNAME "time.nist.gov"

int main ( void )
{
int index = 0, iReturn; // Return value of recv function
char szClientA[100]; // ASCII string
TCHAR szClientW[100]; // Unicode string
TCHAR szError[100]; // Error message string

SOCKET ServerSock = INVALID_SOCKET; // Socket bound to the server
SOCKADDR_IN destination_sin; // Server socket address
PHOSTENT phostent = NULL; // Points to the HOSTENT structure
// of the server

WSADATA WSAData; // Contains details of the
// Winsocket implementation
// Initialize Winsocket.
if(WSAStartup(MAKEWORD(1,1),&WSAData)!= 0)
{
wsprintf (szError, TEXT("WSAStartup failed. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
return FALSE;
}

// Create a TCP/IP socket that is bound to the server.
if((ServerSock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
wsprintf (szError, TEXT("Allocating socket failed. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
return FALSE;
}

// Fill out the server socket's address information.
destination_sin.sin_family = AF_INET;

// Retrieve the host information corresponding to the host name.
if((phostent = gethostbyname(HOSTNAME)) == NULL)
{
wsprintf (szError, TEXT("Unable to get the host name. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
closesocket(ServerSock);
return FALSE;
}
// Assign the socket IP address.
memcpy((char FAR *)&(destination_sin.sin_addr),
phostent->h_addr,phostent->h_length);

// Convert to network ordering.
destination_sin.sin_port =htons(PORTNUM);

// Establish a connection to the server socket.
if(connect(ServerSock,(PSOCKADDR)&destination_sin,sizeof(destination_sin)) == SOCKET_ERROR)
{
wsprintf(szError,TEXT("Connecting to the server failed. Error: %d"),
WSAGetLastError());
MessageBox(NULL, szError, TEXT("Error"), MB_OK);
closesocket(ServerSock);
return FALSE;
}


if(send(ServerSock, "To Server.", strlen("To Server.")+1, 0)== SOCKET_ERROR)
{
wsprintf (szError,
TEXT("Sending data to the server failed. Error: %d"),
WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Error"), MB_OK);
}

// Disable sending on ServerSock.
shutdown(ServerSock, 0x01);

for (;;)
{
// Receive data from the server socket.
iReturn = recv(ServerSock, szClientA, sizeof (szClientA), 0);
// Check if there is any data received.
//If there is, display it.

if(iReturn == SOCKET_ERROR)
{
wsprintf (szError, TEXT("No data is received, recv failed.")
TEXT("Error: %d"), WSAGetLastError ());
MessageBox (NULL, szError, TEXT("Client"), MB_OK);
break;
}
else if (iReturn == 0)
{
MessageBox (NULL,TEXT("Finished receiving data"),TEXT("Client"),MB_OK);
break;
}
else
{
// Convert the ASCII string to a Unicode string.
for (index = 0; index <= sizeof (szClientA); index++)
szClientW[index] = szClientA[index];

// Display the string received from the server.
MessageBox(NULL,szClientW,TEXT("Received From Server"),MB_OK);
}
}
// Disable receiving on ServerSock.
shutdown(ServerSock, 0x00);
// Close the socket.
closesocket(ServerSock);
WSACleanup();
}


p.s.: Use code tags if you post code. Thank you.

Andreas Masur
December 22nd, 2004, 10:37 AM
[ Moved thread ]

Mathew Joy
December 22nd, 2004, 01:16 PM
Usually this error means that there is no application servicing the connection at the specified address. Since your code is working (as said by NoHero) it is more likely to be a host service related problem. Make sure you are connecting at the right address/port. Check the firewall permissions.

ashwath_h
December 23rd, 2004, 01:26 AM
hi,

Thank you very much for the firewall suggestion.My request was not going out of the main server of our company because of firewall .That site was enabled in the server and now the code is working.


thanking you,

ashwath.