TCP /ip
TCP /ip
TCP /ip
SOCKET PROGRAMMING
Outline
Socket and Internet Sockets
Network programming functions
Socket System Calls
TCP Sockets Programming
UDP Sockets Programming
Sockets
• Sockets provide mechanisms to communicate
between computers across a network
• Based on C.
Types of Internet Sockets
• Different types of sockets implement different
communication types (stream vs. datagram)
• Type of socket: stream socket
– connection-oriented
– two way communication
– reliable (error free), in order delivery
– can use the Transmission Control Protocol
(TCP)
– e.g. telnet, http
• Type of socket: datagram socket
– connectionless, does not maintain an open
connection, each packet is independent
– can use the User Datagram Protocol (UDP)
– e.g. IP telephony
Data types
• IP Address
–written as dotted octets (e.g. 10.0.0.1)
–32 bits. Not a number! But often needs to
be converted to a 32-bit to use.
• Port number
–identifies a process on a host
–16 bit number
–Reserved ports ( 0 -1024 )
IP Address Data Structure
struct sockaddr_in {
short int sin_family; // Address
family
unsigned short int sin_port; // Port
number
struct in_addr sin_addr; // Internet
address
unsigned char sin_zero[8]; // Padding 0
};
struct in_addr {
unsigned long s_addr; // 4 bytes
};
Generic Socket Address
• The sockets API is generic.
sin_addr
sa_data
sin_zero
Network Programming
Functions
• Byte Ordering
• Byte Manipulation functions
• Addressing
• Socket system calls
Byte Ordering of Integers
• Different CPU architectures have different
byte ordering
memory memory
address A +1 address A
Integer representation (2 D3 F2
byte)
• Byte Ordering
• Big Endian vs. Little Endian
• Little Endian (Intel, DEC): Least
significant byte of word is stored in the
lowest memory address
• Big Endian (Sun, SGI, HP): Most
significant byte of word is stored in the
lowest
memory address
Network Byte Order Functions
‘16- and 32-bit conversion functions (for platform
independence)
Examples:
int m, n;
short int s,t;
m = ntohl (n) net-to-host long (32-bit)
translation
s = ntohs (t) net-to-host short (16-bit)
translation
n = htonl (m) host-to-net long (32-bit)
translation
t = htons (s) host-to-net short (16-bit)
translation
Byte Manipulation
Functions
• Void bzero (void *dest, size_t
nbytes)
• #include <arpa/inet.h>
• src
– (Input) The pointer to the null-terminated character string that contains the text
presentation form of an IPv4 / IPV6 address.
dst
– (Output) The pointer to a buffer into which the function stores the numeric
address. The calling application must ensure that the buffer referred to by dst is
large enough to hold the numeric address (4 bytes for AF_INET or 16 bytes for
AF_INET6).
size
• (Input) The size of the buffer pointed at by dst.
Address Access/Conversion
Functions
socket()
bind()
socket() listen()
Connection
connect() establishment accept()
write()
Data request read()
mysock = Socket(AF_INET,SOCK_STREAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( portnum );
myaddr.sin_addr = htonl( ipaddress);
mysock=socket(PF_INET,SOCK_STREAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( 80 );
myaddr.sin_addr = htonl( INADDR_ANY);
socket ( )
bind ( )
Client
socket ( )
recvfrom( )
bind ( )
blocks until data received from a client
data (request)
sendo ( )
process request
sendto ( ) recvfrom( )
data reply
int mysock;
struct sockaddr_in myaddr;
mysock = socket(PF_INET,SOCK_DGRAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( 1234 );
myaddr.sin_addr = htonl( INADDR_ANY );
mysock = socket(PF_INET,SOCK_DGRAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( S_PORT );
myaddr.sin_addr = htonl( INADDR_ANY );
bind(mysock, &myaddr, sizeof(myaddr));
while (1) {
len=sizeof(cliaddr);
msglen=recvfrom(mysock,msgbuf,MAXLEN,0,cliaddr,&clilen);
sendto(mysock,msgbuf,msglen,0,cliaddr,clilen);
}
socket
#include <sys/types.h>
#include <sys/socket.h>
int socket(int domain, int type, int protocol)
domain is either AF_UNIX, AF_INET, or AF_OSI, or ..
AF_UNIX is the Unix domain, it is used for
communication within a single computer system.
[AF_LOCAL is the Posix name for AF_UNIX.]
AF_INET is for communication on the internet to
IPv4 addresses.
type is either SOCK_STREAM (TCP, connection
oriented, reliable), or SOCK_DGRAM (UDP,
datagram, unreliable), or SOCK_RAW (IP level).
protocol specifies the protocol used. It is usually 0 to
say we want to use the default protocol for the
chosen domain and type.
readset set of fd’s for sockets you are waiting to read (so, e.g.
accept() or recv())
writeset set of fd’s for sockets you are waiting to write to (e.g.
you want to send())
FD_SETSIZE = 256
Apparently you can’t have more than 256 items in an fd_set. This
shouldn’t be an issue right now, I just mention it for
completeness.
fcntl
#include <fcntl.h>
int fcntl(int fd, int cmd, … /* int arg */)
fd identifies the socket you wish to alter
cmd the command to execute. F_GETFL will cause fcntl to
return an integer which contains all flags for the socket.
In this case pass 0 for arg.
F_SETFL will cause the socket’s state to be set according to
the flag passed as arg.
(There are other options for this. See the man page)
arg … see above
For F_GETFL, returns the flags for socket fd.
For F_SETFL, returns >= 0 for success, else < 0
e.g.
flags = fcntl(fd, F_GETFL, 0)
flags |= O_NONBLOCK (to set the socket to non-blocking)
or flags &= ~O_NONBLOCK (to set the socket back to
blocking)
fcntl(fd, F_SETFL, flags)
Thank you