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();
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();