Inter-process communication: Socket

Download Report

Transcript Inter-process communication: Socket

Inter-process communication:
Socket
http://en.wikipedia.org/wiki/Internet_
socket
•
•
•
•
•
•
•
•
•
•
•
•
•
Internet socket
From Wikipedia, the free encyclopedia
Jump to: navigation, search
In computer networking, an Internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet
Protocol-based computer network, such as the Internet.
The term Internet sockets is also used as a name for an application programming interface (API) for the TCP/IP protocol stack, usually provided by the
operating system. Internet sockets constitute a mechanism for delivering incoming data packets to the appropriate application process or thread,
based on a combination of local and remote IP addresses and port numbers. Each socket is mapped by the operational system to a communicating
application process or thread.
A socket address is the combination of an IP address (the location of the computer) and a port (which is mapped to the application program process)
into a single identity, much like one end of a telephone connection is between a phone number and a particular extension line at that location.
An Internet socket is characterized by a unique combination of the following:
Protocol: A transport protocol (e.g., TCP, UDP), raw IP, or others. TCP port 53 and UDP port 53 are different, distinct sockets.
Local socket address: Local IP address and port number
Remote socket address: Only for established TCP sockets. As discussed in the Client-Server section below, this is necessary since a TCP server may
serve several clients concurrently. The server creates one socket for each client, and these sockets share the same local socket address.
Within the operating system and the application that created a socket, the socket is referred to by a unique integer number called socket identifier or
socket number. The operating system forwards the payload of incoming IP packets to the corresponding application by extracting the socket address
information from the IP and transport protocol headers and stripping the headers from the application data.
In IETF Request for Comments, Internet Standards, in many textbooks, as well as in this article, the term socket refers to an entity that is uniquely
identified by the socket number. In other textbooks[1], the socket term refers to a local socket address, i.e. a "combination of an IP address and a port
number". In the original definition of socket given in RFC 147, as it was related to the ARPA network in 1971, "the socket is specified as a 32 bit
number with even sockets identifying receiving sockets and odd sockets identifying sending sockets." Today, however, socket communications are
bidirectional.
On Unix-like and Microsoft Windows based operating systems the netstat command line tool may be used to list all currently established sockets and
related information.
http://www.troubleshooters.com/cod
ecorn/sockets/
•
Sockets are interfaces that can "plug into" each other over a network. Once so "plugged in", the
programs so connected communicate. This article discusses only simple aspects of stream inet
sockets (don't worry about exactly what that is right now). For the purposes of this article, a
"server" program is exposed via a socket connected to a certain /etc/services port number. A
"client" program can then connect its own socket to the server's socket, at which time the client
program's writes to the socket are read as stdin to the server program, and stdout from the server
program are read from the client's socket reads. This is one subset of socket programming, but it's
perhaps the easiest to master, so this is where you should start.
•
Diagram of client-server socket connection via xinetd.
Note that the client communicates by reading and writing the socket,
but the server program communicates via stdin and stdout. This tutorial requires a Linux box. It
hasn't been tested on other types of UNIX, but I think it might work. This tutorial is centered
around a system using xinetd, but it would be simple enough to adapt it to older inetd systems. This
tutorial will not work under Windows. I think it's important that this complex type of programming
be learned on the most reliable, straightforward system possible, so Windows is out.
For the purposes of this tutorial, the server application will be at port 3333. Note that you can
implement both the client and the server on a single computer, in which case the client is
connected to a port on the computer containing both the client and the server. So if you have only
one Linux box you can still do this tutorial.
•
Client-Server Application
Host A (client)
Host B (server)
socket
bind
listen
accept (blocks)
socket t1
connect (blocks) t2
connect returns t3
write
read (blocks)
t5
t4 accept returns
read (blocks)
t6
read returns
write
read (blocks)
read returns