Click here to Skip to main content
15,900,405 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: read a file using Readfile() Pin
Ozer Karaagac20-Jun-09 14:25
professionalOzer Karaagac20-Jun-09 14:25 
GeneralRe: read a file using Readfile() Pin
rahuljin20-Jun-09 1:55
rahuljin20-Jun-09 1:55 
GeneralRe: read a file using Readfile() Pin
chandu00420-Jun-09 2:06
chandu00420-Jun-09 2:06 
GeneralRe: read a file using Readfile() Pin
rahuljin20-Jun-09 3:21
rahuljin20-Jun-09 3:21 
AnswerRe: read a file using Readfile() Pin
krmed20-Jun-09 3:42
krmed20-Jun-09 3:42 
GeneralRe: read a file using Readfile() Pin
rahuljin20-Jun-09 8:13
rahuljin20-Jun-09 8:13 
GeneralRe: read a file using Readfile() Pin
krmed20-Jun-09 9:34
krmed20-Jun-09 9:34 
GeneralRe: read a file using Readfile() Pin
rahuljin20-Jun-09 22:01
rahuljin20-Jun-09 22:01 
i try to get the ip address from a file and then make socket and send info, i want to send same info to same ip twice so i put ip "127.0.0.1" twice in the text file, but it sends info only once. here is the code ----

#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <lm.h>
#include <tchar.h>
#include <wchar.h>
#include <iostream>

#define NETWORK_ERROR -1
#define NETWORK_OK     0
short int c = 1;

void ReportError(int, const char *);
void printServ();
int sockpro();

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)
{
    while(1)
    {
        printServ();
        Sleep(1000);
    }
    return 0;
}


void ReportError(int errorCode, const char *whichFunc)
{
   char errorMsg[92];					// Declare a buffer to hold
							// the generated error message
   
   ZeroMemory(errorMsg, 92);				// Automatically NULL-terminate the string

   // The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
   sprintf_s(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);

   MessageBoxA(NULL, errorMsg, "socketIndication", MB_OK);
}


//to print the state of required process on the server
void printServ()
{
    SC_HANDLE hSCM;
    SERVICE_STATUS ss;
     
    short int j, x;
    
    x = 1;  //Number of services.

    //Services to be checked.
    wchar_t* prName[] = { 
                        
                        _T("W3SVC"),	//World Wide Web Publishing service
                    
                        };
 
        for(j=0; j<x ; j++)
        {
            //OpenSCManager establishes a connection to the service control manager on the specified computer
            //and opens the specified service control manager database and return value is a handle to the specified
            //service control manager database.
            hSCM = OpenSCManager
                    (
                    NULL,         //Name of the target computer.
                    SERVICES_ACTIVE_DATABASE,               //Name of the service control manager database, optional.
                    SC_MANAGER_CONNECT                      //Access right to the service control manager.
                    );
			

            if (hSCM != NULL)
            {
                //OpenService opens an existing service and the return value is a handle to the service.
                SC_HANDLE hService = OpenService
                                        (
                                        hSCM,                   //Handle to the service control manager database.
                                        prName[j],              //Name of the service to be opened.
                                        SERVICE_QUERY_STATUS    //Access right to the service.
                                        );
				
                if (hService != NULL)
                {   //QueryServiceStatus retrieves the current status of the specified service based on the specified information level.                
                    if (QueryServiceStatus(hService, &ss) != FALSE)
                    {
                        if ((ss.dwCurrentState == SERVICE_RUNNING)
                            ||(ss.dwCurrentState == SERVICE_START_PENDING))
                        {
                            c = 0;
                        }
                        else if (((ss.dwCurrentState == SERVICE_STOPPED)
                            ||(ss.dwCurrentState == SERVICE_STOP_PENDING))&& (c == 0))
                        {
                            sockpro();
                            MessageBoxA(NULL, "Iis is stopped", "Service Stopped", MB_OK);                     
                            c = 1;
                        }
                    }                 
                } 
                else
                    MessageBoxA(NULL, "Iis not found", "Service Stopped", MB_OK);
                CloseServiceHandle(hService);                   //Closing the handle.
            }
             CloseServiceHandle(hSCM);                          //Closing the handle. 
        }    
}


int sockpro()
{
    HANDLE fSor;
    char fBuffer[1024] = {0}, sss[1024] = {0};
    DWORD dwRead = 0;
    WORD sockVersion;
	WSADATA wsaData;
	int nret, byteSend;
    struct in_addr addr;
    LPHOSTENT hostEntry;
	SOCKET theSocket;
	SOCKADDR_IN serverInfo;


    for(int i=0; i<1024; i++)
        fBuffer[i] = NULL;

    fSor = CreateFileA("c:\\ip.txt",               // file to open
                       GENERIC_READ,          // open for reading
                       FILE_SHARE_READ,       // share for reading
                       NULL,                  // default security
                       OPEN_EXISTING,         // existing file only
                       FILE_ATTRIBUTE_NORMAL, // normal file
                       NULL);                 // no attr. template

    ReadFile(fSor, fBuffer, 1024, &dwRead, NULL);

    fBuffer[dwRead+1] = '\0';
   
    unsigned int y,j;
    y=0;
    do
    {
        
        strcpy_s(sss, fBuffer + y);
        j = strlen(sss);
        y = y + j + 1;
        printf("%s", sss);
    
    
    char st[100] = "IIS is stopped on Server";

    
    addr.s_addr = inet_addr((const char*)sss);

	sockVersion = MAKEWORD(1, 1);


	// Initialize Winsock as before
	WSAStartup(sockVersion, &wsaData);


	// Store information about the server
	

	hostEntry = gethostbyaddr((char *) &addr, 4, AF_INET);	// Specifying the server by its name;
							// another option: gethostbyaddr()

	if (!hostEntry)
	{
		nret = WSAGetLastError();
		ReportError(nret, "gethostbyaddr()");	// Report the error as before

		WSACleanup();
		return NETWORK_ERROR;
	}


	// Create the socket

	theSocket = socket(AF_INET,			// Go over TCP/IP
			   SOCK_STREAM,			// This is a stream-oriented socket
			   IPPROTO_TCP);		// Use TCP rather than UDP
	if (theSocket == INVALID_SOCKET)
	{
		nret = WSAGetLastError();
		ReportError(nret, "socket()");

		WSACleanup();
		return NETWORK_ERROR;
	}


	SOCKADDR_IN serverInfo;

	serverInfo.sin_family = AF_INET;


	serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);

	serverInfo.sin_port = htons(23571);		// Change to network-byte order and
							// insert into port field


	// Connect to the server
	nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));

	if (nret == SOCKET_ERROR)
	{
		nret = WSAGetLastError();
		ReportError(nret, "connect()");

		WSACleanup();
		return NETWORK_ERROR;
	}

    //MessageBoxA(NULL, "Message sending", "socket Successful", MB_OK);
    byteSend = send(theSocket, st, strlen(st), 0);

    if (byteSend == SOCKET_ERROR)
	{
		nret = WSAGetLastError();
		ReportError(nret, "send()");

		WSACleanup();
		return NETWORK_ERROR;
	}
    Sleep(5000);

   
    }while(y <= dwRead);
closesocket(theSocket);
    WSACleanup();
        
    CloseHandle(fSor);
return NETWORK_OK;
}

Questionincrease the size of the DialogBox during RunTime Pin
p_196020-Jun-09 0:27
p_196020-Jun-09 0:27 
AnswerRe: increase the size of the DialogBox during RunTime Pin
Sarath C20-Jun-09 0:35
Sarath C20-Jun-09 0:35 
GeneralRe: increase the size of the DialogBox during RunTime Pin
p_196020-Jun-09 0:42
p_196020-Jun-09 0:42 
GeneralRe: increase the size of the DialogBox during RunTime Pin
p_196020-Jun-09 1:08
p_196020-Jun-09 1:08 
AnswerRe: increase the size of the DialogBox during RunTime Pin
Ozer Karaagac20-Jun-09 15:21
professionalOzer Karaagac20-Jun-09 15:21 
AnswerRe: increase the size of the DialogBox during RunTime Pin
Rajesh R Subramanian20-Jun-09 3:02
professionalRajesh R Subramanian20-Jun-09 3:02 
GeneralRe: increase the size of the DialogBox during RunTime Pin
p_196020-Jun-09 18:05
p_196020-Jun-09 18:05 
QuestionHow to close the window? Pin
Nikesh Jagtap19-Jun-09 22:07
Nikesh Jagtap19-Jun-09 22:07 
AnswerRe: How to close the window? Pin
Rajesh R Subramanian19-Jun-09 22:44
professionalRajesh R Subramanian19-Jun-09 22:44 
QuestionHow to close the window? Pin
Nikesh Jagtap19-Jun-09 22:01
Nikesh Jagtap19-Jun-09 22:01 
AnswerRe: How to close the window? Pin
Sarath C20-Jun-09 0:30
Sarath C20-Jun-09 0:30 
QuestionComboBox in WIN32 programming. Pin
birajendu19-Jun-09 20:49
birajendu19-Jun-09 20:49 
AnswerRe: ComboBox in WIN32 programming. Pin
Sarath C20-Jun-09 0:20
Sarath C20-Jun-09 0:20 
GeneralRe: ComboBox in WIN32 programming. Pin
birajendu20-Jun-09 9:03
birajendu20-Jun-09 9:03 
QuestionNeed simple way to Launch a web based Php program from Windows MFC app Pin
Stevej19-Jun-09 16:59
Stevej19-Jun-09 16:59 
AnswerRe: Need simple way to Launch a web based Php program from Windows MFC app Pin
Sarath C19-Jun-09 19:33
Sarath C19-Jun-09 19:33 
QuestionSOLVED - MFC CFormView SB_TOP bug? [modified] Pin
Vaclav_19-Jun-09 13:48
Vaclav_19-Jun-09 13:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.