File: socktest.lsp

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (48 lines) | stat: -rw-r--r-- 1,716 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
(use-package "SOCKETS")

(defun server (port &key (fork t))
  (with-server-socket-loop (sock port :fork fork)
    (format *error-output* "Connection has been made to ~a~%" "????")
    (loop
     (let ((byte (socket-read-byte sock nil nil)))
         (if byte
             (write-char (int-char byte))
           (return))))
      (format *error-output* "socket closed by client~%")))

(defun client (host port)
  (with-client-socket (sock port host)
    (format *error-output* "Connection has been made to ~a:~d~%" host port)
    (let ((nl (string #\newline)))
      (loop
       (let ((line (read-line *standard-input* nil nil)))
         (unless line (return))
         (socket-write-string line sock)
         (socket-write-string nl sock)
         (socket-force-output sock))))))

(defun finger (user &optional (host "localhost") (port 79))
  (with-client-socket (sock port host)
    (socket-write-line user sock)
    (socket-force-output sock)
    (with-output-to-string (s)
      (loop
       (multiple-value-bind (line nlmissing)
                            (socket-read-line sock nil nil)
          (unless line (return))
          (write-string line s)
          (unless nlmissing (terpri s)))))))

(defun echo (&optional (host "localhost") (port 7))
  (with-client-socket (sock port host)
    (format *error-output* "Connection has been made to ~a:~d~%" host port)
    (loop
     (let ((line (read-line *standard-input* nil nil)))
       (unless line (return))
       (socket-write-line line sock)
       (socket-force-output sock)
       (write-line (socket-read-line sock))))))

(defun daytime (&optional (host "localhost") (port 13))
  (with-client-socket (sock port host)
    (socket-read-line sock)))