1 /* Byte Stream Socket Example
2 Copyright (C) 1991-2023 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>.
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <netinet/in.h>
28 #define MESSAGE "Yow!!! Are we having fun yet?!?"
29 #define SERVERHOST "www.gnu.org"
32 write_to_server (int filedes
)
36 nbytes
= write (filedes
, MESSAGE
, strlen (MESSAGE
) + 1);
48 extern void init_sockaddr (struct sockaddr_in
*name
,
52 struct sockaddr_in servername
;
54 /* Create the socket. */
55 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
58 perror ("socket (client)");
62 /* Connect to the server. */
63 init_sockaddr (&servername
, SERVERHOST
, PORT
);
64 if (0 > connect (sock
,
65 (struct sockaddr
*) &servername
,
68 perror ("connect (client)");
72 /* Send data to the server. */
73 write_to_server (sock
);