Recitation9_Sockets

Download Report

Transcript Recitation9_Sockets

Internet Networking
recitation #9
Socket Programming
Spring Semester 2007, Dept. of Computer Science, Technion
1
2
What is socket?




First appeared in 4.1 BSD UNIX 1982.
An integer associated with a network connection.
Implemented as file descriptor.
Types of Internet Sockets:



Stream Sockets for TCP connection.
Datagram Socket for UDP communication.
Raw Sockets.
Internet Networking
3
Socket Interface
User Space
App1
App2
Socket I/F
Kernel
IP Stack
ND1
ND2
ND3
Internet Networking
4
Structures
 ‘Struct sockaddr’ holds socket address information:

struct sockaddr {
unsigned short sa_family; // AF_INET for internet
char sa_data[14];
// 14 bytes of protocol address.
contains a destination address and port number for the
socket.
};
 struct sockaddr_in’ is used to deal with ‘struct
sockaddr’ for internet family (AF_INET):

struct sockaddr_in {
short int sin_family;
// AF_INET for internet
unsigned short int sin_port;
// Port number
struct in_addr sin_addr; // Internet address
unsigned char sin_zero[8];
// padding (must be zero)
};
Internet Networking
5
Structures (Cont.)
 A pointer to a ‘struct sockaddr_in’ can be cast to a
pointer to a ‘struct sockaddr’ and vice-versa

they both have the same size.
 In order to set the IP address one should use ‘struct
in_addr’:

struct in_addr {
unsigned long s_addr;
};
// IPv4 address
Internet Networking
6
Bytes Order

The CPU and the NIC may store and read data from the memory in a
different order.
R/W
CPU
Memory
R/W
NIC
Internet Networking
7
Two ways to store data in the memory:


Big-Endian
Little-Endian
One should convert the order of the bytes (if necessary) in order to
transmit them in network in the same way.
1
2
3
4
4
3
2
1
More:
On a big endian machine we see
Byte: U N I X
Location: 0 1 2 3
On a little-endian machine we would see:
Byte: N U X I
Location: 0 1 2 3
Internet Networking
8
Bytes Order

Accessories Function:
htons(x) – Convert short from host to network order.
 htonl(x)
 ntohs(x)
 ntohl(x)
In order to be portable one should use this function even if not
necessary.


Internet Networking
9
System Calls - Socket

int socket(int domain, int type, int protocol);





Return a file descriptor (or -1 in case of error).
domain: AF_INET for internet.
type: SOCK_STREAM for TCP, SOCK_DGRAM for UDP and
SOCK_RAW for raw socket.
protocol
Example for UDP:
// Opening socket
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
error("Could not open socket");
Internet Networking
10
System Call - Bind

Associate a socket with a port on the local machine

int bind(int sockfd, struct sockaddr *my_addr, int addrlen);





‘sockfd’ is the socket file descriptor.
Associate a socket with a port on the local machine (usually used
by servers).
‘my_addr’ is a pointer to a struct sockaddr that contains information
about the port.
‘addrlen’ is set to sizeof(struct sockaddr).
return 0 on success or -1 on failure (when?).
Internet Networking
11
Example:

// INADDR_ANY allowed to work without knowing the IP
address of the machine running on
#define SERVER_PORT 5555
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
if (bind(sockfd, (struct sockaddr*)&server_addr,
sizeof(server_addr)) < 0)
error("Could not bind to the socket");
Internet Networking
12
Wait for incoming connections, Listen & Accept

int listen(int sockfd, int backlog);




Define the maximum of pending TCP connections.
‘sockfd’ is the socket file descriptor.
‘backlog is the number of pending connections
• incoming connections wait in this queue until accept().
• this is the limit on how many can queue up
int accept(int sockfd, void *addr, int *addrlen);







Used by a TCP server to accept pending connection.
Blocked until a connection is arrived.
Return a new socket file descriptor
• used to receive and send data on this connection by send()
and recv() for example.
The original keeps listening on its port
‘sockfd’ is the new socket file descriptor.
‘addr’ contains information (port and IP address) of the incoming
connection (who is calling).
‘addrlen’ is a local integer variable set to sizeof(struct sockaddr_in).
Internet Networking
13
Connect to a Remote Host

int connect(int sockfd, struct sockaddr *serv_addr, int addrlen);




Used by clients to connect a socket to a server.
‘sockfd’ is the socket file descriptor.
‘struct sockaddr’ contains the destination port and IP address
‘addrlen’ set to sizeof(struct sockaddr).
Internet Networking
14
Data Transfer – Send over stream sockets (TCP)

int send(int sockfd, const void *msg, int len, int flags);






Transmit data through a socket.
Can be used only when the socket is connected.
Return the number of bytes that has been sent (or -1 in case of
error).
‘buf’ is a pointer to the buffer to be sent, while ‘len’ is its length.
Flags – mostly zero.
Flag example:
• MSG_DONTROUTE
• Don't use a gateway to send out the packet, only send to hosts
on directly connected networks.
Internet Networking
15
Data Transfer – Receive over stream sockets (TCP)

int recv(int sockfd, void *buf, int len, unsigned int flags);




Obtain data from the socket.
Used only when the socket is in a connected state.
‘buf’ is a pointer to the buffer for holding the received data and ‘len’
is its length.
Returns the number of bytes actually sent (or -1).
Internet Networking
16
Data Transfer – Send over Datagram sockets (UDP)


Datagram sockets are not connected to a remote host
int sendto(int sockfd, const void *msg, int len, unsigned int flags, const
struct sockaddr *to, int tolen);


Transmit data to through a socket. The destination (port and IP
address) is specified in the call.
Used for UDP communication.
Internet Networking
17
Data Transfer – Receive over stream sockets (UDP)

int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct
sockaddr *from, int *fromlen);





Obtain data from the socket.
from is a pointer to a local struct sockaddr that will be filled with the
IP address and port of the originating machine.
fromlen is a pointer to a local int that should be initialized to
sizeof(struct sockaddr).
When the function returns, fromlen will contain the length of the
address actually stored in from.
Returns the number of bytes actually sent.
Internet Networking
18
Shut Down and Close

int shutdown(int sockfd, int how);






sockfd - socket file descriptor to shutdown
How:
0 - Further receives are disallowed
1 - Further sends are disallowed
2 - Further sends and receives are disallowed (like close())
close(sockfd);


sockfd - socket file descriptor to shutdown
Only close() frees the socket descriptor
Internet Networking
19
Client-Server Model
UDP Client
UDP Server
sd=socket(…)
sd=socket(…)
bind(sd,…)
sendto(sd,…)
recvfrom(sd,…)
recvfrom(sd,…)
sendto(sd,…)
close(sd)
close(sd)
Internet Networking
20
Client-Server Model
TCP Client
TCP Server
sd=socket(…)
sd=socket(…)
connect(sd)
bind(sd,…)
listen(sd,…)
new_sd=accept(sd,…)
fork()
send(sd,…)
recv(new_sd,…)
recv(sd,…)
send(new_sd,…)
close(sd)
close(new_sd)
Internet Networking
21
Port Assignment


Ports 0 through 1023 are reserved.
In order to let the system to assign the port number, one should set the
port number (e.g. in ‘bind’) to zero.
Internet Networking