1 /*****************************************************************************/
2 /* 8888888 88888888 88888888 */
5 /* 8 88888888 88888888 */
8 /* 888888 888888888 888888888 */
10 /* A Two-Dimensional General Purpose Semiconductor Simulator. */
13 /* Last update: March 29, 2007 */
17 /* NINT, No.69 P.O.Box, Xi'an City, China */
19 /*****************************************************************************/
27 /* This function gets called whenever the pipe broken.
28 See the signal(2) manpage for more information. */
29 inline void signal_handler(int signum
)
34 printf("Received signal SIGPIPE. It seems ngspice terminated.\n");
35 siglongjmp(net_buf
,1);
41 /* ----------------------------------------------------------------------------
42 * NDEVServer: This function set TCP/IP socket and connect to NGSPICE.
44 int NDEVServer(int port
,int &listener
, int &client
)
46 char dotted_ip
[15]; /* Buffer for converting the resolved address to a readable format. */
47 struct sockaddr_in sa
; /* Connection address. */
50 signal(SIGPIPE
, signal_handler
);
51 listener
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_IP
);
54 gss_log
.string_buf()<<"Unable to create a listener socket: "<<strerror(errno
)<<"\n";
58 /* Now bind the listener to a local address. This uses
59 the same sockaddr_in structure as connect. */
63 sa
.sin_family
= AF_INET
;
64 sa
.sin_port
= htons(port
);
65 sa
.sin_addr
.s_addr
= htonl(INADDR_ANY
); /* Listen on all interfaces. */
66 if (bind(listener
, (sockaddr
*)&sa
, sa_len
) < 0)
68 gss_log
.string_buf()<<"Unable to bind to port "<<port
<<": "<<strerror(errno
)<<"\n";
72 /* Let the networking system know we're accepting
73 connections on this socket. Ask for a connection
74 queue of five clients. (If more than five clients
75 try to connect before we call accept, some will
77 if (listen(listener
, 1) < 0)
79 gss_log
.string_buf()<<"Unable to listen: "<<strerror(errno
)<<"\n";
83 gss_log
.string_buf()<<"Waiting for ngspice...";
85 client
= accept(listener
, (sockaddr
*)&sa
, &sa_len
);
86 /* We now have a live client. Print information
87 about it and then send something over the wire. */
88 inet_ntop(AF_INET
, &sa
.sin_addr
, dotted_ip
, 15);
90 /* Waiting for ngspice query data */
91 recv(client
,sendbuf
,128,MSG_FLAG
);
92 if(strcmp(sendbuf
,NG_QUERY
)) /* Query error */
94 gss_log
.string_buf()<<"Query information error from ngspice.\n";
98 gss_log
.string_buf()<<"Received ngspice connection from "<<dotted_ip
<<"\n";
101 sprintf(sendbuf
,NDEV_REPLY
);
102 send(client
,sendbuf
,128,0);