# An example of a server socket
  # Add this mh.ini parm:  server_speak_port = 8090
  $socket_server = new  Socket_Item(undef, undef, 'server_speak');
  if (my $data = said $socket_server) {
    print "speak_server socket data: $data\n";
    speak $data;

  # An example of a client socket
  my $lcdproc_address = 'misterhouse:13666';
  $lcdproc = new  Socket_Item(undef, undef, $lcdproc_address, 'lcdproc');

  $Vcmd_viavoice = new  main::Socket_Item(undef, undef, 'localhost:3234', 'viavoice');

  # This example walks the reboot menu of a router
  $router_reboot = new Voice_Cmd 'Reboot the router';
  $router_client = new Socket_Item(undef, undef, $config_parms{router_address} . ":23", 'router', 'tcp', 'raw');
  set_expect $router_client (Password => $config_parms{router_password}, Number => 24, Number => 4, Number => 11) if said $router_reboot;

  # Example of sending a message to all clients
  set $telnet_server "Hi to all clients", 'all';


Read/Write to TCP/IP socket ports.




new('data_stream', 'state_name', 'host_port', 'name', 'host_protocol', 'datatype', 'break_char')

Allows you to change the server_name/port.

add('data_stream', 'state_name')

'host_port' - must be of the form ip_address:port (when creating a client) or it must match a server_* parm in mh.ini (when creating a server) (e.g. server_telnet_port).

'host_protocol' - can be 'tcp' or 'udp'. Default is 'tcp'

'datatype' - can be 'raw', 'rawout', or 'record'. If 'raw', the said method returns data as read. If record, said only returns data when reaching a newline. When writing to the port, a newline is added unless datatype is 'raw' or 'rawout'.

'break_char' - This will be added to data sent with set and, unless datatype=raw, will be used to break incoming data into state_now records. Default is \n\r.

'name' - is optional. If used you can set debug to this string to turn on debug for just this socket data.


Connect to the specified port. This allows mh to act as a client, rather than a server, and initiate communications with a server.


This will drop the client or server from mh and free up the port.


Returns 1 if the socket is available, 0 if not


True if the port is active.


True for the mh pass that the socket first becomes active.


True for the mh pass that the socket first becomes inactive.


Returns a data record received by the port. Note: If you want to process binary socket data, specify server_*_datatype = raw in the mh.ini file. This will cause said to return any data read immediately, rather than buffering up data until a newline is read.


Reads and returns the next record from the socket handle


Returns the socket handle, so you can loop on reading/writing data to it directly.


Use this to control echoing of incoming characters: - set_echo $my_socket 0 -> Do not echo incoming characters - set_echo $my_socket 1 -> echo incoming characters - set_echo $my_socket '*' -> echo incoming characters with *


Sets the item to the specified state. - If the specified state has not been defined with 'new' or 'add', the state data is sent. Otherwise the data_stream associated with that state is sent. - If there is more than one client connected to this port, an optional 2nd parm can be used to pick which client set to. Values are: - 'all' (send to all connected clients), - ip_address (a regular expresion of the ip address(s) you want to send to - client_number (which client to send to, 0 .. #). Note this can change as clients are added and dropped. - socket hash name, which can be captured when reading data from the port with this: $client = $Socket_Ports{$port_name}{socka};


Used to send a series of strings to the port, waiting for a specified prompt for each string. This is useful for walking through menus. The arguments passed to set_expect are pairs of 'prompt' => 'response' strings. See example below.

  sub set_expect {
    my ($self, @set_expect_cmds) = @_;
    if (active $self) {
        &main::print_log("set_expect: $$self{port_name} is already active");
    else {
        &main::print_log("set_expect: $$self{port_name} start");
        @{$$self{set_expect_cmds}}  = @set_expect_cmds;
        $$self{set_expect_timer} = new Timer;
        $$self{set_expect_timer}-> set(10);
        &::MainLoop_pre_add_hook( \&Socket_Item::set_expect_check, 0, $self );



Returns the last state that was received or sent


Returns the state that was received or sent in the current pass.


Server socket ports are specified with mh.ini parms (see 'server options' in mh.ini)




More examples are in mh/code/bruce/, mh/code/examples/socket_test*.pl, mh/code/bruce/, bruce/, and bruce/


This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.