Basic Socket Programming: - TCP/IP Overview. - TCP Interface
Basic Socket Programming: - TCP/IP Overview. - TCP Interface
• TCP/IP overview.
• TCP interface
• Reference:
– UNIX Network Programming, by Richard Stevens.
– UNIX man page.
• Overview of TCP/IP protocols
–Application layer(ssh,http,etc)
– Transport layer (TCP, UDP)
– Network layer (IPv4, IPv6)
– Host to Network layer (Ethernet)
• Some concepts:
– Entity (process/hardware/system
calls)
– Protocol: How peer entities
interact with each other.
– Service interface: How upper
layer entities interact with lower
layer entities.
• Socket Programming: the use of
TCP and UDP.
• TCP: Transmission control
protocol.
• connection-oriented, reliable,
full duplex, byte stream service
• Interface: socket, bind, listen,
accept, connect, read, write,
close.
– An analogy:
Socket: telephone
Bind: assign telephone number to a
telephone
Listen: turn on the ringer so that you
can hear the phone call
Connect: dial a phone number
Accept: answer the phone
Read/write: talking
Close: ???
• To send:
– Socket, connect write
• To receive:
– Socket, bind, listen, accept read
• TCP endpoint:
– IP address + port number
• Basic TCP sockets.
#include <sys/socket.h>
int socket(int family, int type, int
protocol);
• Family: AF_INET (PF_INET).
• Type: SOCK_STREAM (TCP)
SOCK_DGRAM (UDP)
• Protocol: = 0
• Return descriptor, -1 on error.
• Connect:
#include <sys/socket.h>
int connect(int sockfd, const
struct sockaddr *servaddr,
socklen_t addrlen);
struct sockaddr_in {
uint8_t sin_len; struct sockaddr {
sa_family_t sin_family; uint8_t sa_len;
in_port_t sin_port; sa_family_t sa_family;
struct in_addr sin_addr; char sa_data[14];
char sin_zero[8]; }
}
– See example3.c
• Some byte manipulation functions:
#include <strings.h>
Void *memset(void *dst, int c, size_t len);
Void *memcpy(void *dst, void *src, size_t
nbytes);
Void *memcmp(const void *ptr1, const
void *ptr2, size_t nbytes);
• Address conversion functions
• inet_aton/inet_addr/inet_ntoa