Jump to content

Running two programs

- - - - -

  • Please log in to reply
13 replies to this topic

#1
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
Is to run two programs in the Code::Blocks? Its run each in a prompt?

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Did you mean that you want to run process from your program.

1. On Windows you can use CreateProcess, ShellExecute functions to run a process.
CreateProcess Function (Windows)
ShellExecute Function (Windows)

2. On Linux, you can use fork and exec functions to run process.

Fork, Exec and Process control

I hope this helps

Munir

#3
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I don't think he's asking about threads. In Code::Blocks you can have multiple files opened but you can only run one via their console. You can compile others and start them manually.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#4
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

mnirahd said:

Hi,

Did you mean that you want to run process from your program.

1. On Windows you can use CreateProcess, ShellExecute functions to run a process.
CreateProcess Function (Windows)
ShellExecute Function (Windows)

2. On Linux, you can use fork and exec functions to run process.

Fork, Exec and Process control

I hope this helps

Munir

How does these functions? Can I put the server to run in a process and the client in another?

#5
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Yes, you can create any process from any process using the function I mentioned above.

Could you please explain me a bit what you're actually trying to achieve specifically so I could help with that?

Munir

#6
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

mnirahd said:

Hi,

Yes, you can create any process from any process using the function I mentioned above.

Could you please explain me a bit what you're actually trying to achieve specifically so I could help with that?

Munir

I have two programs. Server program and one client program. I want to simulate the connection between client-server
Just what I need to run the server and leave him waiting for the client to initiate the connection

#7
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I understand what you're trying to do, but I'm unable to figure out what problem you're facing. You can simply run the server process, and then run the client application to simulate the connection.

Munir

#8
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

mnirahd said:

Hi,

I understand what you're trying to do, but I'm unable to figure out what problem you're facing. You can simply run the server process, and then run the client application to simulate the connection.

Munir

"You can simply run the server process, and then run the client application to simulate the connection"

It is just that, only need to run everything on a single project, so I must have only one main

#9
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

Ok - then you can use the functions as I suggested.

Use CreateProcess on Windows
Use fork, and exec on Linux.

you can see the links I've posted above, and that would surely help you to achieve the results you want.

Munir

#10
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts

mnirahd said:

Hi,

Ok - then you can use the functions as I suggested.

Use CreateProcess on Windows
Use fork, and exec on Linux.

you can see the links I've posted above, and that would surely help you to achieve the results you want.

Munir

I have a main.cpp:
#include "ExceptionSocket.h"
#include "ClientSocket.h"
#include "ServerSocket.h"
#include "Socket.h"
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char** argv)
{
    //Server
    cout << "Run...\n";
    try
    {
        ServerSocket server(10000);
        while(true)
        {
            ServerSocket n_sock;
            server.accept(n_sock);
            try
            {
                while(true)
                {
                    string data;
                    n_sock >> data;
                    n_sock << data;
                }
            }
            catch(ExceptionSocket&) {}
        }
    }
    catch(ExceptionSocket& e)
    {
        cout << "The exception was detected:" << e.description() << "\nLeaving...\n";
    }

    //Client
    try
    {
        ClientSocket client("localhost", 10000);
        string reply;
        try
        {
            client << "Test message";
            client >> reply;
        }
        catch(ExceptionSocket&) {}
        cout << "We received this response from the server:\n" << reply << "\n";
    }
    catch(ExceptionSocket& e)
    {
        std::cout << "The exception was detected:" << e.description() << "\n";
    }
    
    return 0;
}

I think I need to create a process for the server and run the client. Am I right? Just do not know how, I do not quite understand the function CreateProcess

#11
Apprentice123

Apprentice123

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 430 posts
Any ideas?

#12
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi,

I'd suggest you to create 3 projects

1. Server Application
2. Client Application
3. Simulation Application

The Simulation Application should create first Server Process, and then after a delay of few milli seconds, create Client Process. I believe this should help you.

Munir




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users