Coders4fun Quiz #1

Categories: C, Quiz

Tags:

What will be printed using this c++ code?

#include <iostream>

int main()
{
	int i = 0;
	for (i = i == 0; i < 9; i = 1 + i * 2);
		std::cout<<i<<std::endl;
	return 0;
}

Please don’t cheat using a compiler! :)

P.S.
Of course the example compiles!

UDPListener: Receiving UDP messages in C#

Categories: C, Source Code, UDP Communication

Tags:

This post is part of the UDPCommunication project.

UDP - User Datagram Protocol

The class in this post allows, in an easy and fast manner, to receive UDP messages. You don’t need to be a socket expert to use the class, cause you only need to write few rows of code to use it. Following, a little example showing how to use the class:

private void InitConnection()
{
	m_udpListener = new UDPListener(m_port, new byte[4] { 0, 0, 0, 0 });
	m_udpListener.MessageReceived += new UDPMessageReceivedDelegate(udpListener_MessageReceived);
}

Continue Reading »

W Firefox! :D

Categories: News, Site

Tags:

This post is dedicated to all non-IE users of coders4fun:
firefox batte ie

THANKS!!

Reboot or Shutdown a pc

Categories: C, Source Code, Windows

Tags:

This is a little how-to that explains how to shutdown or reboot a Windows machine in c++ .
In the source code you will find the function ShutDown, definied as follow:

bool ShutDown(bool restart)

The function shutdowns or reboots the pc, and returns false if the shutdown can’t be done (for example a process denied the power off).

Shut Down

So, to shutdown the pc, you only have to call the function and check if the shutdown was done.

if (!ShutDown(false))
{
	// shutdown not done.
}

Part of source code following:
Continue Reading »

UDPSender: Sending UDP messages in C#

Categories: C, Source Code, UDP Communication

Tags:

This post is part of the UDPCommunication project.

Header UDP

This is a class that allows to send UDP messages in C# with an unbelievable semplicity.
To create the class you only need to create it, specifying a port for the udp communication, eg 3125 in this case:

try
{
	INetworkSender udpSender = new UDPSender(3125)
}
catch(UDPSenderException e)
{
        	// error during the socket initialization
}

Sending a message to an host is simple: call the SendMessage method specifying the receiver (with an host name, like “www.coders4fun.com“, or with an ip, like “192.168.10.6“) and a byte array containing the message.
Continue Reading »

Wordpress Plugin: Wiki-Dashboard 0.1

Categories: News, Source Code, Wiki-Dashboard, Wordpress, php

Tags:

Wiki-Dashboard is a Wordpress plugin that allows to all registered user on a blog to share and edit a text like a wiki, and can be usefull even on mono-author blogs, giving a space to write notes.

Features:

  • Dashboard subpage with the text where to put notes
  • Text shared to all blog authors
  • bbCode parser for text formatting
  • Internationalization support

Installation:
Simply download the Zip-Archive and extract all files into your wp-content/plugins/ directory. Then go into your WordPress administration page, click on Plugins and activate it.
After that you will have a new submenu called “Wiki” under the “Dashboard” menu.

Download Wiki Dashboard 0.1. Downloads: 2694

Screenshots:
Wiki-Dashboard showing the text:

Wiki-Dashboard View

Wiki-Dashboard editing the text:

Wiki-Dashboard Edit
Continue Reading »

Edit form elements from a Thread

Categories: C, Source Code

Tags:

Scenario:
The program has to execute a long operation (such as a downloaded file from the internet) when the user clicks on a button, but during the operation the user has the opportunity to interact with the program, and the program gives a visual feedback of the task completion percentage.
Easy, but not working, solution:
To solve a problem like that we need to use a Thread that executes the task in a separate context from the main Form, otherwise, if we execute the long operation in the onClick method of the button, the form will remain frozen during the task execution. The problem comes out when the program tries to update the form from the thread:

private void button1_Click(object sender, EventArgs e)
{
    testThread = new Thread(threadProc);
    testThread.Start();
}

public void threadProc()
{
    // wast a lot of time...
    for (int i = 1; i <= 10; i++)
    {
        Thread.Sleep(250);
        progressBar.Value = i * 10;
    }
    MessageBox.Show("Operation Completed!");
}

Unfortunately, the code that assigns the value to the progressBar will produce an exception like that:
“Illegal cross-thread operation: Control ‘progressBar1′ accessed from a thread other than the thread it was created on.”

lock

Continue Reading »

MySQLDump backup class interface

Categories: MySQL, Source Code, Web, php

Tags:

MySQLDump class is now at version 2.0 and this article contains obsolete informations about the version 1.0. For information about the new version check this article

After the success of the article PHP backup of a mysql database, I decided to write a short post about a file that can be usefull to download the backup of your db site/wordpress blog directly from your browser.
To use the script, you can simply download it and edit the first lines with the basic configuration:

// DB Configuration
$server = "IP of your server (localhost will work in most cases)";
$username = "Valid username for your mysql db";
$password = "Password";
$dbToDump = "Db to dump";
// password to access the php page. PLEASE change the default password!!!
$backupPassword = "UltraSecretKeyThatYouMUSTInsertHere";
// filename to save
$filename = "backup.sql.gz";

After that you can upload the file to your server, in the same folder where is located the class MySQLDump.
To access the script you have to specify two parameters: the password to access the script (pass), and another parameter (t) that if it’s 1 will separate the SQL inserts every 100 rows.
For example, if you upload the file to the root directory of yoursite, you have to write in the address bar of the browser:

http://www.yoursite.com/backup.php?pass=UltraSecretKeyThatYouMUSTInsertHere&t=1

…and your favorite browser will open the dialog to save the file with the db backup!

Download MySqlDump Interface.php. Downloads: 657

For more info about the MySQLDump class, read the original post, or download the source from here:

Download MySQLDump. Downloads: 4321

C# Threads using anonymous methods

Categories: C

Tags:

When we want to use a Thread in C#, we usually write a method with the code that the Thread must execute, and when we create the Thread object we set its delegate to the method already created:

public void DoSomethingUsefull()
{
	for (int i = 0; i < 100; i++)
	{
		// doing something...
		Thread.Sleep(500);
	}
}

static void Main()
{
	// here we create the thread, passing to it the right delegate
	Thread t = new Thread(DoSomethingUsefull);
	t.Name = "Test Thread";
	t.Start();
}

dual core

To create a Thread, C# offers the opportunity to use anonymous methods. To use an anonymous method, we need to write the Thread’s code inside the code that creates the Thread using the delegate keyword:
Continue Reading »

PHP backup of a mysql database

Categories: MySQL, Source Code, Web, php

Tags:

MySQLDump class is now at version 2.0 and this article contains obsolete informations about the version 1.0. For information about the new version check this article

I searched a way to do a daily backup of my wordpress blog in my home pc but I didn’t find nothing that completely satisfy me, so I decided to code it.
I separated the problem in two scripts:
The first, that is written in php and runs on the web server, makes the dump of mysql databases from my hosting provider and leaves the dump on the web server.
The other one (written in bash, and running locally) downloads the site and the mysql dump from the web server, via FTP. Unfortunately I discovered that my hosting provider (Aruba.it) didn’t supply with mysqldump its server.

Lamp

First of all, I searched for a php script that makes the direct dump of the database.

Continue Reading »

« Prev - Next »