Download Note

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Computer network wikipedia , lookup

AppleTalk wikipedia , lookup

Low Pin Count wikipedia , lookup

Wake-on-LAN wikipedia , lookup

Airborne Networking wikipedia , lookup

Distributed firewall wikipedia , lookup

Net neutrality law wikipedia , lookup

Network tap wikipedia , lookup

Net bias wikipedia , lookup

Parallel port wikipedia , lookup

Recursive InterNetwork Architecture (RINA) wikipedia , lookup

Piggybacking (Internet access) wikipedia , lookup

List of wireless community networks by region wikipedia , lookup

I²C wikipedia , lookup

Cracking of wireless networks wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Transcript
System calls for sockets IPC
1. socket()
; obtain socket descriptor
int
int
int
sd = socket (<domain>, <type>, <protocol> ) ;
Socket
descriptor
AF_UNIX
AF_INET
SOCK_STREAM
SOCK_DGRAM
Typically 0
(system selects)
See <sys/socket.h>
2. bind()
; assign an address to a socket
bind ( sd , <pointer> , <len> ) ;
Pointer to a
structure of type
sockaddr_un or
sockaddr_in
3. listen()
The size of the
structure
; a process (server) is ready to receive
data at this socket (establish a listen
queue)
listen ( sd , <integer> ) ;
The number of requests
from clients waiting at
this socket
1
Relationship between
System calls socket() and bind()
a) before
Process
socket()
Host
Network
IP address
Process
Host
b) after
Data structure (no IP
address, no port)
socket
Network
IP address
Process
Host
socket()
c) after
bind()
socket
port
Network
IP address
2
4. accept()
; accept a connection at a socket
(used by a server)
ns = accept ( sd , <pointer> , <pointer> ) ;
A pointer to a
Connection
structure of type
descriptor
sockaddr_un or
For new
sockaddr_in
socket!!!
sd
A pointer to an integer
(the size of the structure)
Old
socket
New
socket
ns
port
IP address
5. connect()
;establish a connection (with server)
connect ( sd , <pointer> , <size> ) ;
A pointer to a
structure of type
sockaddr_un or
sockaddr_in
The size of
the structure
3
System calls for
sending/receiving
For stream sockets
For datagram
sockets
or
read/write
send/recv
as for files
sendto/recvfrom
specialized
special flags
n = send ( sd, buf, size, flags);
n = sendto ( sd, buf, size, flags,ptr, len);
Address of a struct
of type sockaddr_in
(or sockaddr_un)
Size of the struct
n = recv ( sd, buf, size, flags);
n = recvfrom ( sd, buf, size, flags, ptr, len);
Address of a struct
of type sockaddr_in
(or sockaddr_un)
Size of the struct
4
From http://linux.die.net/man/3/inet_addr
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int inet_aton(const char *cp, struct in_addr *inp);
in_addr_t inet_addr(const char *cp);
in_addr_t inet_network(const char *cp);
char *inet_ntoa(struct in_addr in);
struct in_addr inet_makeaddr(int net, int host);
in_addr_t inet_lnaof(struct in_addr in);
in_addr_t inet_netof(struct in_addr in);
Description
inet_aton() converts the Internet host address cp from the standard numbers-and-dots notation into
binary data and stores it in the structure that inp points to. inet_aton() returns non-zero if the address
is valid, zero if not.
The inet_addr() function converts the Internet host address cp from numbers-and-dots notation into
binary data in network byte order. If the input is invalid, INADDR_NONE (usually -1) is returned. This
is an obsolete interface to inet_aton(), described immediately above; it is obsolete because -1 is a
valid address (255.255.255.255), and inet_aton() provides a cleaner way to indicate error return.
The inet_network() function extracts a number suitable for use as an Internet address in host byte
order from the address cp in numbers-and-dots notation. If the input is invalid, -1 is returned.
The inet_ntoa() function converts the Internet host address in given in network byte order to a string in
standard numbers-and-dots notation. The string is returned in a statically allocated buffer, which
subsequent calls will overwrite.
The inet_makeaddr() function makes an Internet host address in network byte order by combining the
network number net with the local address host in network net, both in local host byte order.
The inet_lnaof() function returns the local host address part of the Internet address in. The local host
address is returned in local host byte order.
The inet_netof() function returns the network number part of the Internet Address in. The network
number is returned in local host byte order.
The structure in_addr as used in inet_ntoa(), inet_makeaddr(), inet_lnoaf() and inet_netof() is
defined in netinet/in.h as:
struct in_addr {
unsigned long int s_addr;
}
Note that on the i80x86 the host byte order is Least Significant Byte first (little endian), whereas the
network byte order, as used on the Internet, is Most Significant Byte first (big endian).
5
Note
When you using numbers-and-dots notation for addresses, be aware that each number will be
interpreted as octal if preceded by a 0 and as hexadecimal if preceded by 0x. For example,
inet_aton("226.000.000.037", &t) will interpret the address as 226.0.0.31 and not 226.0.0.37.
From http://linux.die.net/man/3/htons
#include <arpa/inet.h>
uint32_t htonl(uint32_t hostlong);
uint16_t htons(uint16_t hostshort);
uint32_t ntohl(uint32_t netlong);
uint16_t ntohs(uint16_t netshort);
Description
The htonl() function converts the unsigned integer hostlong from host byte order to network byte
order.
The htons() function converts the unsigned short integer hostshort from host byte order to network
byte order.
The ntohl() function converts the unsigned integer netlong from network byte order to host byte order.
The ntohs() function converts the unsigned short integer netshort from network byte order to host byte
order.
On the i80x86 the host byte order is Least Significant Byte first, whereas the network byte order, as
used on the Internet, is Most Significant Byte first.
From http://linux.die.net/man/7/ip
There are several special addresses: INADDR_LOOPBACK (127.0.0.1) always refers to the local
host via the loopback device; INADDR_ANY (0.0.0.0) means any address for binding;
INADDR_BROADCAST (255.255.255.255) means any host and has the same effect on bind as
INADDR_ANY for historical reasons.
6
A client with connection-less socket
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERV_PORT 7000 /* Port of this server */
#define SERV_IP “194.27.78.05”
#define SIZE sizeof(struct sockaddr_in)
int main(void)
{
int sd, ret, n, len;
struct socaddr_in cln, srv;
char buf[100], msg[] = “A message from client\n”;
/* Create the internet socket, of SOCK_DGRAM type. */
if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{ perror("socket problem"); exit(1); }
cln.sin_family = AF_INET;
cln.sin_port = htons(0); /*Any port*/
cln.sin_addr.s_addr = htonl(INADDR_ANY);
/* Bind the socket to the IP address of this host. */
if (bind(sd, (struct sockaddr *) &cln, SIZE) < 0)
{ perror("bind problem");
exit(1);
}
srv.sin_family = AF_INET;
srv.sin_port = htons (SERV_PORT);
srv.sin_addr.s_addr = inet_addr(SERV_IP);
/* Send a message to the socket sd*/
ret = sendto(sd, msg, 22 0, (struct sockaddr*)&srv, SIZE);
if (ret < 0) {perror("sendto problem"); exit(1);}
n=recvfrom(sd, buf, sizeof(buf), 0, (struct sockaddr*)&srv,
SIZE);
if (n < 0) {perror("recvfrom problem"); exit(1);}
buf[n] = NULL; printf(“%s\n”, buf);
close(sd); exit(0); }
7
A server with connection-less socket
#include …
/* Same as in client */
#define PORT 7000
/* Port of this server */
#define SIZE sizeof(struct sockaddr_in)
int main(void)
{
int sd, ret, n, cln_len;
struct socaddr_in srv, cln;
char buf[100], reply[] = “A reply from server\n”;
/* Create the internet socket, of SOCK_DGRAM type. */
if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{ perror("socket problem"); exit(1); }
srv.sin_family = AF_INET;
srv.sin_port = htons (PORT);
srv.sin_addr.s_addr = htonl (INADDR_ANY);
/* Bind the socket to the IP address of this host. */
if (bind(sd, (struct sockaddr *) &srv, SIZE) < 0)
{ perror("bind problem");
exit(1);
}
/* Now read from the socket s */
while(1) /* Endless loop*/
{
n = recvfrom(sd, buf, sizeof(buf), 0, (struct
sockaddr*)&cln, &cln_len);
if (n < 0) {perror("recvfrom problem"); exit(1);}
write(1, buf, n);
ret = sendto(sd, reply, sizeof(reply), 0, (struct
sockaddr*)&cln, cln_len);
if (ret < 0) {perror("sendto problem"); exit(1);}
}
close(sd);
exit(0);
}
8