122 lines
4.9 KiB
HTML
122 lines
4.9 KiB
HTML
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ -->
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
<!-- This manual documents Guile version 3.0.10.
|
||
|
|
||
|
Copyright (C) 1996-1997, 2000-2005, 2009-2023 Free Software Foundation,
|
||
|
Inc.
|
||
|
|
||
|
Copyright (C) 2021 Maxime Devos
|
||
|
|
||
|
Copyright (C) 2024 Tomas Volf
|
||
|
|
||
|
|
||
|
Permission is granted to copy, distribute and/or modify this document
|
||
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
||
|
any later version published by the Free Software Foundation; with no
|
||
|
Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A
|
||
|
copy of the license is included in the section entitled "GNU Free
|
||
|
Documentation License." -->
|
||
|
<title>Internet Socket Examples (Guile Reference Manual)</title>
|
||
|
|
||
|
<meta name="description" content="Internet Socket Examples (Guile Reference Manual)">
|
||
|
<meta name="keywords" content="Internet Socket Examples (Guile Reference Manual)">
|
||
|
<meta name="resource-type" content="document">
|
||
|
<meta name="distribution" content="global">
|
||
|
<meta name="Generator" content=".texi2any-real">
|
||
|
<meta name="viewport" content="width=device-width,initial-scale=1">
|
||
|
|
||
|
<link href="index.html" rel="start" title="Top">
|
||
|
<link href="Concept-Index.html" rel="index" title="Concept Index">
|
||
|
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
||
|
<link href="Networking.html" rel="up" title="Networking">
|
||
|
<link href="Network-Sockets-and-Communication.html" rel="prev" title="Network Sockets and Communication">
|
||
|
<style type="text/css">
|
||
|
<!--
|
||
|
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
|
||
|
div.example {margin-left: 3.2em}
|
||
|
span.r {font-family: initial; font-weight: normal; font-style: normal}
|
||
|
span:hover a.copiable-link {visibility: visible}
|
||
|
-->
|
||
|
</style>
|
||
|
<link rel="stylesheet" type="text/css" href="https://www.gnu.org/software/gnulib/manual.css">
|
||
|
|
||
|
|
||
|
</head>
|
||
|
|
||
|
<body lang="en">
|
||
|
<div class="subsubsection-level-extent" id="Internet-Socket-Examples">
|
||
|
<div class="nav-panel">
|
||
|
<p>
|
||
|
Previous: <a href="Network-Sockets-and-Communication.html" accesskey="p" rel="prev">Network Sockets and Communication</a>, Up: <a href="Networking.html" accesskey="u" rel="up">Networking</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
|
||
|
</div>
|
||
|
<hr>
|
||
|
<h4 class="subsubsection" id="Network-Socket-Examples"><span>7.2.11.5 Network Socket Examples<a class="copiable-link" href="#Network-Socket-Examples"> ¶</a></span></h4>
|
||
|
<a class="index-entry-id" id="index-network-examples"></a>
|
||
|
<a class="index-entry-id" id="index-socket-examples"></a>
|
||
|
|
||
|
<p>The following give examples of how to use network sockets.
|
||
|
</p>
|
||
|
<h4 class="subsubheading" id="Internet-Socket-Client-Example"><span>Internet Socket Client Example<a class="copiable-link" href="#Internet-Socket-Client-Example"> ¶</a></span></h4>
|
||
|
|
||
|
<a class="index-entry-id" id="index-socket-client-example"></a>
|
||
|
<p>The following example demonstrates an Internet socket client.
|
||
|
It connects to the HTTP daemon running on the local machine and
|
||
|
returns the contents of the root index URL.
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(let ((s (socket PF_INET SOCK_STREAM 0)))
|
||
|
(connect s AF_INET (inet-pton AF_INET "127.0.0.1") 80)
|
||
|
(display "GET / HTTP/1.0\r\n\r\n" s)
|
||
|
|
||
|
(do ((line (read-line s) (read-line s)))
|
||
|
((eof-object? line))
|
||
|
(display line)
|
||
|
(newline)))
|
||
|
</pre></div>
|
||
|
|
||
|
|
||
|
<h4 class="subsubheading" id="Internet-Socket-Server-Example"><span>Internet Socket Server Example<a class="copiable-link" href="#Internet-Socket-Server-Example"> ¶</a></span></h4>
|
||
|
|
||
|
<a class="index-entry-id" id="index-socket-server-example"></a>
|
||
|
<p>The following example shows a simple Internet server which listens on
|
||
|
port 2904 for incoming connections and sends a greeting back to the
|
||
|
client.
|
||
|
</p>
|
||
|
<div class="example">
|
||
|
<pre class="example-preformatted">(let ((s (socket PF_INET SOCK_STREAM 0)))
|
||
|
(setsockopt s SOL_SOCKET SO_REUSEADDR 1)
|
||
|
;; <span class="r">Specific address?</span>
|
||
|
;; <span class="r">(bind s AF_INET (inet-pton AF_INET "127.0.0.1") 2904)</span>
|
||
|
(bind s AF_INET INADDR_ANY 2904)
|
||
|
(listen s 5)
|
||
|
|
||
|
(simple-format #t "Listening for clients in pid: ~S" (getpid))
|
||
|
(newline)
|
||
|
|
||
|
(while #t
|
||
|
(let* ((client-connection (accept s))
|
||
|
(client-details (cdr client-connection))
|
||
|
(client (car client-connection)))
|
||
|
(simple-format #t "Got new client connection: ~S"
|
||
|
client-details)
|
||
|
(newline)
|
||
|
(simple-format #t "Client address: ~S"
|
||
|
(gethostbyaddr
|
||
|
(sockaddr:addr client-details)))
|
||
|
(newline)
|
||
|
;; <span class="r">Send back the greeting to the client port</span>
|
||
|
(display "Hello client\r\n" client)
|
||
|
(close client))))
|
||
|
</pre></div>
|
||
|
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
</body>
|
||
|
</html>
|