Elementary TCP Sockets Network Programming Lecture 1 Dr. Mona Sayed
Introduction  First: Elementary socket functions  Next: TCP client-server example  Concurrent services  Common Unix server technique  Allows server to simultaneously serve multiple clients  Server forks a separate process to serve each client
Typical TCP Client- Server Scenario  Server starts up  Client starts up and connects to server  Client contacts server  Server processes client request  Server responds to client  Client closes connection  Server goes back to waiting for clients to connect
socket()  To perform network I/O, a process first calls socket()  Called by both client and server  #include <sys/socket.h>  int socket(int family, int type, int protocol);  returns a socket descriptor
socket() parameters  family specifies the protocol family or domain  AF_INET: IPv4 protocols  AF_INET6: IPv6 protocols  AF_LOCAL: Unix domain protocols  AF_ROUTE: Routing sockets  AF_KEY: Key socket  Usually: AF_INET  AF_LOCAL might work on CATS machines
socket() parameters  type specifies type of communication  SOCK_STREAM: stream socket (Reliable, connection-oriented (TCP))  SOCK_DGRAM: datagram socket (Unreliable, connectionless (UDP))  SOCK_SEQPACKET: sequenced packet socket (Reliable, Connection-Oriented, Preserves Message Boundaries (SCTP))  SOCK_RAW: raw socket (for direct IP packet access)
socket() parameters  protocol specifies transport protocol  IPPROTO_TCP: TCP transport protocol  IPPROTO_UDP: UDP transport protocol  IPPROTO_SCTP: SCTP transport protocol  TCP provides reliable, in-order streaming communication  Resends lost packets, guarantees order  UDP provides possibly unreliable, possibly out-of-order message delivery  Doesn’t guarantee anything, but usually works fine
socket() parameters  Return Value:  On success Returns a socket → descriptor (a non-negative integer, similar to a file descriptor).  On failure Returns -1 (error can be → checked using errno).
socket() parameters The socket() function creates a communication endpoint but does not assign an address (IP + port) yet. Example Usage: Creating a TCP Socket (IPv4) This creates a TCP socket (since SOCK_STREAM with 0 defaults to TCP). Creating a UDP Socket (IPv6) This creates a UDP socket (since SOCK_DGRAM with 0 defaults to UDP).
connect()  Use by a TCP client to establish a connection with a TCP server  #include <sys/socket.h>  int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);  Returns when the connection has been established
connect()
connect() parameters  sockfd is the socket descriptor returned by socket()  servaddr is the socket address  Contains the IP address and port number of the server  Typically, struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6) is used.  addrlen is the size of the address structure
bind()  Used by server to bind a socket to an IP address/port pair. This allows clients to know where to connect.  #include <sys/socket.h>  int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);  Common error: EADDRINUSE (The requested port is already in use by
bind() parameters  sockfd is the socket descriptor returned by socket()  myaddr is the address of this server  Containing the local IP address and port.  Note: addresses are protocol-specific  Usually well-known addresses so that clients can find the server  addrlen is the length of the address
bind()
listen()  Called by a TCP server to:  Set the socket up to receive connections  Specify the maximum number of connections the kernel should queue up for this socket  #include <sys/socket.h>  int listen(int sockfd, int backlog);  Normally called after socket() and bind() and before accept()  sockfd is the socket descriptor from socket()  Backlog: The maximum number of pending connections the kernel should
accept()  Called by a TCP server to return the next completed connection from a client  Blocks if no connection is available  #include <sys/socket.h>  int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);  sockfd is the socket descriptor from socket()  cliaddr is the address of the client that connected to the server  Returns a new socket descriptor for the connection to the client
int main(int argc, char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; char buff[maxline]; time_t ticks; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); // daytime sever bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); listen(listenfd, LISTENQ); while(1) { connfd = accept(listenfd, (SA *)NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), “%.24srn”, ctime(&ticks)); write(connfd, buff, strlen(buff));
Concurrent Servers  The previous example is an iterative server  It sequentially receives connections and processes each request in turn  A concurrent server forks a child process to handle each request  Multiple requests can be handled in parallel by multiple concurrently executing child processes
pid_t pid; int listenfd, connfd; listenfd = socket(…); bind(listenfd, …); listen(listenfd, LISTENQ); while(1) { connfd = accept(listenfd, …); if( (pid = fork()) == 0) { close(listenfd); // service the request doit(connfd); close(connfd) ; exit(0); } close(connfd) ; }
close()  Normal close() is used to close sockets  #include <unistd.h>  int close(int sockfd);
getsockname() and getpeername()  getsockname() returns the local protocol address (IP+Port) associate with a socket  Used to get socket address when responding to a wildcard connection (INADDR_ANY)  getpeername() returns the foreign protocol address associated with a socket  Used to get client address in an execed process

Lecture 1 Socket programming elementary tcp sockets.pptx

  • 1.
    Elementary TCP Sockets NetworkProgramming Lecture 1 Dr. Mona Sayed
  • 2.
    Introduction  First: Elementarysocket functions  Next: TCP client-server example  Concurrent services  Common Unix server technique  Allows server to simultaneously serve multiple clients  Server forks a separate process to serve each client
  • 3.
    Typical TCP Client- ServerScenario  Server starts up  Client starts up and connects to server  Client contacts server  Server processes client request  Server responds to client  Client closes connection  Server goes back to waiting for clients to connect
  • 5.
    socket()  To performnetwork I/O, a process first calls socket()  Called by both client and server  #include <sys/socket.h>  int socket(int family, int type, int protocol);  returns a socket descriptor
  • 6.
    socket() parameters  familyspecifies the protocol family or domain  AF_INET: IPv4 protocols  AF_INET6: IPv6 protocols  AF_LOCAL: Unix domain protocols  AF_ROUTE: Routing sockets  AF_KEY: Key socket  Usually: AF_INET  AF_LOCAL might work on CATS machines
  • 7.
    socket() parameters  typespecifies type of communication  SOCK_STREAM: stream socket (Reliable, connection-oriented (TCP))  SOCK_DGRAM: datagram socket (Unreliable, connectionless (UDP))  SOCK_SEQPACKET: sequenced packet socket (Reliable, Connection-Oriented, Preserves Message Boundaries (SCTP))  SOCK_RAW: raw socket (for direct IP packet access)
  • 8.
    socket() parameters  protocolspecifies transport protocol  IPPROTO_TCP: TCP transport protocol  IPPROTO_UDP: UDP transport protocol  IPPROTO_SCTP: SCTP transport protocol  TCP provides reliable, in-order streaming communication  Resends lost packets, guarantees order  UDP provides possibly unreliable, possibly out-of-order message delivery  Doesn’t guarantee anything, but usually works fine
  • 9.
    socket() parameters  ReturnValue:  On success Returns a socket → descriptor (a non-negative integer, similar to a file descriptor).  On failure Returns -1 (error can be → checked using errno).
  • 10.
    socket() parameters The socket()function creates a communication endpoint but does not assign an address (IP + port) yet. Example Usage: Creating a TCP Socket (IPv4) This creates a TCP socket (since SOCK_STREAM with 0 defaults to TCP). Creating a UDP Socket (IPv6) This creates a UDP socket (since SOCK_DGRAM with 0 defaults to UDP).
  • 11.
    connect()  Use bya TCP client to establish a connection with a TCP server  #include <sys/socket.h>  int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);  Returns when the connection has been established
  • 12.
  • 13.
    connect() parameters  sockfdis the socket descriptor returned by socket()  servaddr is the socket address  Contains the IP address and port number of the server  Typically, struct sockaddr_in (IPv4) or struct sockaddr_in6 (IPv6) is used.  addrlen is the size of the address structure
  • 14.
    bind()  Used byserver to bind a socket to an IP address/port pair. This allows clients to know where to connect.  #include <sys/socket.h>  int bind(int sockfd, const struct sockaddr *myaddr, socklen_t addrlen);  Common error: EADDRINUSE (The requested port is already in use by
  • 15.
    bind() parameters  sockfdis the socket descriptor returned by socket()  myaddr is the address of this server  Containing the local IP address and port.  Note: addresses are protocol-specific  Usually well-known addresses so that clients can find the server  addrlen is the length of the address
  • 16.
  • 17.
    listen()  Called bya TCP server to:  Set the socket up to receive connections  Specify the maximum number of connections the kernel should queue up for this socket  #include <sys/socket.h>  int listen(int sockfd, int backlog);  Normally called after socket() and bind() and before accept()  sockfd is the socket descriptor from socket()  Backlog: The maximum number of pending connections the kernel should
  • 18.
    accept()  Called bya TCP server to return the next completed connection from a client  Blocks if no connection is available  #include <sys/socket.h>  int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);  sockfd is the socket descriptor from socket()  cliaddr is the address of the client that connected to the server  Returns a new socket descriptor for the connection to the client
  • 19.
    int main(int argc,char **argv) { int listenfd, connfd; struct sockaddr_in servaddr; char buff[maxline]; time_t ticks; listenfd = socket(AF_INET, SOCK_STREAM, 0); bzero(&servaddr, sizeof(servaddr); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); // daytime sever bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); listen(listenfd, LISTENQ); while(1) { connfd = accept(listenfd, (SA *)NULL, NULL); ticks = time(NULL); snprintf(buff, sizeof(buff), “%.24srn”, ctime(&ticks)); write(connfd, buff, strlen(buff));
  • 20.
    Concurrent Servers  Theprevious example is an iterative server  It sequentially receives connections and processes each request in turn  A concurrent server forks a child process to handle each request  Multiple requests can be handled in parallel by multiple concurrently executing child processes
  • 21.
    pid_t pid; int listenfd,connfd; listenfd = socket(…); bind(listenfd, …); listen(listenfd, LISTENQ); while(1) { connfd = accept(listenfd, …); if( (pid = fork()) == 0) { close(listenfd); // service the request doit(connfd); close(connfd) ; exit(0); } close(connfd) ; }
  • 22.
    close()  Normal close()is used to close sockets  #include <unistd.h>  int close(int sockfd);
  • 23.
    getsockname() and getpeername()  getsockname()returns the local protocol address (IP+Port) associate with a socket  Used to get socket address when responding to a wildcard connection (INADDR_ANY)  getpeername() returns the foreign protocol address associated with a socket  Used to get client address in an execed process