mirror of
https://sourceware.org/git/glibc.git
synced 2025-10-27 12:15:39 +03:00
Previously this file failed to compile with the following errors:
$ gcc manual/examples/filesrv.c
manual/examples/filesrv.c: In function ‘main’:
manual/examples/filesrv.c:37:3: error: implicit declaration of function ‘unlink’ [-Wimplicit-function-declaration]
37 | unlink (SERVER);
| ^~~~~~
manual/examples/filesrv.c:40:10: error: implicit declaration of function ‘make_named_socket’ [-Wimplicit-function-declaration]
40 | sock = make_named_socket (SERVER);
| ^~~~~~~~~~~~~~~~~
manual/examples/filesrv.c:46:54: error: passing argument 6 of ‘recvfrom’ from incompatible pointer type [-Wincompatible-pointer-types]
46 | (struct sockaddr *) & name, &size);
| ^~~~~
| |
| size_t * {aka long unsigned int *}
In file included from manual/examples/filesrv.c:21:
/usr/include/sys/socket.h:165:48: note: expected ‘socklen_t * restrict’ {aka ‘unsigned int * restrict’} but argument is of type ‘size_t *’ {aka ‘long unsigned int *’}
165 | socklen_t *__restrict __addr_len);
| ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
This patch fixes the missing declaration for unlink and uses
'socklen_t *' for the fourth argument of recv from. The
make_named_socket function is defined in the manual.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
67 lines
1.7 KiB
C
67 lines
1.7 KiB
C
/* Datagram Socket Example
|
|
Copyright (C) 1991-2025 Free Software Foundation, Inc.
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU General Public License
|
|
as published by the Free Software Foundation; either version 2
|
|
of the License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <errno.h>
|
|
#include <stdlib.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/un.h>
|
|
#include <unistd.h>
|
|
|
|
#define SERVER "/tmp/serversocket"
|
|
#define MAXMSG 512
|
|
|
|
int
|
|
main (void)
|
|
{
|
|
int sock;
|
|
char message[MAXMSG];
|
|
struct sockaddr_un name;
|
|
socklen_t size;
|
|
int nbytes;
|
|
|
|
/* Remove the filename first, it's ok if the call fails */
|
|
unlink (SERVER);
|
|
|
|
/* Make the socket, then loop endlessly. */
|
|
sock = make_named_socket (SERVER);
|
|
while (1)
|
|
{
|
|
/* Wait for a datagram. */
|
|
size = sizeof (name);
|
|
nbytes = recvfrom (sock, message, MAXMSG, 0,
|
|
(struct sockaddr *) & name, &size);
|
|
if (nbytes < 0)
|
|
{
|
|
perror ("recfrom (server)");
|
|
exit (EXIT_FAILURE);
|
|
}
|
|
|
|
/* Give a diagnostic message. */
|
|
fprintf (stderr, "Server: got message: %s\n", message);
|
|
|
|
/* Bounce the message back to the sender. */
|
|
nbytes = sendto (sock, message, nbytes, 0,
|
|
(struct sockaddr *) & name, size);
|
|
if (nbytes < 0)
|
|
{
|
|
perror ("sendto (server)");
|
|
exit (EXIT_FAILURE);
|
|
}
|
|
}
|
|
}
|