1

I have no idea how to do this please help me out:-

earlier i used to take the server ip address from a .txt file so it was easy to read the ip address from the txt file

#include ".mySocket.h"
#include "myLog.h"
#include "myException.h"
#include "myHostInfo.h"

myLog winLog;
void readServerConfig(string&);
void checkFileExistence(const string&);

int main()
{
    // Initialize the winsock library
    myTcpSocket::initialize();

    // get client's information (assume neither the name nor the address is given)
    winLog << endl;
    winLog << "Retrieve the localHost [CLIENT] name and address:" << endl;
    myHostInfo clientInfo;
    string clientName = clientInfo.getHostName();
    string clientIPAddress = clientInfo.getHostIPAddress();
    cout << "Name: " << clientName << endl;
    cout << "Address: " << clientIPAddress << endl;
    winLog << " ==> Name: " << clientName << endl;
    winLog << " ==> Address: " << clientIPAddress << endl;

    // get server's IP address and name
    string serverIPAddress = "";
    readServerConfig(serverIPAddress);
    winLog << endl;
    winLog << "Retrieve the remoteHost [SERVER] name and address:" << endl;
    winLog << " ==> the given address is " << serverIPAddress << endl;

    myHostInfo serverInfo(serverIPAddress,ADDRESS);
    string serverName = serverInfo.getHostName();
    cout << "Name: " << serverName << endl;
    cout << "Address: " << serverIPAddress << endl;
    winLog << " ==> Name: " << serverName << endl;
    winLog << " ==> Address: " << serverIPAddress << endl;

    // create the socket for client
    myTcpSocket myClient(PORTNUM);
    cout << myClient;
    winLog << "client configuation: " << endl;
    winLog << myClient;

    // connect to the server.
    cout   << "connecting to the server [" << serverName << "] ... " << endl;
    winLog << "connecting to the server [" << serverName << "] ... " << endl;
    myClient.connectToServer(serverIPAddress,ADDRESS);

    int recvBytes = 0;
    while (1)
    {
        // send message to server
        char messageToServer[MAX_MSG_LEN+1];
        memset(messageToServer,0,sizeof(messageToServer));
        cout << "[SEND] ";
        cin.getline(messageToServer,MAX_MSG_LEN);

        winLog << "[SEND] " << messageToServer << endl;
        myClient.sendMessage(string(messageToServer));

        if ( !string(messageToServer).compare("Quit") || !string(messageToServer).compare("quit") ) 
            break;

        // receive message from server
        string messageFromServer = "";
        recvBytes = myClient.recieveMessage(messageFromServer);
        if ( recvBytes == -99 ) 
            break;

        cout   << "[RECV:" << serverName << "]: " << messageFromServer << endl;
        winLog << "[RECV:" << serverName << "]: " << messageFromServer << endl;

    }

    return 1;
}

//from the following code i read the ip address from the txt file but now how to read from INI file
**void readServerConfig(string& serverIPAddr)
{
    string serverConfigFile = "serverConfig.txt";
    checkFileExistence(serverConfigFile);
    ifstream serverConfig(serverConfigFile.c_str());

    // read server's IP address
    getline(serverConfig,serverIPAddr);
    serverConfig.close();
}**

void checkFileExistence(const string& fileName)
{
    ifstream file(fileName.c_str());
    if (!file) 
    {
        cout << "Cannot continue:" << fileName << " does NOT exist!" << endl;
        exit(1);
    }
    file.close();
}

But now i have to take read the server ip address from the INI file,Now the problem is how to read the INI file to get the ip address

My INI file reader and writer

#include "iostream"
#include "IniWriter.h"
#include "IniReader.h"
int main(int argc, char * argv[])
{
    CIniWriter iniWriter(".\\Logger.ini");
    iniWriter.WriteString("Setting", "ServerIp", "192.168.15.168");
    iniWriter.WriteString("Setting", "MultiCastIp", "239.255.42.42");
    iniWriter.WriteString("Setting", "Name", "jianxx");   
    iniWriter.WriteInteger("Setting", "Age", 27); 
    iniWriter.WriteFloat("Setting", "Height", 1.82f); 
    iniWriter.WriteBoolean("Setting", "Marriage", false);  
    CIniReader iniReader(".\\Logger.ini");
    char *szName = iniReader.ReadString("Setting", "Name", "");   
    int iAge = iniReader.ReadInteger("Setting", "Age", 25); 
    float fltHieght = iniReader.ReadFloat("Setting", "Height", 1.80f); 
    bool bMarriage = iniReader.ReadBoolean("Setting", "Marriage", true); 
    char *szName1 = iniReader.ReadString("Setting", "MultiCastIp", ""); 

    std::cout<<"Name:"<<szName<<std::endl
        <<"Age:"<<iAge<<std::endl 
        <<"Height:"<<fltHieght<<std::endl 
        <<"Marriage:"<<bMarriage<<std::endl
      **<<"MultiCastIp:"<<szName1<<std::endl;//i want to read this ip**
    while(1);
    delete szName;  
    return 1;   
}

Please help me out,i have no idea how to write the following function to obtain the ip address

void readServerConfig(string& serverIPAddr)
{
    //Please guide how to write this function
}
0

3 Answers 3

2

It seems you have severely convoluted what you're trying to ask. What I'm getting from your question is that you want to parse an IP address string into an IP address, not to read it from an INI file, which you've already done.

You don't need to. The OS does it for you. Use gethostbyname is the easy answer.

If this is homework or something and you have to fill in that signature, first of all you're screwed because it won't return anything...you'll have to set some stupid global to return the value or something. However, you can get the job done with sscanf.

2

That code looks really nasty and it's not going to get prettier while you add more and more configuration options in the ini file. You should check out this nice alternative, with the boost library.

2

Keep in mind that when you need to do something that has already been done by millions of programmers before you, there's usually a way to do it easily that doesn't involve reinventing the wheel.

In this case, I'd use GetPrivateProfileString. Actually, I'd probably forget about INI files and just use the Registry instead, but it is what it is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.