1 ## Copyright (C) 2006,2007,2008 Carlo de Falco
3 ## This file is part of:
4 ## OCS - A Circuit Simulator for Octave
6 ## OCS is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation.
10 ## This program is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program (see the file LICENSE); if not,
17 ## see <http://www.gnu.org/licenses/>.
19 ## author: Carlo de Falco <cdf _AT_ users.sourceforge.net>
22 ## @deftypefn{Function File} {} utl_sbn_server(@var{port})
23 ## Listen for socket connections on port @var{port}, read a command @
24 ## and return the corresponding output to the socket.
27 function utl_sbn_server (portnum)
29 QUITMESSAGE = "quit UTLsbnserver";
30 CONFIRMMESSAGE = "confirmed";
32 ## CREATE THE SOCKET AND WAIT FOR CONNECTIONS
33 s = socket(AF_INET, SOCK_STREAM, 0);
35 error("cannot create socket\n");
38 if bind(s, portnum) < 0
39 error("bind failed\n");
43 error("listen failed\n");
52 error("connection error")
55 ## READ COMMANDS FROM THE SOCKET
58 ##IF CLIENT SENT SHUTDOWN MESSAGE EXIT
59 if strcmp (msg,QUITMESSAGE)
60 printf("client requested server shutdown, goodbye!\n");
61 disconnect(c); disconnect(s);
65 ##EXECUTE COMMANDS FROM THE CLIENT
68 ##SEND OUTPUT TO THE CLIENT
69 str = [ sprintf("%17g ",A) "\n" sprintf("%17g ",B)...
70 "\n" sprintf("%17g ",C) "\n"]
83 function msg = readstring (c)
90 while read >= BUFFER_SIZE
91 newmsg = char(recv(c, BUFFER_SIZE));