root/trunk/soc1.c

Revision 389, 2.6 kB (checked in by heitor.barbieri, 3 weeks ago)

essage first commit

Line 
1
2     #include <stdio.h>
3     #include <errno.h>
4     #include <stdlib.h>
5     #include <unistd.h>
6     #include <sys/types.h>
7     #include <sys/socket.h>
8     #include <netinet/in.h>
9     #include <netdb.h>
10
11//     #define PORT            5555
12     #define MESSAGE         "Yow!!! Are we having fun yet?!?"
13//   #define SERVERHOST      "mescaline.gnu.org"
14
15     #define PORT            80
16     #define SERVERHOST      "www.bireme.br"
17
18
19     void
20     init_sockaddr (struct sockaddr_in *name,
21                    const char *hostname,
22                    uint16_t port)
23     {
24       struct hostent *hostinfo;
25
26       name->sin_family = AF_INET;
27       name->sin_port = htons (port);
28       hostinfo = gethostbyname (hostname);
29       if (hostinfo == NULL)
30         {
31           fprintf (stderr, "Unknown host %s.\n", hostname);
32           exit (EXIT_FAILURE);
33         }
34       name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
35     }
36
37//     void
38//     write_to_server (int filedes)
39//     {
40//       int nbytes;
41//
42//       nbytes = write (filedes, MESSAGE, strlen (MESSAGE) + 1);
43//       if (nbytes < 0)
44//         {
45//           perror ("write");
46//           exit (EXIT_FAILURE);
47//         }
48//     }
49
50     void
51     read_from_server (int filedes)
52     {
53       int nbytes=1;
54       char buffer[BUFSIZ+1];
55       int size=BUFSIZ;
56       int flags=0;
57
58       while (nbytes) {
59
60         nbytes = recv (filedes, buffer, size, flags);
61
62         if (nbytes < 0)
63           {
64             perror ("read");
65             exit (EXIT_FAILURE);
66           }
67         else
68           {
69             buffer[nbytes]='\0';
70             fprintf(stdout,"%s",buffer);
71           }
72       }
73
74     }
75
76
77     int
78     main (void)
79     {
80       extern void init_sockaddr (struct sockaddr_in *name,
81                                  const char *hostname,
82                                  uint16_t port);
83       int sock;
84       struct sockaddr_in servername;
85
86       /* Create the socket. */
87       sock = socket (PF_INET, SOCK_STREAM, 0);
88       if (sock < 0)
89         {
90           perror ("socket (client)");
91           exit (EXIT_FAILURE);
92         }
93
94       /* Connect to the server. */
95       init_sockaddr (&servername, SERVERHOST, PORT);
96       if (0 > connect (sock,
97                        (struct sockaddr *) &servername,
98                        sizeof (servername)))
99         {
100           perror ("connect (client)");
101           exit (EXIT_FAILURE);
102         }
103
104       /* Send data to the server. */
105//       write_to_server (sock);
106       read_from_server (sock);
107
108       close (sock);
109       exit (EXIT_SUCCESS);
110     }
Note: See TracBrowser for help on using the browser.