Coding in / for linux

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1233 Views - Last Post: 18 October 2010 - 09:12 PM Rate Topic: -----

#1 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Coding in / for linux

Posted 17 October 2010 - 05:33 AM

ok so if loaded ubuntu on a virtual machine, and downloaded net beans.

since this is the first time iv ever programed for linux, iv got a few questions.
but not many since its only a small program.

so for started, im rather used to programming in C++, but not so much in C.
would it be better to program my program in the C language? or does C++ work just as well in linux with the proper compiling libraries? (would the using linux system need the same C++ libraries?

(should also be noted that i have written the program for windows, i just want to expand this program to work on linux as well)

Is This A Good Question/Topic? 0
  • +

Replies To: Coding in / for linux

#2 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: Coding in / for linux

Posted 17 October 2010 - 05:53 AM

Depends on what your program is doing.

For general purpose stuff, you're fine using C or C++ (or any other language for that matter). I believe the kernel is in C, so if you're messing with that then C might be better.
Was This Post Helpful? 0
  • +
  • -

#3 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Coding in / for linux

Posted 17 October 2010 - 06:09 AM

View Postelgose, on 17 October 2010 - 04:53 AM, said:

Depends on what your program is doing.

For general purpose stuff, you're fine using C or C++ (or any other language for that matter). I believe the kernel is in C, so if you're messing with that then C might be better.


pretty much the program is reading from different files at once (on difference threads) and sending them to another program using socket programming (sys/socket.h)
Was This Post Helpful? 0
  • +
  • -

#4 elgose  Icon User is offline

  • D.I.C Head

Reputation: 102
  • View blog
  • Posts: 228
  • Joined: 03-December 09

Re: Coding in / for linux

Posted 17 October 2010 - 06:31 AM

I've never tackled sockets or much in Linux beyond simple programs. I can't imagine there'd be a reason why you cannot use C++ for that. Have you tried running some code to see what happens?
Was This Post Helpful? 0
  • +
  • -

#5 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Coding in / for linux

Posted 17 October 2010 - 06:38 AM

View Postelgose, on 17 October 2010 - 05:31 AM, said:

I've never tackled sockets or much in Linux beyond simple programs. I can't imagine there'd be a reason why you cannot use C++ for that. Have you tried running some code to see what happens?


well iv yes iv tried using some C++ libraries.
my first problem came before i even created the socket connection.
the program receives the server address and port from a file.
but for some reason, the file fails to open


	string filename = "config.txt";
        std::ifstream config;
	config.open(filename.c_str(), fstream::beg);
	if(config.is_open())
	{
		getline(config,port);
		getline(config,address);
		config.close();
	}else{
		cout<< "this isnt working captain \n"
			<< "I cannot open the file!!\n";
	}



is the fstream library platform specific?
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Coding in / for linux

Posted 17 October 2010 - 07:34 AM

Are you getting any warning/error messages when you try to compile?

   config.open(filename.c_str(), fstream::beg);
   if(config.is_open())


Your fstream::beg is incorrect.
1. If you are not using "using namespace std;" somewhere in your code then
it would be:
config.open(filename.c_str)), std::ios::beg)

2. There is no std::ios::beg or std::fstream::beg

3. In your code you should not need any open flags.
 config.open(filename.c_str());

should be all you need for now.


Jim

This post has been edited by jimblumberg: 17 October 2010 - 07:35 AM

Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,440
  • Joined: 23-August 08

Re: Coding in / for linux

Posted 17 October 2010 - 07:37 AM

ifstream::open. Where do you see fstream::beg in there as a valid parameter?
Was This Post Helpful? 0
  • +
  • -

#8 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Coding in / for linux

Posted 17 October 2010 - 09:48 PM

ok, so here is the new code
and yes, using namespace std is writtin above the code.
but it still does not open the file.

    string filename = "config.txt";

    std::ifstream config;
    config.open(filename.c_str());
    if(config.is_open())
    {
        getline(config, port);
        getline(config, address);
        getline(config, clientNum);
        getline(config, clientDets);

        config.close();
    }else{
        cout<<"Unable to get address details from config.txt\n";
        cin;
    }

Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Coding in / for linux

Posted 18 October 2010 - 05:52 AM

Do you get any error/warning messages when you compile?

Is the "config.txt" file in the same directroy as the executable?

Do you have permission to open the "config.txt" file?

And is the current program printing "Unable to get address details from config.txt"?

If the answers are no, yes, yes, yes then post a compilable program that demonstrates the problem. If you are getting any errors/warnings please post them also. Also post a sample of your "config.txt" file.


Jim
Was This Post Helpful? 0
  • +
  • -

#10 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Coding in / for linux

Posted 18 October 2010 - 07:48 PM

as you predicted, the answer is no, yes yes yes.
although im not sure on the permissions part. Ubuntu is so strict on everything, even logged in on the only account (admin account) trying to install a program asks for a password.

at any rate, my code as requested,
/*
 * File:   main.cpp
 * Author: lars
 *
 * Created on 6 October 2010, 9:05 PM
 */

#define _CRT_SECURE_NO_DEPRECATE

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>
#include <fstream>
#include <string.h>
#include <pthread.h>
using namespace std;

//file reading loops
/*---------------------
 * these two functions read logs
 * and send them to the server.
 */
void *fileProtOne(void *ptr);
void *fileProtTwo(void *ptr);

//send data functions
bool sendData(string data);

//connection function
int StartClient(unsigned short port, const char* serverName);

//end client functions
void EndClient();

//variables
int conSocket;
bool CONNECTED = false;
bool SENDING = false;




/*
 *
 */
int main(int argc, char** argv) {

    string port = "";
    string address = "";
    string clientNum, clientDets;

    string filename = "config.txt";


    std::ifstream config;
    config.open(filename.c_str());
    if(config.is_open())
    {
        getline(config, port);
        getline(config, address);
        getline(config, clientNum);
        getline(config, clientDets);

        config.close();
    }else{
        cout<<"Unable to get address details from config.txt\n";
        cin;
    }

    int port2 = atoi(port.c_str());
    int clientNum2 = atoi(clientNum.c_str());
    char* addr;
    addr = (char*)address.c_str();


    conSocket = StartClient(port2, addr);
    if(conSocket == -1)
    {
        return (EXIT_SUCCESS);
    }
    char myhostname[256];
    int rc = gethostname(myhostname, sizeof myhostname);
    clientNum = myhostname;

    if(!sendData(clientNum))
    {
        cout << "Client: Unable to send client Name\n Press any key\n";
        getchar();
        close(conSocket);
        return (EXIT_SUCCESS);
    }

    if(!sendData(clientDets))
    {
        cout << "Client: Unable to send client details\n Press any key\n";
        getchar();
        close(conSocket);
        return(EXIT_SUCCESS);
    }
    CONNECTED = true;

    //start two threads here;
    pthread_t thread1, thread2;
    int iret1, iret2;
    int number = 1;

    iret1 = pthread_create(&thread1, NULL, fileProtOne, (void *)number);
    iret2 = pthread_create(&thread2, NULL, fileProtTwo, (void *)number);

    while(CONNECTED)
    {
        int c;
        c = getchar();
        if(c == 81 || c == 113)
        {
            CONNECTED = false;
        }
    }

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    return (EXIT_SUCCESS);
}

int StartClient(unsigned short port, const char* serverName)
{
    //int mysocket, portno, n;
    struct sockaddr_in server;
    struct hostent *host_entry;

    int mySocket = socket(AF_INET, SOCK_STREAM, 0);
    if(mySocket == -1)
    {
        cout << "Client: Fail to initiate socket\nPress any key\n";
        getchar();
        close(mySocket);
        return -1;
    }

    if((host_entry = gethostbyname(serverName)) == NULL)
    {
        cout << "Client: Unable to locate Host\n Press any key to continue\n";
        getchar();
        close(mySocket);
        return -1;
    }

    server.sin_family = AF_INET;
    server.sin_port = htons(port);
    server.sin_addr.s_addr = *(unsigned long*)host_entry->h_addr;
    if(connect(mySocket, (sockaddr*)&server, sizeof(server)) == -1)
    {
        cout << "Client: Unable to connect to server\n Press any key\n";
        getchar();
        close(mySocket);
        return -1;
    }

    cout << "Client: Connected";

    return mySocket;
}

bool sendData(string data)
{
    int bytes;
    unsigned long messageSize = strlen(data.c_str());
    messageSize = htonl(messageSize);
    if((bytes = send(conSocket, (char*)&messageSize, sizeof(messageSize), 0)) < 0)
    {
        
        return false;
    }
    messageSize = ntohl(messageSize);
    if((bytes = send(conSocket, (char*)data.c_str(), messageSize, 0)) < 0)
    {
        return false;
    }
    return true;
}

void *fileProtOne(void *ptr)
{
    int logline = 0;
    int backline = 0;
    ifstream log;
    log.open("http.txt");
    string logMessage;
    string backMessage;

    while(CONNECTED)
    {
        while(!log.eof())
        {
            sleep(10);
            getline(log, logMessage);
            if(logMessage == "")
            {
                if(strlen(logMessage.c_str()) > 50)
                {
                    if(logMessage != backMessage)
                    {

                        backMessage = logMessage;
                        while(SENDING)
                        {
                            sleep(5);
                        }
                        SENDING = true;
                        if(!sendData("HTTP"))
                        {
                            //do somthing here
                        }
                        if(!sendData(logMessage))
                        {
                            //so somthing here
                        }
                    }
                }
            }
            logline = log.tellg();
            if(logline != -1)
                backline = logline;
        }
        log.close();
        log.clear();
        log.open("http.txt");
    }
}
void *fileProtTwo(void *ptr)
{
    int logline = 0;
    int backline = 0;
    ifstream log;
    log.open("http.txt");
    string logMessage;
    string backMessage;

    while(CONNECTED)
    {
        while(!log.eof())
        {
            sleep(10);
            getline(log, logMessage);
            if(logMessage == "" && (strlen(logMessage.c_str()) > 50) && logMessage != backMessage)
            {
                backMessage = logMessage;
                while(SENDING)
                {
                    sleep(5);
                }
                SENDING = true;
                if(!sendData("HTTP"))
                {
                    //do somthing here
                }
                if(!sendData(logMessage))
                {
                    //so somthing here
                }
            }
            logline = log.tellg();
            if(logline != -1)
                backline = logline;
        }
        log.close();
        log.clear();
        log.open("http.txt");
    }
}


now, disregard the threaded fucntions, as i cant get to them before im able to connect to the server. (which cant be done whilst i cant get access to the text file).

config.txt -- This file is in the same folder as the application.
7700
localhost
2222
aclient

Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3993
  • View blog
  • Posts: 12,323
  • Joined: 25-December 09

Re: Coding in / for linux

Posted 18 October 2010 - 08:15 PM

How are you compiling the code? Are you using an IDE or command line?

When I run the code I am able to read the config.txt file. Check your permissions and insure the file is in the correct directory.

These are the errors I get when I compile the code:

Quote

main.cpp||In function ‘int main(int, char**)’:|
main.cpp|70|warning: statement has no effect|
main.cpp|74|warning: unused variable ‘clientNum2’|
main.cpp|85|warning: unused variable ‘rc’|
main.cpp|49|warning: unused parameter ‘argc’|
main.cpp|49|warning: unused parameter ‘argv’|
main.cpp||In function ‘void* fileProtOne(void*)’:|
main.cpp|233|warning: no return statement in function returning non-void|
main.cpp|186|warning: unused parameter ‘ptr’|
main.cpp||In function ‘void* fileProtTwo(void*)’:|
main.cpp|274|warning: no return statement in function returning non-void|
main.cpp|234|warning: unused parameter ‘ptr’|
||=== Build finished: 0 errors, 9 warnings ===|


The last warnings about no return statement and unused parameter should be fixed.
Line numbers 233, 186, 274, 234.

And you need to increase the warning level.


Jim
Was This Post Helpful? 0
  • +
  • -

#12 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Coding in / for linux

Posted 18 October 2010 - 08:30 PM

as i said in the first post, i am using netbeans 6.8.

also the unused variables in the threaded functions are just to fill. i find pthreads a real pain in the ass to use, especially when i dont need any variables parsed to the function.

iv tried playing around with the permissions of the textfile, set it all to read and write and what not, the most open privs possible, and it still fails to open.
im thinking, maybe the netbeans debugger is looking in the wrong place? i currently have the textfile with the executable, in the project/dist/debug/GNU-Linux-x86

i will attend to the warning, when i can get the program working as it should.
Was This Post Helpful? 0
  • +
  • -

#13 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Coding in / for linux

Posted 18 October 2010 - 08:30 PM

First thing.
Look at the problems the compiler has with your code
DIC.cpp: In function ‘int main(int, char**)’:
DIC.cpp:70: warning: statement has no effect
DIC.cpp:74: warning: unused variable ‘clientNum2’
DIC.cpp:85: warning: unused variable ‘rc’
DIC.cpp: In function ‘void* fileProtTwo(void*)’:
DIC.cpp:274: warning: control reaches end of non-void function
DIC.cpp: In function ‘void* fileProtOne(void*)’:
DIC.cpp:233: warning: control reaches end of non-void function



This is line 70
        cin;


any decent compiler will warn you about a line like that if you ask it to warn you.

So your most important lesson here is to learn how to turn on the display of warnings in your compiler.
Do you know how to do that?
From here on always ensure the compiler gives you maximum help by turning on the display of warnings.
Errors are not enough. You need to see and fix warnings too.
Was This Post Helpful? 0
  • +
  • -

#14 Larsonator  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 77
  • Joined: 13-August 10

Re: Coding in / for linux

Posted 18 October 2010 - 08:34 PM

my last theory is correct. i only had the text file placed where the application is. how ever, while debugging in the IDE, it looks in a different directory for the text file, although i am not sure which one yet, as i just placed a copy of the text file in every directory relevant to the project. (lol)
Was This Post Helpful? 0
  • +
  • -

#15 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Coding in / for linux

Posted 18 October 2010 - 08:39 PM

View PostLarsonator, on 19 October 2010 - 12:30 PM, said:

i will attend to the warning, when i can get the program working as it should.


You might as well say I will deal with the poison in the food after I have eaten it.
I'm hungry now.

If you know your code is bad why would you not fix it?
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2