root/trunk/client30.c

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

essage first commit

Line 
1/*
2
316.9.6 Byte Stream Socket Example
4Here is an example client program that makes a connection for a byte stream socket in the Internet namespace. It doesn't do anything particularly interesting once it has connected to the server; it just sends a text string to the server and exits.
5
6This program uses init_sockaddr to set up the socket address; see Inet Example.
7*/
8
9     #include <stdio.h>
10     #include <errno.h>
11     #include <stdlib.h>
12     #include <unistd.h>
13     #include <sys/types.h>
14     #include <sys/socket.h>
15     #include <netinet/in.h>
16     #include <netdb.h>
17
18     #define PORT            80
19     #define MESSAGE         "HEAD /"
20     #define SERVERHOST      "www.bireme.br"
21
22     #define MAXMSG  512
23
24     int
25     read_from_server (int filedes)
26     {
27       char buffer[MAXMSG];
28       int nbytes;
29
30       nbytes = read (filedes, buffer, MAXMSG-1);
31       if (nbytes < 0)
32         {
33           /* Read error. */
34           perror ("read");
35           exit (EXIT_FAILURE);
36         }
37       else if (nbytes == 0)
38         /* End-of-file. */
39         return -1;
40       else
41         {
42           /* Data read. */
43           buffer[nbytes]='\0';
44           fprintf (stderr, "Client: got message: `%s'\n", buffer);
45           return 0;
46         }
47     }
48
49     void
50     write_to_server (int filedes, char *message)
51     {
52       int nbytes;
53
54       nbytes = write (filedes, message, strlen (message) + 0);
55       if (nbytes < 0)
56         {
57           perror ("write");
58           exit (EXIT_FAILURE);
59         }
60       nbytes = write (filedes, " HTTP/1.0\n\n", strlen (" HTTP/1.0\n\n") + 1);
61       if (nbytes < 0)
62         {
63           perror ("write");
64           exit (EXIT_FAILURE);
65         }
66     }
67
68
69     int
70     main(int argc, char *argv[])
71     {
72       extern void init_sockaddr (struct sockaddr_in *name,
73                                  const char *hostname,
74                                  uint16_t port);
75       int sock;
76       struct sockaddr_in servername;
77
78       /* calling parameters */
79       char *message=MESSAGE;
80       char *serverhost=SERVERHOST;
81       uint16_t port=PORT;
82       if (argc == 2) if (strcmp(argv[1],"?") == 0) {
83           perror ("client3 \"HEAD|GET docpath\" [serverhost [port] ]");
84           exit (EXIT_FAILURE);
85       }
86       if (argc > 1) message=argv[1];
87       if (argc > 2) serverhost=argv[2];
88       if (argc > 3) port=(uint16_t)atoi(argv[3]);
89
90       /* Create the socket. */
91       sock = socket (PF_INET, SOCK_STREAM, 0);
92       if (sock < 0)
93         {
94           perror ("socket (client)");
95           exit (EXIT_FAILURE);
96         }
97
98       /* Connect to the server. */
99       init_sockaddr (&servername, serverhost, port);
100       if (0 > connect (sock,
101                        (struct sockaddr *) &servername,
102                        sizeof (servername)))
103         {
104           perror ("connect (client)");
105           exit (EXIT_FAILURE);
106         }
107
108       /* Send data to the server. */
109       write_to_server (sock, message
110#if 0
111       "HEAD /'message' HTTP/1.0\n\n"
112       "GET / HTTP/1.0\nUser-Agent: Mozilla/3.0 (compatible; Opera/3.0; Windows 95/NT4)\nAccept: */*\nHost: birk105.studby.uio.no:81\n\n"
113#endif
114       );
115
116       /* and Read...
117       */
118       read_from_server (sock);
119
120       close (sock);
121       exit (EXIT_SUCCESS);
122     }
123
124
125
126
127/*
128*/
129
130#if 1
131
132     #include <stdio.h>
133     #include <stdlib.h>
134     #include <sys/socket.h>
135     #include <netinet/in.h>
136     #include <netdb.h>
137
138     void
139     init_sockaddr (struct sockaddr_in *name,
140                    const char *hostname,
141                    uint16_t port)
142     {
143       struct hostent *hostinfo;
144
145       name->sin_family = AF_INET;
146       name->sin_port = htons (port);
147       hostinfo = gethostbyname (hostname);
148       if (hostinfo == NULL)
149         {
150           fprintf (stderr, "Unknown host %s.\n", hostname);
151           exit (EXIT_FAILURE);
152         }
153       name->sin_addr = *(struct in_addr *) hostinfo->h_addr;
154     }
155
156#endif
Note: See TracBrowser for help on using the browser.