Debian Codegraph @

COSMIC/EXAMPLES/sktsig.c

raw
1 /* sktsig.c: set up a Socket so that it has a SIGIO interrupt whenever data
2 * appears on the Socket (this is a unix sysv version)
3 */
4 #include <stdio.h>
5 #include <signal.h>
6 #include <fcntl.h>
7 #include "xtdio.h"
8 #include "sockets.h"
9
10 /* ------------------------------------------------------------------------
11 * Definitions:
12 */
13 #define BUFSIZE 256
14
15 /* ------------------------------------------------------------------------
16 * Typedefs:
17 */
18
19 /* ------------------------------------------------------------------------
20 * Local Data Structures:
21 */
22
23 /* ------------------------------------------------------------------------
24 * Global Data:
25 */
26
27 /* ------------------------------------------------------------------------
28 * Explanation:
29 */
30
31 /* ------------------------------------------------------------------------
32 * Prototypes:
33 */
34 #ifdef __PROTOTYPE__
35 int main( int, char **); /* sktsig.c */
36 # ifdef __gnu_linux__
37 void sigio_handler(int); /* sktsig.c */
38 # else
39 void sigio_handler(int,int,struct sigcontext *); /* sktsig.c */
40 # endif
41 #else
42 int main(); /* sktsig.c */
43 void sigio_handler(); /* sktsig.c */
44 #endif
45
46 /* ------------------------------------------------------------------------
47 * Source Code:
48 */
49
50 /* main: */
51 #ifdef __PROTOTYPE__
52 int main(
53 int argc,
54 char **argv)
55 #else /* __PROTOTYPE__ */
56 int main(argc,argv)
57 int argc;
58 char **argv;
59 #endif /* __PROTOTYPE__ */
60 {
61 char buf[BUFSIZE];
62 Socket *srvr = NULL;
63 Socket *skt = NULL;
64
65 /* open server */
66 srvr= Sopen("sktsig","S");
67 if(!srvr) error(XTDIO_ERROR,"sktsig: cannot open a server <sktsig>\n");
68 printf("server <sktsig> now available\n");
69
70 /* wait for a client */
71 printf("waiting for a client\n");
72 skt= Saccept(srvr);
73 if(!skt) {
74 Sclose(srvr);
75 error(XTDIO_ERROR,"sktsig: Saccept failed\n");
76 }
77
78 /* set up SIGIO handler */
79 signal(SIGIO,sigio_handler); /* install sigio handler */
80 fcntl(skt->skt,F_SETOWN,getpid()); /* set ownership to self */
81 fcntl(skt->skt,F_SETFL,FASYNC); /* incoming data generates SIGIO */
82
83 Sputs("ready to receive",skt);
84 printf("ready to receive\n");
85
86 while(1) {
87 pause(); /* wait until a signal arrives */
88 if(!Sgets(buf,BUFSIZE,skt)) { /* socket error */
89 error(XTDIO_WARNING,"sktsig: socket error\n");
90 break;
91 }
92 printf("rcvd buf<%s>\n",sprt(buf));
93 Sprintf(skt,"rcvd buf <%s>",buf);
94 if(!strcmp(buf,"q")) break; /* quit on "q" */
95 }
96
97 /* close down Sockets */
98 Sclose(skt);
99 Sclose(srvr);
100
101 return 0;
102 }
103
104 /* --------------------------------------------------------------------- */
105
106 #if defined(__gnu_linux__)
107 void sigio_handler(int sig)
108 #elif defined(__PROTOYPE__)
109 void sigio_handler(
110 int sig,
111 int code,
112 struct sigcontext *sc)
113 #else /* __PROTOTYPE__ */
114 void sigio_handler(sig,code,sc)
115 int sig;
116 int code;
117 struct sigcontext *sc;
118 #endif /* __PROTOTYPE__ */
119 {
120 signal(SIGIO,sigio_handler); /* re-install sigio handler */
121
122 printf("caught a sigio\n");
123 }
124
125 /* --------------------------------------------------------------------- */