Socket programming With C/C++
Network Layers • 1 – user application • 2 – internet application (well-known ports) • 3 – transport layer {TCP or UDP} • 4 – network layer (internet address) • 5 – LLC sub layer • 6 – MAC sub layer • 7 - Physical
Two important protocols • TCP/IP – Provides reliable ,in-order transfer of bytes between client and server
• UDP – Provides unreliable transfer of groups of bytes between client and server
What is SOCKET? • A well defined method of connecting two applications locally or through network • Protocol & language independent
Association • A connection between two processes using socket is called association • An association can abstractly defined by 5 terms which specifies two processes and a method of communication • A half association is a single side of association which is defined as a 3tuple
• /etc/services is used to administrate
port numbers from 0-1024 which are called well-known ports and use of them needs root access • The socket concepts came from Berkeley based UNIX systems
Socket library functions • System calls
• Startup / close • Data transfer • Optional control • ….
• Network configuration lookup • Host address • Ports for services •…
• Utility functions
• Data conversion • Address manipulation • Error handling
Methods : • socket() • bind()
– Creates a new socket and returns its descriptor – Associates a socket with a port and address
• connect() • listen()
– Establish queue for connection requests – Accepts a connection request
• accept() • recv() • send() • write() • read()
– Initiates a connection to a remote host – Receive data from a socket descriptor – Sends data to a socket descriptor
• int socket(int domain,int type,int
protocol) • Domain = AF_INET | AF_UNIX • Type = SOCK_STREAM | SOCK_DGRAM | SOCK_RAW | SOCK_SEQPACKET | SOCK_RDM • Protocol = usually 0 , but every protocol you want to use
struct sockaddr{ u_short sa_family; char sa_data[14]; } struct sockaddr_in{ short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero[8]; }
union sock{ struct sockaddr s; struct sockaddr_in i; }sock; sock.i.sin_family = AF_INET; sock.i.sin_port = port number; sock.i.sin_addr.s_addr = “ip number”; or INADDR_ANY
• bind(int s,struct sockaddr*name,int len) • S : specefies the socket
• listen(int s,int backlog) • backlog : defines the maximum
number of outstanding connections
int accept (int s,struct sockaddr * addr, int * addrlen) addrlen : points to an integer which is reserved for coming address
• connect(int s,struct
sockaddr*name,int len) • S : specefies the socket
struct fd_set{ u_int fd_count; //number of sockets in the set socket fd_array [FD_SETSIZE]; //sockets } FD_CLR (s, *set) //removes s from set FD_ISSET (s, *set) //nonzero if s is a member of the set FD_SET (s, *set) //adds s to set FD_ZERO ( *set) //initializes the set to NULL struct timeval{ long tv_sec; //seconds long tv_usec; //microseconds }
• int select (int nfds , fd_set * read , fd_set * write , fd_set * except , struct timeval * timeout ) timeout is the maximum wait time
• recv(int s,char*buff,int len,int flag) • send(int s,char*buff,int len,int flag) flag can be MSG_OOB or MSG_DONTROUTE
Server and Client • socket() • bind() • listen() • accept() • recv()/send() • close()
• socket() • connect() • send()/recv() • close()
Some server/client basics • Primary and secondary sockets • Just primary socket
Created by AYLOOZ Alireza Fathi