Source string Read only

_
(itstool) path: imageobject/imagedata This is a reference to an external file such as an image or video. When the file changes, the md5 hash will change to let you know you need to update your localized copy. The msgstr is not used at all. Set it to whatever you like once you have updated your copy of the file.
48/480
Context English State
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);
Note that this time <varname>addrlen</varname> is a pointer. This is necessary because in this case it is the socket that fills out <varname>addr</varname>, the <varname>sockaddr_in</varname> structure.
The return value is an integer. Indeed, the <function>accept</function> returns a <emphasis>new socket</emphasis>. You will use this new socket to communicate with the client.
What happens to the old socket? It continues to listen for more requests (remember the <varname>backlog</varname> variable we passed to <function>listen</function>?) until we <function>close</function> it.
Now, the new socket is meant only for communications. It is fully connected. We cannot pass it to <function>listen</function> again, trying to accept additional connections.

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

Context
_
Source string comment
(itstool) path: imageobject/imagedata This is a reference to an external file such as an image or video. When the file changes, the md5 hash will change to let you know you need to update your localized copy. The msgstr is not used at all. Set it to whatever you like once you have updated your copy of the file.
Flags
read-only
Source string location
book.translate.xml:5907
String age
a year ago
Source string age
a year ago
Translation file
books/developers-handbook.pot, string 973