• 0

A first venture in network programming


Question

Here's what I want to do. There will be a C++ application running on a remote machine, doing some testing. Then there will be a python application on the client machine: it will allow the user to tell to the remote machine what tests must be done, see the results of the tests and some tests may require human intervention so the python app must also provide that. So basically I need to have messages going two ways. Also the C++ app may need to restart, breaking the connection so we must be able to detect that and restore the connection.

Both apps should run on Linux, portability on Windows would be appreciated though not mandatory (if it means twice the work forget it).

I did a quick tutorial for socket programming in Python, and another one in C, I even managed to get the python client to connect to the C server, but I'm still quite lost.

- Python is portable but not C++ (winsock vs unistd); should I try to use Boost::Asio or some other portable library?

- Aside from basic tutorials like the ones I linked to, the topic explodes really fast and I feel like I don't need to absorb all that information; this should be something simple, just two applications sending a few messages to each other over TCP. Can you give some pointers on how to implement this?

By the way this is not homework, the point is to get it done, so I'm all for using existing components if they do the job instead of writing a lot of boilerplate code.

Thanks.

Edited by Dr_Asik
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

When I was on placement last year I did some sockets programming in C and to get me started I used the guide here - http://beej.us/guide/bgnet/

I found it pretty useful for getting to grips with socket programming. For my program I ended up implement a custom protocol so I could send messages between a client and server. You could maybe do something similar, so the Python client can communicate with the C server.

You only really need a basic send() and receive function, which either side can decode and realise what each message means. I've no idea how this works in Python though.

Link to comment
Share on other sites

  • 0

Boost::Asio and Qt's Network Libs are both great portable solutions. Personally I'd just write both apps in Python, though. Unless there is a dire need for C++... which I doubt.

Link to comment
Share on other sites

  • 0
Boost::Asio and Qt's Network Libs are both great portable solutions. Personally I'd just write both apps in Python, though. Unless there is a dire need for C++... which I doubt.
I'd be the first one to avoid C++, unfortunately I have no choice. Well it's either that or C, so I prefer C++. :p
Link to comment
Share on other sites

  • 0

Thanks for the link Vizion, it's very helpful. I have a question about one the examples : see http://beej.us/guide/bgnet/output/html/mul...ientserver.html , the server code (more specifically this code).

After he starts listening on the socket, he does :

	sa.sa_handler = sigchld_handler; // reap all dead processes
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	if (sigaction(SIGCHLD, &sa, NULL) == -1) {
		perror("sigaction");
		exit(1);
	}

Where sa is a struct sigaction, and sigchld_handler is the following function :

void sigchld_handler(int s)
{
	while(waitpid(-1, NULL, WNOHANG) > 0);
}

I don't get why he would "reap all dead processes": he didn't spawn any yet as far as I can see, no calls to fork().

Also I have difficulty telling whether my application will need to spawn any additional process. It will only ever connect to a single client at a time, performance isn't much of a concern, communication can be strictly synchronous : [client : "do A" *waits for server / server : "I'm done, here are the results" * waits for client ] etc.

Thanks in advance.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.