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 )
struct sockaddr_in (IPv4)
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family;
in_port_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
A special kind of sockaddr structure
struct in_addr
struct in_addr {
in_addr_t s_addr;
};
in_addr_t is uint32_t
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)
uint16_t htons(uint16_t);
uint16_t ntohs(uint_16_t);
uint32_t htonl(uint32_t);
uint32_t ntohl(uint32_t);
Byte Manipulation Functions
mysock = Socket(AF_INET,SOCK_STREAM,0);
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( portnum );
myaddr.sin_addr = htonl( ipaddress);
socket()
bind()
socket() listen()
Connection
connect() establishment accept()
write()
Data request read()
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
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons( 1234 );
myaddr.sin_addr = htonl( INADDR_ANY );
int main(void) {
int sockfd, new_fd; // listen on sockfd, new connection on new_fd
struct sockaddr_in my_addr; // my address information
struct sockaddr_in their_addr; // connector's address information
int sin_size;
49
if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
close(new_fd);
}
return 0;
}
50
#include <netinet/in.h>
Client Example
#include <sys/socket.h>
if (argc != 2) {
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
buf[numbytes] = '\0';
printf("Received: %s",buf);
close(sockfd);
return 0;
}
52
Iterative (or sequential) Server
• Handles one request at a time
• Client waits for all previous requests to be processed
• Unacceptable to user if long request blocks short request
while (1) {
accept a connection (or request) from a client
service the client
close the connection (if necessary)
}
Concurrent Server
• Can handle multiple requests at a time by creating
new thread of control to handle each request
• No waiting
while (1) {
accept a connection/request from client
start a new thread to handle this client
/* the thread must close the connection! */
}
Why Was There a Need to Introduce
Concurrency in Client-Server
Internet
Client
Concurrent Server
Client
Internet
Client
Iterative Server
Client
Thank you