How to write what is received in an Apache port right in mySQL?

1

Hello. I have a server Apache (XAMPP) and I need to receive messages from a device that sends messages to a specific IP and port. I would like to set Apache to listen for a port (443, for example) and to record everything that it receives / listens directly in mySQL , inserting in a specific table of the database. We will call the DB database and the table to be written is mensagens , which has the codigo (INT, auto increment), mensagem (VARCHAR(200)) fields.

Is this possible? Any suggestions?

    
asked by anonymous 23.04.2014 / 15:59

1 answer

1

Apache implements the HTTP protocol. If your application speaks HTTP, you can talk to APACHE. On the server side, Apache can forward the message to a CGI (in Python, Perl, etc.) or to a server side language, such as PHP or ASP. In CGI, PHP, ASP, etc., you get the message and write it in MySQL.

If you really only want to send a message to an IP and a port, without having to use the HTTP protocol, the easiest is to write a server in Python, Perl or Node.js. I would recommend writing a server in Node.js because it can get very good performances, since it is asynchronous.

For example, creating a server in node.js is as simple as:

require('net').createServer(function (socket) {
    socket.on('data', function (data) {
        console.log(data.toString());
    });
}).listen(7777);

Running the program with node servidor.js , it listens on port 7777. This server does not write the message in MySQL (to have only 5 rows). Write only what you receive on the console.

A customer can call and send the message. Example of a client (also in Javascript):

var s = require('net').Socket();
s.connect(7777, 'localhost');
s.write('Oi!\n');
s.on('data', function(d){
    console.log(d.toString());
});
s.end();
    
09.05.2014 / 11:21