socket
1. socket.2.man
Manpage of SOCKET
SOCKET
Section: Systemaufrufe (2)Updated: 2. Mai 2000
Index Return to Main Contents
BEZEICHNUNG
socket - erzeuge einen KommunikationsendpunktSYNTAX
#include <sys/types.h>#include <sys/socket.h>
int socket(int domain, int type, int protocol);
BESCHREIBUNG
Socket erzeugt einen Kommunikationsendpunkt und gibt den zugehörigen Deskriptor zurück.Der Parameter domain spezifiziert die Kommunikationsdomain, in der die Kommunikation stattfinden soll, also die Protokoll-Familie, die benutzt werden soll. Diese Familien sind in der Include-Datei <sys/socket.h> definiert.
Zurzeit werden folgende Domains unterstützt:
| Name | Zweck | Handbuchseite |
| PF_UNIX,PF_LOCAL | Lokale Kommunikation |
unix(7)
|
| PF_INET | IPv4 Internet-Protokoll |
ip(7)
|
| PF_INET6 | IPv6 Internet-Protokoll | |
| PF_IPX | IPX - Novell-Protokoll | |
| PF_NETLINK | Kernel User Interface Device |
netlink(7)
|
| PF_X25 | ITU-T X.25 / ISO-8208-Protokoll |
x25(7)
|
| PF_AX25 | Amateur-Radio AX.25-Protokoll | |
| PF_ATMPVC | Zugriff auf raw ATM PVCs | |
| PF_APPLETALK | Appletalk |
ddp(7)
|
| PF_PACKET | Low-Level Paket-Interface |
packet(7)
|
Der Socket hat den in type angegebenen Typ, der die Art der Kommunikation bestimmt. Zurzeit sind folgende Arten definiert:
- SOCK_STREAM
- stellt einen sequenziellen, verlässlichen, zwei-wege, verbindungsbasierten Byte-Stream zur Verfügung. Ein lqout-of-bandrq Übertragungsmechanismus kann unterstützt werden.
- SOCK_DGRAM
- bietet Datagramme (verbindungslos, unverlässliche Nachricht einer festen (meist kleinen) maximalen Länge).
- SOCK_SEQPACKET
- bietet einen sequenziellen, verlässlichen, zwei-weg-basierten Übertragungspfad für Datagramme einer festen maximalen Länge. Der Empfänger muss ein ganzes Paket mit jedem Funktionsaufruf lesen.
- SOCK_RAW
- stellt Zugriff auf interne Netzwerkprotokolle und Schnittstellen zur Verfügung.
- SOCK_RDM
- bietet eine verlässliche Datagramm-Schicht, die kein Sortieren garantiert.
- SOCK_PACKET
- ist veraltet und sollte nicht in neuen Programmen benutzt werden. Siehe packet(7).
protocol bezeichnet ein spezielles Protokoll, das auf diesem Socket benutzt wird. Normalerweise gibt es nur ein einziges Protokoll, das von einem speziellen Socket einer Protokollfamilie unterstützt wird. Nichtsdestotrotz ist es möglich, dass mehrere Protokolle existieren. In diesem Fall muss das zu verwendende auf diese Art angegeben werden. Die Protokollnummer ist individuell für eine bestimmte "Kommunikationsdomain". Siehe dazu auch protocols(5).
In getprotoent(3), ist beschrieben, wie Protokoll-Namen in Protokoll-Nummern umgewandelt werden können.
Sockets des Typs SOCK_STREAM sind voll-duplex-orientierte Byte-Streams, ähnlich wie Pipes. Sie erhalten die Record-Grenzen nicht. Ein Stream-Socket muss sich in einem connected-Modus befinden, bevor mit ihm irgendwelche Daten gesendet oder empfangen werden können. Eine Verbindung zu einem anderen Socket wird mit connect(2) hergestellt. Einmal verbunden können Daten mit read(2) und write(2) übertragen werden bzw. mit Varianten von send(2) oder recv(2). Wenn eine Verbindung abgebaut werden soll, wird close(2) ausgeführt. Out-of-band Daten können, wie in send(2) beschrieben, gesendet und, wie in recv(2) beschrieben, empfangen werden.
Die Kommunikationsprotokolle, die verwendet werden, um ein SOCK_STREAM zu implementieren, stellen sicher, dass Daten weder verloren gehen noch verdoppelt werden. Wenn ein Datum, für das das Partnerprotokoll ausreichend Puffer zur Verfügung hat, in einem angemessenen Zeitraum nicht erfolgreich übertragen werden kann, wird angenommen, dass die Verbindung unterbrochen (qlbrokenqr) ist. Wenn SO_KEEPALIVE für den Socket gesetzt ist, überprüft das Protokoll auf eine protokoll-spezifische Art, ob das andere Ende noch immer existiert. Ein SIGPIPE-Signal wird erzeugt, wenn ein Prozess versucht, auf einem kaputten Stream zu senden oder zu empfangen. Dies beendet Prozesse, die das Signal nicht verarbeiten.
SOCK_SEQPACKET-Sockets benutzen die selben Systemcalls wie SOCK_STREAM-Sockets. Der einzige Unterschied besteht darin, dass read(2) nur die angeforderte Menge an Daten zurückliefert und alle restlichen verwirft. Außerdem werden all Nachrichten-Grenzen der eingehenden Datagramme beibehalten.
SOCK_DGRAM- und SOCK_RAW-Sockets erlauben das Senden von Datagrammen zu Empfängern, die im send(2) Aufruf genannt werden. Datagramme werden grundsätzlich mit recvfrom(2) empfangen, das das nächste Datagramm zusammen mit der Absenderadresse zurückliefert.
SOCK_PACKET ist ein veralteter Socket-Typ, um rohe Pakete direkt vom Geräte-Treiber zu empfangen. Benutzen Sie stattdessen packet(7).
Ein fcntl(2)-Aufruf kann mit dem F_SETOWN-Argument benutzt werden, um eine Prozessgruppe anzugeben, die ein SIGURG-Signal empfangen soll, wenn out-of-band Daten ankommen, oder ein SIGPIPE-Signal, wenn eine SOCK_STREAM-Verbindung unerwartet zusammenbricht. Damit kann ebenfalls der Prozess oder die Prozessgruppe eingestellt werden, welche I/O und asynchrone Benachrichtigung von I/O-Ereignissen mit dem SIGIO-Signal erhält. F_SETOWN zu benutzen entspricht dem Aufruf der Systemfunktion ioctl(2) mit dem Argument SIOSETOWN
Wenn das Netzwerk dem Protokoll-Modul einen Fehler meldet (z. B. durch eine ICMP-Nachricht unter IP), wird der Flag gesetzt, der auf einen unbearbeiteten Fehler (lqpending errorrq) hinweist. Der nächste Funktionsaufruf für diesen Socket liefert den Code des Fehler zurück. Bei manchen Protokollen ist es möglich, eine socket-spezifische Fehlerliste einzuschalten, um genaue Informationen über den Fehler zu erhalten. Siehe IP_RECEIVER in ip(7).
Die Arbeitsweise von Sockets wird von Socket-Level-Optionen gesteuert. Diese sind in der Include-Datei <sys/socket.h> definiert. setsockopt(2) und getsockopt(2) werden verwendet, um diese Optionen zu setzen bzw. zu lesen.
BEMERKUNG
Die unter BSD 4.* benutzten Konstanten für Protokoll-Familien sind PF_UNIX, PF_INET usw., während AF_UNIX usw. für Adress-Familien verwandt werden. Allerdings verspricht die BSD Handbuchseite bereits: lqDie Protokoll-Familie ist generell das selbe wie die Adress-Familierq und die folgenden Standards benutzen überall AF_*.RÜCKGABEWERTE
-1 wird zurückgegeben, wenn ein Fehler auftritt, ansonsten wird die Nummer des Deskriptors zurückgegeben, der den Socket referenziert.FEHLER
- EPROTONOSUPPORT
- Der Protokolltyp, der in protocol angegeben ist, wird nicht von dieser Kommunikationsdomain unterstützt.
- ENFILE
- Es ist nicht genügend Kernel-Speicher vorhanden, um eine neue Socket-Struktur anzulegen.
- EMFILE
- Die Datei-Deskriptor-Tabelle des Prozesses ist voll.
- EACCESS
- Es ist dem Prozess nicht erlaubt, einen Socket von angegebenen Tyo und/oder Protokoll zu erzeugen.
- ENOBUFS oder ENOMEM
- Es ist nicht ausreichend Speicher verfügbar. Der Socket kann nicht erzeugt werden bis ausreichend Ressourcen freigemacht wurden.
- EINVAL
-
Unbekanntes Protokoll oder Protokoll-Familie nicht verfügbar.
KONFORM ZU
4.4BSD (die socket-Funktion taucht in BSD 4.2 auf). Generell portable auf/von nicht-BSD-Systemen, die den BSD-Socket-Layer unterstützen (inklusive System-V-Varianten).BUGS
SOCK_UUCP ist noch nicht implementiert.SIEHE AUCH
accept(2), bind(2), connect(2), getprotoent(3), getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2), select(2), send(2), shutdown(2), socketpair(2), write(2)lqAn Introductory 4.3 BSD Interprocess Communication Tutorialrq ist in UNIX Programmer's Supplementary Documents Volume 1 abgedruckt.
lqBSD Interprocess Communication Tutoriallq ist in UNIX Programmer's Supplementary Documents Volume 1 abgedruckt.
Index
This document was created by man2html using the manual pages.
Time: 00:23:38 GMT, November 20, 2008
2. socket.9.man
Manpage of socket
socket
Section: Tcl Built-In Commands (n)Updated: 8.0
Index Return to Main Contents
NAME
socket - Open a TCP network connectionSYNOPSIS
socket ?options? host port
socket -server command ?options? port
DESCRIPTION
This command opens a network socket and returns a channel identifier that may be used in future invocations of commands like read, puts and flush. At present only the TCP network protocol is supported; future releases may include support for additional protocols. The socket command may be used to open either the client or server side of a connection, depending on whether the -server switch is specified.
Note that the default encoding for all sockets is the system encoding, as returned by encoding system. Most of the time, you will need to use fconfigure to alter this to something else, such as utf-8 (ideal for communicating with other Tcl processes) or iso8859-1 (useful for many network protocols, especially the older ones).
CLIENT SOCKETS
If the -server option is not specified, then the client side of a connection is opened and the command returns a channel identifier that can be used for both reading and writing. Port and host specify a port to connect to; there must be a server accepting connections on this port. Port is an integer port number (or service name, where supported and understood by the host operating system) and host is either a domain-style name such as www.tcl.tk or a numerical IP address such as 127.0.0.1. Use localhost to refer to the host on which the command is invoked.
The following options may also be present before host to specify additional information about the connection:
- -myaddr addr
- Addr gives the domain-style name or numerical IP address of the client-side network interface to use for the connection. This option may be useful if the client machine has multiple network interfaces. If the option is omitted then the client-side interface will be chosen by the system software.
- -myport port
- Port specifies an integer port number (or service name, where supported and understood by the host operating system) to use for the client's side of the connection. If this option is omitted, the client's port number will be chosen at random by the system software.
- -async
- The -async option will cause the client socket to be connected asynchronously. This means that the socket will be created immediately but may not yet be connected to the server, when the call to socket returns. When a gets or flush is done on the socket before the connection attempt succeeds or fails, if the socket is in blocking mode, the operation will wait until the connection is completed or fails. If the socket is in nonblocking mode and a gets or flush is done on the socket before the connection attempt succeeds or fails, the operation returns immediately and fblocked on the socket returns 1.
SERVER SOCKETS
If the -server option is specified then the new socket will be a server for the port given by port (either an integer or a service name, where supported and understood by the host operating system; if port is zero, the operating system will allocate a free port to the server socket which may be discovered by using fconfigure to read the -sockname option). Tcl will automatically accept connections to the given port. For each connection Tcl will create a new channel that may be used to communicate with the client. Tcl then invokes command with three additional arguments: the name of the new channel, the address, in network address notation, of the client's host, and the client's port number.
The following additional option may also be specified before host:
- -myaddr addr
- Addr gives the domain-style name or numerical IP address of the server-side network interface to use for the connection. This option may be useful if the server machine has multiple network interfaces. If the option is omitted then the server socket is bound to the special address INADDR_ANY so that it can accept connections from any interface.
Server channels cannot be used for input or output; their sole use is to accept new client connections. The channels created for each incoming client connection are opened for input and output. Closing the server channel shuts down the server so that no new connections will be accepted; however, existing connections will be unaffected.
Server sockets depend on the Tcl event mechanism to find out when new connections are opened. If the application doesn't enter the event loop, for example by invoking the vwait command or calling the C procedure Tcl_DoOneEvent, then no connections will be accepted.
If port is specified as zero, the operating system will allocate an unused port for use as a server socket. The port number actually allocated may be retrieved from the created server socket using the fconfigure command to retrieve the -sockname option as described below.
CONFIGURATION OPTIONS
The fconfigure command can be used to query several readonly configuration options for socket channels:- -error
- This option gets the current error status of the given socket. This is useful when you need to determine if an asynchronous connect operation succeeded. If there was an error, the error message is returned. If there was no error, an empty string is returned.
- -sockname
- This option returns a list of three elements, the address, the host name and the port number for the socket. If the host name cannot be computed, the second element is identical to the address, the first element of the list.
- -peername
- This option is not supported by server sockets. For client and accepted sockets, this option returns a list of three elements; these are the address, the host name and the port to which the peer socket is connected or bound. If the host name cannot be computed, the second element of the list is identical to the address, its first element.
EXAMPLES
Here is a very simple time server:-
proc Server {channel clientaddr clientport} { puts "Connection from $clientaddr registered" puts $channel [clock format [clock seconds]] close $channel } socket -server Server 9900 vwait forever
And here is the corresponding client to talk to the server:
-
set server localhost set sockChan [socket $server 9900] gets $sockChan line close $sockChan puts "The time on $server is $line"
SEE ALSO
fconfigure(n), flush(n), open(n), read(n)KEYWORDS
bind, channel, connection, domain name, host, network address, socket, tcp
Index
- NAME
- SYNOPSIS
- DESCRIPTION
- CLIENT SOCKETS
- SERVER SOCKETS
- CONFIGURATION OPTIONS
- EXAMPLES
- SEE ALSO
- KEYWORDS
This document was created by man2html using the manual pages.
Time: 00:23:38 GMT, November 20, 2008




