4 This file is part of socket(1).
5 Copyright (C) 1992 by Juergen Nickelsen <nickel@cs.tu-berlin.de>
6 Please read the file COPYRIGHT for further details.
10 #if defined(__linux__) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__sun)
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <netinet/in.h>
24 void init_signals(void);
26 int socket_poll( int asocket
)
33 FD_SET(asocket
, &read_fd
);
39 ret
= select( asocket
+1, &read_fd
, NULL
, NULL
, &wait
);
40 if (ret
<0 && errno
!= EINTR
) {
46 int socket_get( int asocket
)
49 if (socket_read( asocket
, buffer
, 1)!=-1) {
55 int socket_read( int asocket
, char *buffer
, int len
)
59 got
= read( asocket
, buffer
, len
);
66 int socket_write( int sink
, char *buffer
, int len
)
71 wrote
= write( sink
, buffer
, len
);
80 int socket_init(int port
)
82 struct sockaddr_in sock
;
85 ret
= socket(AF_INET
, SOCK_STREAM
, 0);
88 /* Clear the sockaddr_in */
89 memset((void *)&sock
, 0, sizeof(struct sockaddr_in
));
91 sock
.sin_family
= AF_INET
;
92 sock
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
93 sock
.sin_port
= htons(port
);
95 if (bind(ret
, (struct sockaddr
*)&sock
, sizeof(struct sockaddr_in
))<0) {
96 return -2; /* Bind error */
98 if (listen(ret
, 1)<0) {
99 return -3; /* Listen error */
104 int socket_main(int hsocket
)
106 struct sockaddr_in sock
;
107 int asocket
; /* Active socket */
112 sock_len
= sizeof(struct sockaddr_in
);
113 asocket
= accept( hsocket
, (struct sockaddr
*)&sock
, &sock_len
);
117 socket_write( asocket
, "Hi there\n", 9 );
119 while (socket_poll(asocket
)==0) {}
120 if ((got
=socket_read(asocket
, buffer
, 200))>0) {
121 socket_write( asocket
, buffer
, got
);
130 /* Signal handler, print message and exit */
134 if (sig
!= SIGUSR1
) {
135 fprintf(stderr
, "\nSignal %i occured, exiting\n", sig
) ;
140 /* set up signal handling. All except TSTP, CONT, CLD, and QUIT
141 * are caught with exitsig(). */
145 #ifdef BSD_SIG_SETMASK /* only with BSD signals */
146 static struct sigvec svec
= { exitsig
, ~0, 0 } ;
149 for (i
= 0; i
< NSIG
; i
++) {
159 #if !defined (SIGCHLD) && defined (SIGCLD)
160 #define SIGCHLD SIGCLD
167 case SIGWINCH
: /* it is ridiculous to exit on WINCH */
170 case SIGQUIT
: /* if the user wants a core dump, */
171 continue ; /* they can have it. */
173 #ifdef BSD_SIG_SETMASK
174 sigvec(i
, &svec
, NULL
) ;