Source string Read only

(itstool) path: sect4/programlisting
64/640
Context English State
The <varname>s</varname> argument is the socket, i.e., the value returned by the <function>socket</function> function. The <varname>name</varname> is a pointer to <varname>sockaddr</varname>, the structure we have talked about extensively. Finally, <varname>namelen</varname> informs the system how many bytes are in our <varname>sockaddr</varname> structure.
If <function>connect</function> is successful, it returns <constant>0</constant>. Otherwise it returns <constant>-1</constant> and stores the error code in <varname>errno</varname>.
There are many reasons why <function>connect</function> may fail. For example, with an attempt to an Internet connection, the <acronym>IP</acronym> address may not exist, or it may be down, or just too busy, or it may not have a server listening at the specified port. Or it may outright <emphasis>refuse</emphasis> any request for specific code.
Our First Client
We now know enough to write a very simple client, one that will get current time from <systemitem class="ipaddress">192.43.244.18</systemitem> and print it to <filename>stdout</filename>.
/*
* daytime.c
*
* Programmed by G. Adam Stanislav
*/
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;netinet/in.h&gt;

int main() {
register int s;
register int bytes;
struct sockaddr_in sa;
char buffer[BUFSIZ+1];

if ((s = socket(PF_INET, SOCK_STREAM, 0)) &lt; 0) {
perror("socket");
return 1;
}

bzero(&amp;sa, sizeof sa);

sa.sin_family = AF_INET;
sa.sin_port = htons(13);
sa.sin_addr.s_addr = htonl((((((192 &lt;&lt; 8) | 43) &lt;&lt; 8) | 244) &lt;&lt; 8) | 18);
if (connect(s, (struct sockaddr *)&amp;sa, sizeof sa) &lt; 0) {
perror("connect");
close(s);
return 2;
}

while ((bytes = read(s, buffer, BUFSIZ)) &gt; 0)
write(1, buffer, bytes);

close(s);
return 0;
}
Go ahead, enter it in your editor, save it as <filename>daytime.c</filename>, then compile and run it:
<prompt>%</prompt> <userinput>cc -O3 -o daytime daytime.c</userinput>
<prompt>%</prompt> <userinput>./daytime</userinput>

52079 01-06-19 02:29:25 50 0 1 543.9 UTC(NIST) *
<prompt>%</prompt>
In this case, the date was June 19, 2001, the time was 02:29:25 <acronym>UTC</acronym>. Naturally, your results will vary.
Server Functions
The typical server does not initiate the connection. Instead, it waits for a client to call it and request services. It does not know when the client will call, nor how many clients will call. It may be just sitting there, waiting patiently, one moment, The next moment, it can find itself swamped with requests from a number of clients, all calling in at the same time.
The sockets interface offers three basic functions to handle this.
<function>bind</function>
Ports are like extensions to a phone line: After you dial a number, you dial the extension to get to a specific person or department.
There are 65535 <acronym>IP</acronym> ports, but a server usually processes requests that come in on only one of them. It is like telling the phone room operator that we are now at work and available to answer the phone at a specific extension. We use <citerefentry><refentrytitle>bind</refentrytitle><manvolnum>2</manvolnum></citerefentry> to tell sockets which port we want to serve.
int bind(int s, const struct sockaddr *addr, socklen_t addrlen);
Beside specifying the port in <varname>addr</varname>, the server may include its <acronym>IP</acronym> address. However, it can just use the symbolic constant <symbol>INADDR_ANY</symbol> to indicate it will serve all requests to the specified port regardless of what its <acronym>IP</acronym> address is. This symbol, along with several similar ones, is declared in <filename>netinet/in.h</filename>
#define INADDR_ANY (u_int32_t)0x00000000
Suppose we were writing a server for the <emphasis>daytime</emphasis> protocol over <acronym>TCP</acronym>/<acronym>IP</acronym>. Recall that it uses port 13. Our <varname>sockaddr_in</varname> structure would look like this:
_ external ref='sockets/sainserv' md5='__failed__'
0 1 2 3
+--------+--------+--------+--------+
0 | 0 | 2 | 0 | 13 |
+--------+--------+--------+--------+
4 | 0 |
+-----------------------------------+
8 | 0 |
+-----------------------------------+
12 | 0 |
+-----------------------------------+
<imageobject> <imagedata fileref="sockets/sainserv"/> </imageobject> <textobject> <_:literallayout-1/> </textobject> <textobject> <phrase>Example Server sockaddr_in</phrase> </textobject>
<function>listen</function>
To continue our office phone analogy, after you have told the phone central operator what extension you will be at, you now walk into your office, and make sure your own phone is plugged in and the ringer is turned on. Plus, you make sure your call waiting is activated, so you can hear the phone ring even while you are talking to someone.
The server ensures all of that with the <citerefentry><refentrytitle>listen</refentrytitle><manvolnum>2</manvolnum></citerefentry> function.
int listen(int s, int backlog);
In here, the <varname>backlog</varname> variable tells sockets how many incoming requests to accept while you are busy processing the last request. In other words, it determines the maximum size of the queue of pending connections.
<function>accept</function>
After you hear the phone ringing, you accept the call by answering the call. You have now established a connection with your client. This connection remains active until either you or your client hang up.
The server accepts the connection by using the <citerefentry><refentrytitle>accept</refentrytitle><manvolnum>2</manvolnum></citerefentry> function.
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);

Loading…

No matching activity found.

Browse all component changes

Things to check

Multiple failing checks

The translations in several languages have failing checks

Reset

Glossary

English English
No related strings found in the glossary.

Source information

Source string comment
(itstool) path: sect4/programlisting
Flags
no-wrap, read-only
Source string location
book.translate.xml:5927
String age
a year ago
Source string age
a year ago
Translation file
books/developers-handbook.pot, string 979