Download Sockets

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

Server Message Block wikipedia , lookup

Parallel port wikipedia , lookup

Dynamic Host Configuration Protocol wikipedia , lookup

AppleTalk wikipedia , lookup

Remote Desktop Services wikipedia , lookup

Internet protocol suite wikipedia , lookup

Recursive InterNetwork Architecture (RINA) wikipedia , lookup

Lag wikipedia , lookup

Cracking of wireless networks wikipedia , lookup

Real-Time Messaging Protocol wikipedia , lookup

Zero-configuration networking wikipedia , lookup

Hypertext Transfer Protocol wikipedia , lookup

Peer-to-peer wikipedia , lookup

Transcript
Client-Server model
Socket programming



http://www.cse.buffalo.edu/~milun/unix.programming.html
Java: look at java.net package at
http://java.sun.com/javase/7/docs/api/
Interprocess communication: Interface to layer 4 protocol
3 socket types:



SOCK_DGRAM: datagram mode of communication,
typical for short messages such as finger, like a
message queue in UNIX domain (AF_UNIX),
implemented via UDP in the Internet domain
(AF_INET).
SOCK_STREAM: connection-oriented 2-way byte
stream, like full-duplex pipe in UNIX domain,
implement via TCP in Internet domain (what demos
use)
SOCK_RAW: write your own, work w/ hardware
interface
Overview
Server
client
socket (create socket)
Socket (create socket)
bind (specifies IP address & port #)
Port# identifies an application (process)
listen (put socket in passive mode,
Connect (issue a connection request to
ready to accept requests)
accept (accepts a connect request)
send and receive data via socket (must
be in synch w/ client)
Close
a server)
Receive and send data via sockets
(must be in synch w/ server)
Close
Commands and structs (C language)
struct sockaddr {
Specifies generalized address family, may use in
u_short sa_family; // address type
UNIX network. This is the more generic version of
char sa_data[14] }; // address value
sockets
struct sockaddr_in {
Used with Internet domain sockets
u_short sin_family, //protocol and family
where
u_short sin_port,
struct in_addr { u_int s_addr } //IP address
//port number
struct in-addr sin_addr,
We’ll use this form
char sin_zero[8] //not used, all 0s
}
struct hostent {
char* h_name, //text address of host computer
Null terminated
char** h_aliases, //not important for us
Alternate names for host
int h_addrtype,
//address type (AF_INET)
List of host addresses
int h_length,
//address length
Just corresponds to 1st in h_addr_list
char** h_addr_list } //not important for us
#define h_addr h_addr_list[0]

gethostname(char* name, int namelength)
 Put host name into name
 Return 0 if OK, -1 if error (errno specifies the error)
 namelength is length of 1st parameter

gethostbyname(char* name)
 name has hostname
 Returns pointer to a hostent structure (make sure you
have one)
 Maps host name to IP address
 If error occurs, h_errno contains the code.



Describe htons, ntohs, htonl, ntohl (h for host, s for short, l
for long)
Establishes a standard form for which bit is least/most
significant in a type int
big endian vs. little endian (from Gulliver’s travels,
politicians who started wars because they could not agree
on whether to crack an egg on the big or little end)

socket(int domain, int type, int protocol)
 Create unbound socket
 Return socket descriptor (index into kernel
descriptor table)
 Domain: AF_INET
 Type: SOCK_STREAM
 Protocol: TCP or UDP or 0. If 0, system selects
appropriate protocol, default TCP.

bind(int s, struct sockaddr_in * sa, int size)
 s is socket descriptor
 sa points to sockaddr_in struct
 size is size of sockaddr_in struct
 Return 0 if OK, -1 if error (errno has the code)
 Bind (associate) a socket with an address and
port # specified in sa)

connect(int s, struct sockaddr_in *sa, int size)
 s … socket identifier
 sa … point to sa structure containing the port#,
protocol family and IP address of remote host
 size is length of sa structure

listen(int s, int n)
 s … socket descriptor
 n … max size of unprocessed conection requests
(queue length)
 Puts server in listen (passive) mode making it able to
accept connections.

accept (int s, struct sockaddr_in *sa, sizeof (sa))
 Accept a connection and return a new socket
descriptor w/ the same properties as s.

send(int s, address buffer, size, flags)
 Send contenst of specified memory (use 0 for flags)


recv(int s, address buffer, size, flags)
 Read into specified memory, up to max of size
close(s)
Peer-to-peer (P2P) networking



Contrast with client/server (CS) using file distribution
(mp3s, videos, etc.) as an example: C/S: Server transfers
each file to a client upon request. P2P: any pair of devices
in the P2P network is capable of exchanging files.
Devices have equivalent abilities (True P2P). Many are
hybrids. [http://en.wikipedia.org/wiki/Peer-to-peer]
[http://www.oreilly.com/catalog/peertopeer/chapter/ch
01.html]
BitTorrent: P2P file distribution protocol.




[http://computer.howstuffworks.com/bittorrent.htm]
Torrent (sometimes swarm): group of peers involved in the
distribution of a file. They exchange (download and
upload) fixed sized chunks of the file.
Tracker: each peer in a torrent registers with the tracker
and periodically informs the tracker of its status.
When a new client joins a torrent the tracker selects a
subset of peers and sends the IP addresses to the new
client (now peer).



Neighboring Peers: peers with which a TCP connection
is established.
Peer gets a list of chunks that other peers have.
Request rarest chunks first. More quickly spreads rare
chunks among peers.
A peer gives priorities (for sending chunks) to others
who are providing data at the best rate.


Recalculations are made periodically.
Free-riding or leeching (downloading without
uploading)

Index for tracking: An index lists what clients make what
files available. Must be frequently updated. Different
approaches for indexes.
 Centralized index: Similar to the original Napster. Index
service provided by a large server or server farm.
Hybrid of P2P and client/server. Drawbacks include
copyright issues (central servers can be shut down –
Napster) and performance (Napster had real problems
back in 2000) and single point of failure.




Query flooding (Gnutella
[http://en.wikipedia.org/wiki/Gnutella]
LimeWire [http://en.wikipedia.org/wiki/Limewire]
for example): [http://en.wikipedia.org/wiki/Query_flooding]
Each peer maintains a few TCP connection with others:
creates an overlay network (graph using discrete math
term)

To locate a file: send a request to all connected peers
(one hop away). If not found, do again and ask connected
peers to do the same (request goes two hops). If not
found, do again but next request goes three hops. And so
on.

To join an overlay network:
 Peer x finds one peer in the network (either from a
known list it maintains or a tracker site).
 Find a peer, y, to which it can establish a TCP
connection (use the ping-pong protocol – "ping"
message and "pong" response). Y can forward "pings"
(up to a hop count) and any peer, z, that gets it can
"pong" back to x.

Hierarchical overlay (Kazaa, Morpheus,Gnutella) – sort of
a hybrid of the previous two. No dedicated server (farm)
but not complete query flooding.
 Super peer: a peer with high bandwidth and high
availability. Like a peer, but more equal.
 A peer connects to a super peer which acts as a
tracker for other peers connected to it.
 Super peers form an overlay network among
themselves.